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

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.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to find the  every segment size of
// array have a search key x
#include <bits/stdc++.h>
using namespace std;
  
bool findxinkindowSize(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<n; j++)
      if (arr[j] == x)
          break;
    if (j == n)
       return false;  
       
    return true;
}
  
// main driver
int main()
{
    int arr[] = { 3, 5, 2, 4, 9, 3, 1, 7, 3, 11, 12, 3 };
    int x = 3, k = 3;
    int n = sizeof(arr) / sizeof(arr[0]);
    if (findxinkindowSize(arr, x, k, n))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to find the every 
// segment size of array have
// a search key x
class GFG 
{
static boolean findxinkindowSize(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 < n; j++)
    if (arr[j] == x)
        break;
    if (j == n)
    return false
      
    return true;
}
  
// 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 (findxinkindowSize(arr, x, k, n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by Kirti_Mangal

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find 
# the every segment size of 
# array have a search key x
  
def findxinkindowSize(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 (findxinkindowSize(arr, x, k, n)) :
        print("Yes")
    else :
        print("No")
          
# This code is contributed 
# by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to find the every 
// segment size of array have
// a search key x
using System;
  
class GFG 
{
static bool findxinkindowSize(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 (findxinkindowSize(arr, x, k, n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to find the every 
// segment size of array have 
// a search key x 
  
function findxinkindowSize(&$arr, $x
                            $k, $n
    for ($i = 0; 
         $i < $n; $i = $i + $k
    
  
        // Search x in segment 
        // starting from index i. 
        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. 
    for ($j = $i - $k; $j < $n; $j++) 
    if ($arr[$j] == $x
        break
    if ($j == $n
    return false; 
      
    return true; 
  
// Driver Code 
$arr = array(3, 5, 2, 4, 9, 3, 1,
             7, 3, 11, 12, 3); 
$x = 3;
$k = 3; 
$n = sizeof($arr); 
if (findxinkindowSize($arr, $x, $k, $n)) 
    echo "Yes"
else
    echo "No"
  
// This code is contributed 
// by Shivi_Aggarwal
?>

chevron_right


Output:

Yes

Time Complexity: O(n)



My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.