﻿ C# - A couple of 5s next to each other or separated by one

# 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:

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 Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿