Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

C# Sharp: Basic Algorithm Exercises - Check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere.

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: Flowchart: Check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere.

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.