DSA- L1- Check if a key is present in every segment of size k in an array

Kapil Patel
1 min readJul 6, 2021

The question is taken from GeeksForGeeks, links below. Solution is my own code.

Use CTRL + ALT + 6 to format code in Medium

My first solution, which passes the test case but may not be most efficient

I will keep updating this with better solution.

Solution using C#.

using System;public class GFG{
static public void Main (){
//Code

int[] arr = {5, 8, 7, 12, 14, 3, 9};
int findItem = 8;
int chunk = 2;

int counter = 0;
bool found = false;
for(int i=0; i<arr.Length;i++){
if(arr[i] == findItem ){
found = true;
}
counter++;
if(counter == chunk && found == false )
{
break;
}else{
counter = 0;
found = true;
}
}
Console.WriteLine(found);

}
}

This post is a first part of DSA RoadMap by The Code Skool

Youtube video:

https://www.youtube.com/watch?v=WjYdkHzcGhc&list=PL6ZXPAW2GSTuO4m6wCITtyVobnD1O1dkj

GeeksForGeeks top 50 array coding problems

https://www.geeksforgeeks.org/top-50-array-coding-problems-for-interviews/

Item 1 from above top 50 array problems

https://www.geeksforgeeks.org/check-if-a-key-is-present-in-every-segment-of-size-k-in-an-array/

--

--

Kapil Patel

Software engineer | loves working in a startup like environment