C# Sharp Basic Algorithm Exercises: Check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere
C# Sharp Basic Algorithm: Exercise-34 with Solution
Write a C# Sharp program to check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere.
Pictorial Presentation:
Sample Solution:-
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(test(new[] {1,1,2,3,1}));
Console.WriteLine(test(new[] {1,1,2,4,1}));
Console.WriteLine(test(new[] {1,1,2,1,2,3}));
Console.ReadLine();
}
public static bool test(int[] nums)
{
for (var i = 0; i < nums.Length-2; i++)
{
if (nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3)
return true;
}
return false;
}
}
}
Sample Output:
True False True
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to check if one of the first 4 elements in an array of integers is equal to a given element.
Next: Write a C# Sharp program to comapre two given strings and return the number of the positions where they contain the same length 2 substring.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework