Greatest contiguous sub-array of size K

Given an array arr[] of integers and an integer K, the task is to find the greatest contiguous sub-array of size K. Sub-array X is said to be greater than sub-array Y if the first non-matching element in both the sub-arrays has a greater value in X than in Y.

Examples:

Input: arr[] = {1, 4, 3, 2, 5}, K = 4
Output: 4 3 2 5
Two subarrays are {1, 4, 3, 2} and {4, 3, 2, 5}.
Hence, the greater one is {4, 3, 2, 5}

Input: arr[] = {1, 9, 2, 7, 9, 3}, K = 3
Output: 9 2 7

Approach: Generate all the sub-arrays of size K and store them in any Data-Structure. Sort all the sub-arrays, and the answer will be the last sub-array in the sorted Data-structure. In C++, we can use a vector of vectors to store sub-arrays of size K.

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns the sub-array
vector<int> findSubarray(int a[], int k, int n)
{
  
    // Data-structure to store all
    // the sub-arrays of size K
    vector<vector<int> > vec;
  
    // Iterate to find all the sub-arrays
    for (int i = 0; i < n - k + 1; i++) {
        vector<int> temp;
  
        // Store the sub-array elements in the array
        for (int j = i; j < i + k; j++) {
            temp.push_back(a[j]);
        }
  
        // Push the vector in the container
        vec.push_back(temp);
    }
  
    // Sort the vector of elements
    sort(vec.begin(), vec.end());
  
    // The last sub-array in the sorted order
    // will be the answer
    return vec[vec.size() - 1];
}
  
// Driver code
int main()
{
    int a[] = { 1, 4, 3, 2, 5 };
    int k = 4;
    int n = sizeof(a) / sizeof(a[0]);
  
    // Get the sub-array
    vector<int> ans = findSubarray(a, k, n);
  
    for (auto it : ans)
        cout << it << " ";
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function that returns the sub-array
def findSubarray(a, k, n):
  
    # Data-structure to store all
    # the sub-arrays of size K
    vec=[]
  
    # Iterate to find all the sub-arrays
    for i in range(n-k+1):
        temp=[]
  
        # Store the sub-array elements in the array
        for j in range(i,i+k):
            temp.append(a[j])
  
        # Push the vector in the container
        vec.append(temp)
  
    # Sort the vector of elements
    vec=sorted(vec)
  
    # The last sub-array in the sorted order
    # will be the answer
    return vec[len(vec) - 1]
  
# Driver code
  
a =[ 1, 4, 3, 2, 5 ]
k = 4
n = len(a)
  
# Get the sub-array
ans = findSubarray(a, k, n)
  
for it in ans:
    print(it,end=" ")
      
# This code is contributed by mohit kumar 29

chevron_right


Output:

4 3 2 5


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.



Improved By : mohit kumar 29