w3resource

C#: Check a given array of integers and return true if the given array contains two 5's next to each other, or two 5 separated by one element


C# Sharp Basic Algorithm: Exercise-120 with Solution

Write a C# Sharp program to check a given array of integers. Return true if the given array contains two 5's next to each other, or two 5's separated by one element.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check a given array of integers and return true if the given array contains two 5's next to each other, or two 5 separated by one element.

Sample Solution:

C# Sharp Code:

using System; // Importing the System namespace

namespace exercises // Defining a namespace called 'exercises'
{
    class Program // Defining a class named 'Program'
    {
        static void Main(string[] args) // The entry point of the program
        {               
            // Outputting the result of the 'test' method with different integer arrays as arguments
            Console.WriteLine(test(new[] { 5, 5, 1, 5, 5 })); // Testing the method with an array containing at least two consecutive 5s
            Console.WriteLine(test(new[] { 1, 2, 3, 4 })); // Testing the method with an array containing no consecutive 5s
            Console.WriteLine(test(new[] { 3, 3, 5, 5, 5, 5 })); // Testing the method with an array containing two or more consecutive 5s
            Console.WriteLine(test(new[] { 1, 5, 5, 7, 8, 10 })); // Testing the method with an array containing two non-consecutive 5s
        }           

        // Method to check if an array contains at least two consecutive occurrences of the number 5
        static bool test(int[] numbers)
        {
            int len = numbers.Length; // Storing the length of the input array

            for (int i = 0; i < len - 1; i++) // Loop through the array except for the last element
            {
                // Check if two consecutive elements are both 5s or if a 5 is followed by another 5 two elements ahead
                if ((numbers[i] == 5 && numbers[i + 1] == 5) || (i + 2 < len && numbers[i] == 5 && numbers[i + 2] == 5))
                    return true; // Return true if two consecutive 5s are found or a 5 is followed by another 5 two positions ahead
            }

            return false; // Return false if no such sequence is found in the array
         }    
   } 
}

Sample Output:

True
False
True
True

Flowchart:

C# Sharp: Flowchart: Check a given array of integers and return true if the given array contains two 5's next to each other, or two 5 separated by one element.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to check if an array of integers contains a 3 next to a 3 or a 5 next to a 5 or both.
Next: Write a C# Sharp program to check a given array of integers and return true if there is a 3 with a 5 somewhere later in the given array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/basic-algo/csharp-basic-algorithm-exercises-120.php