Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save schauhan232/9ec91649ba35c9a98a2458ca83eae314 to your computer and use it in GitHub Desktop.
Save schauhan232/9ec91649ba35c9a98a2458ca83eae314 to your computer and use it in GitHub Desktop.
GeeksForGeeks - Check if a key is present in every segment of size k in an array C#
class Program
{
public static void Main(string[] args)
{
int[] userInputArray = { 21, 23, 56, 65, 34, 54, 76, 32, 23, 45, 21, 23, 25 } ;
int userAskedNumberToBeFind = 23;
int segment = 5;
var output = "NO";
var loopLength = userInputArray.Length / segment;
for (var i = 0; i < loopLength; i++)
{
if (CheckElementExists(i * segment, segment - 1, userAskedNumberToBeFind, userInputArray))
{
output = "YES";
}
else
{
output = "NO";
break;
}
}
Console.WriteLine(output);
Console.Read();
}
public static bool CheckElementExists(int start, int end, int numberToBeFind, int[] input)
{
var loopLength = start + end;
for (var i = start; i <= loopLength; i++)
{
if (input[i] == numberToBeFind)
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment