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:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findSubarray( int a[], int k, int n)
{
vector<vector< int > > vec;
for ( int i = 0; i < n - k + 1; i++) {
vector< int > temp;
for ( int j = i; j < i + k; j++) {
temp.push_back(a[j]);
}
vec.push_back(temp);
}
sort(vec.begin(), vec.end());
return vec[vec.size() - 1];
}
int main()
{
int a[] = { 1, 4, 3, 2, 5 };
int k = 4;
int n = sizeof (a) / sizeof (a[0]);
vector< int > ans = findSubarray(a, k, n);
for ( auto it : ans)
cout << it << " " ;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static ArrayList<Integer> findSubarray( int a[],
int k, int n)
{
ArrayList<
ArrayList<Integer>> vec = new ArrayList<
ArrayList<Integer>>();
for ( int i = 0 ; i < n - k + 1 ; i++)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
for ( int j = i; j < i + k; j++)
{
temp.add(a[j]);
}
vec.add(temp);
}
Collections.sort(vec, new Comparator<ArrayList<Integer>>()
{
@Override
public int compare(ArrayList<Integer> o1,
ArrayList<Integer> o2)
{
return o1.get( 0 ).compareTo(o2.get( 0 ));
}
});
return vec.get(vec.size() - 1 );
}
public static void main(String[] args)
{
int a[] = { 1 , 4 , 3 , 2 , 5 };
int k = 4 ;
int n = a.length;
ArrayList<Integer> ans = findSubarray(a, k, n);
for ( int it: ans)
{
System.out.print(it + " " );
}
}
}
|
Python3
def findSubarray(a, k, n):
vec = []
for i in range (n - k + 1 ):
temp = []
for j in range (i,i + k):
temp.append(a[j])
vec.append(temp)
vec = sorted (vec)
return vec[ len (vec) - 1 ]
a = [ 1 , 4 , 3 , 2 , 5 ]
k = 4
n = len (a)
ans = findSubarray(a, k, n)
for it in ans:
print (it,end = " " )
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG
{
static List< int > findSubarray( int [] a, int k, int n)
{
List<List< int >> vec = new List<List< int >>();
for ( int i = 0; i < n - k + 1; i++)
{
List< int > temp = new List< int >();
for ( int j = i; j < i + k; j++)
{
temp.Add(a[j]);
}
vec.Add(temp);
}
vec.OrderBy( l => l[0]);
return vec[vec.Count - 1];
}
static public void Main (){
int [] a = { 1, 4, 3, 2, 5 };
int k = 4;
int n = a.Length;
List< int > ans = findSubarray(a, k, n);
foreach ( int it in ans)
{
Console.Write(it + " " );
}
}
}
|
Javascript
<script>
function findSubarray(a, k, n)
{
var vec = [];
for ( var i = 0; i < n - k + 1; i++) {
var temp = [];
for ( var j = i; j < i + k; j++) {
temp.push(a[j]);
}
vec.push(temp);
}
vec.sort()
return vec[vec.length - 1];
}
var a = [1, 4, 3, 2, 5];
var k = 4;
var n = a.length;
var ans = findSubarray(a, k, n);
ans.forEach(it => {
document.write(it+ " " )
});
</script>
|
Time Complexity: O(n*n), as nested loops are used
Auxiliary Space: O(n), as extra space of size n is used to make vector and vector of vectors.
Last Updated :
13 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...