Minimum common element in all subarrays of size K
Last Updated :
06 Oct, 2021
Given an array arr[] consisting of N distinct integers and a positive integer K, the task is to find the minimum element that occurs in all subarrays of size K. If no such element exists, then print “-1”.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}, K = 4
Output: 2
Explanation:
The subarrays of size 4 are {1, 2, 3, 4} and {2, 3, 4, 5}. The common elements in the above subarrays are {2, 3, 4}.
The minimum of the above common element is 2.
Input: arr[] = {1, 2, 3, 4, 5}, K = 2
Output: -1
Explanation:
The subarrays of size 2 are {1, 2}, {2, 3}, {3, 4}, {4, 5}. Since there is no common element, print -1.
Naive Approach: The idea is to generate all possible subarrays of the given array of size K and find the common elements in all the subarrays formed. After, finding the common elements, print the minimum among them. If no element is found to be common in all the subarrays, then print “-1”.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to first check the condition for the common element in all the subarrays and if such an element exists, then it should be within the range [N – K, K], in the given array. Below are the conditions where we can find any such minimum element:
- If N is odd and K ? (N + 1)/2.
- If N is even and K ? ((N + 1)/2) + 1.
If the above conditions don’t satisfy, then the minimum element lies in the range [N – K, K]. Therefore, iterate over the given array in this range and print the value of the minimum element in it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minCommonElementInSubarrays(
int arr[], int N, int K)
{
int c;
if ((N + 1) % 2 == 0) {
c = (N + 1) / 2;
}
else {
c = (N + 1) / 2 + 1;
}
if (K < c) {
cout << -1;
}
else {
int ar = INT_MAX;
for ( int i = N - K; i < K; i++) {
ar = min(arr[i], ar);
}
cout << ar;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int K = 4;
int N = sizeof (arr) / sizeof (arr[0]);
minCommonElementInSubarrays(arr, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void minCommonElementInSubarrays( int arr[],
int N, int K)
{
int c;
if ((N + 1 ) % 2 == 0 )
{
c = (N + 1 ) / 2 ;
}
else
{
c = (N + 1 ) / 2 + 1 ;
}
if (K < c)
{
System.out.print(- 1 );
}
else
{
int ar = Integer.MAX_VALUE;
for ( int i = N - K; i < K; i++)
{
ar = Math.min(arr[i], ar);
}
System.out.print(ar);
}
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int K = 4 ;
int N = arr.length;
minCommonElementInSubarrays(arr, N, K);
}
}
|
Python3
import sys
def minCommonElementInSubarrays(arr, N, K):
c = 0
if ((N + 1 ) % 2 = = 0 ):
c = (N + 1 ) / / 2
else :
c = (N + 1 ) / 2 + 1
if (K < c):
print ( - 1 )
else :
ar = sys.maxsize
for i in range (N - K, K):
ar = min (arr[i], ar)
print (ar)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
K = 4
N = len (arr)
minCommonElementInSubarrays(arr, N, K)
|
C#
using System;
class GFG{
static void minCommonElementInSubarrays( int [] arr,
int N, int K)
{
int c;
if ((N + 1) % 2 == 0)
{
c = (N + 1) / 2;
}
else
{
c = (N + 1) / 2 + 1;
}
if (K < c)
{
Console.Write(-1);
}
else
{
int ar = Int32.MaxValue;
for ( int i = N - K; i < K; i++)
{
ar = Math.Min(arr[i], ar);
}
Console.Write(ar);
}
}
public static void Main ()
{
int [] arr = { 1, 2, 3, 4, 5 };
int K = 4;
int N = arr.Length;
minCommonElementInSubarrays(arr, N, K);
}
}
|
Javascript
<script>
function minCommonElementInSubarrays(arr, N, K)
{
let c;
if ((N + 1) % 2 == 0) {
c = parseInt((N + 1) / 2);
}
else {
c = parseInt((N + 1) / 2) + 1;
}
if (K < c) {
document.write(-1);
}
else {
let ar = Number.MAX_VALUE;
for (let i = N - K; i < K; i++) {
ar = Math.min(arr[i], ar);
}
document.write(ar);
}
}
let arr = [ 1, 2, 3, 4, 5 ];
let K = 4;
let N = arr.length;
minCommonElementInSubarrays(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...