 GeeksforGeeks App
Open App Browser
Continue

# Check if a key is present in every segment of size k in an array

Given an array arr[] and size of array is n and one another key x, and give you a segment size k. The task is to find that the key x present in every segment of size k in arr[].
Examples:

Input :
arr[] = { 3, 5, 2, 4, 9, 3, 1, 7, 3, 11, 12, 3}
x = 3
k = 3
Output : Yes
Explanation: There are 4 non-overlapping segments of size k in the array, {3, 5, 2}, {4, 9, 3}, {1, 7, 3} and {11, 12, 3}. 3 is present all segments.

Input :
arr[] = { 21, 23, 56, 65, 34, 54, 76, 32, 23, 45, 21, 23, 25}
x = 23
k = 5
Output :Yes
Explanation: There are three segments and last segment is not full {21, 23, 56, 65, 34}, {54, 76, 32, 23, 45} and {21, 23, 25}.
23 is present all window.

Input :arr[] = { 5, 8, 7, 12, 14, 3, 9}
x = 8
k = 2
Output : No

Approach:

The idea is simple, we consider every segment of size k and check if x is present in the window or not. We need to carefully handle the last segment.

Algorithm:

Step 1: Create a function named “findxinkwindowSize” which takes input parameters  “N “- the size of the array, “arr” – the input  array, “x” – the search key, “k” – the segment size.
Step 2: Create a boolean variable and initialize it to false
Step 3: Traverse i from 0 to N-1 in steps of k.
Step 4:  Now, for each traversed i , iterate j from 0 to k-1 and perform the following:
a. Check if the (i+j)th element of the array arr is equal to x. If yes, break out of the inner loop.
b. If j equals k, return false.
c. If (i+j) is greater than or equal to N, return false.
Step 5: Return true if I is more than or equal to N; else, return b’s value.

Below is the implementation of the above approach:

## C++

 `// C++ code to find the  every segment size of``// array have a search key x``#include ``using` `namespace` `std;` `bool` `findxinkwindowSize(``int` `arr[], ``int` `x, ``int` `k, ``int` `n)``{``    ``int` `i;``    ``for` `(i = 0; i < n; i = i + k) {` `        ``// Search x in segment starting``        ``// from index i.``        ``int` `j;``        ``for` `(j = 0; j < k; j++)``            ``if` `(arr[i + j] == x)``                ``break``;` `        ``// If loop didn't break``        ``if` `(j == k)``           ``return` `false``;``    ``}` `    ``// If n is a multiple of k``    ``if` `(i == n)``       ``return` `true``;` `    ``// Check in last segment if n``    ``// is not multiple of k.``    ``int` `j;``    ``for` `(j=i-k; j

## Java

 `// Java code to find the every``// segment size of array have``// a search key x``import` `java.util.*;``class` `GFG {``    ``static` `boolean` `findxinkwindowSize(``int` `N, ``int``[] arr,``                                     ``int` `x, ``int` `k)``    ``{``        ``int` `i;``        ``boolean` `b = ``false``;``      ` `        ``// Iterate from 0 to N - 1``        ``for` `(i = ``0``; i < N; i = i + k) {``          ` `            ``// Iterate from 0 to k - 1``            ``for` `(``int` `j = ``0``; j < k; j++) {``                ``if` `(i + j < N && arr[i + j] == x)``                    ``break``;` `                ``if` `(j == k)``                    ``return` `false``;``                ``if` `(i + j >= N)``                    ``return` `false``;``            ``}``        ``}``        ``if` `(i >= N)``            ``return` `true``;``        ``else``            ``return` `b;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `arr[] = ``new` `int``[] { ``3``, ``5``, ``2``, ``4``,  ``9``,  ``3``,``                                ``1``, ``7``, ``3``, ``11``, ``12``, ``3` `};``        ``int` `x = ``3``, k = ``3``;``        ``int` `n = arr.length;``        ``if` `(findxinkwindowSize(n, arr, x, k))``            ``System.out.println(``"Yes"``);``        ``else``            ``System.out.println(``"No"``);``    ``}``}` `// This code is contributed by Vivek258709`

## Python 3

 `# Python 3 program to find``# the every segment size of``# array have a search key x` `def` `findxinkwindowSize(arr, x, k, n) :` `    ``i ``=` `0``    ``while` `i < n :` `        ``j ``=` `0``        ` `        ``# Search x in segment``        ``# starting from index i``        ``while` `j < k :``            ` `            ``if` `arr[i ``+` `j] ``=``=` `x :``                ``break``            ` `            ``j ``+``=` `1` `        ``# If loop didn't break``        ``if` `j ``=``=` `k :``            ``return` `False` `        ``i ``+``=` `k``        ` `    ``# If n is a multiple of k    ``    ``if` `i ``=``=` `n :``        ``return` `True` `    ``j ``=` `i ``-` `k``    ` `    ``# Check in last segment if n``    ``# is not multiple of k.``    ``while` `j < n :``        ``if` `arr[j] ``=``=` `x :``            ``break` `        ``j ``+``=` `1` `    ``if` `j ``=``=` `n :``        ``return` `False` `    ``return` `True` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``3``, ``5``, ``2``, ``4``, ``9``, ``3``,``            ``1``, ``7``, ``3``, ``11``, ``12``, ``3` `]``    ``x, k ``=` `3``, ``3``    ``n ``=` `len``(arr)``    ` `    ``if` `(findxinkwindowSize(arr, x, k, n)) :``        ``print``(``"Yes"``)``    ``else` `:``        ``print``(``"No"``)``        ` `# This code is contributed``# by ANKITRAI1`

## C#

 `// C# code to find the every``// segment size of array have``// a search key x``using` `System;` `class` `GFG``{``static` `bool` `findxinkwindowSize(``int``[] arr, ``int` `x,``                              ``int` `k, ``int` `n)``{``    ``int` `i;``    ``for` `(i = 0; i < n; i = i + k)``    ``{` `        ``// Search x in segment``        ``// starting from index i.``        ``int` `j;``        ``for` `(j = 0; j < k; j++)``            ``if` `(arr[i + j] == x)``                ``break``;` `        ``// If loop didn't break``        ``if` `(j == k)``        ``return` `false``;``    ``}` `    ``// If n is a multiple of k``    ``if` `(i == n)``    ``return` `true``;` `    ``// Check in last segment if``    ``// n is not multiple of k.``    ``int` `l;``    ``for` `(l = i - k; l < n; l++)``    ``if` `(arr[l] == x)``        ``break``;``    ``if` `(l == n)``    ``return` `false``;``    ` `    ``return` `true``;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int``[] arr = ``new` `int``[] {3, 5, 2, 4, 9, 3,``                         ``1, 7, 3, 11, 12, 3};``    ``int` `x = 3, k = 3;``    ``int` `n = arr.Length;``    ``if` `(findxinkwindowSize(arr, x, k, n))``        ``Console.Write(``"Yes"``);``    ``else``        ``Console.Write(``"No"``);``}``}` `// This code is contributed by ChitraNayal`

## PHP

 ``

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1) as constant space is being used.

My Personal Notes arrow_drop_up