For each value in [1, N] find Minimum element present in all Subarray of that size
Last Updated :
29 Jun, 2022
Given an array A[] of size N, the task is to find the minimum element present in all subarrays for all sizes from 1 to N where all the elements in the array are in range 1 to N
Examples:
Input: A[ ] = {1, 2, 3}
Output: [-1, 2, 1]
Explanation: All subarrays of size 1 {{1}, {2}, {3}} there is no common value
For subarrays of size 2 {{1, 2}, {2, 3}} the minimum common element is 2
For subarrays of size 3 {{1, 2, 3}} the minimum common element is 1 Hence, ans=[-1, 2, 1]
Input: A[ ] = {1, 2, 1, 3, 1}
Output: [-1, 1, 1, 1, 1]
Naive Approach: The basic idea to solve the problem is to find all the subarrays for all the sizes in the range [1, N]. Now for all subarrays of the same size find the minimum common element in those subarrays. Follow the steps mentioned below to solve the problem:
- Iterate a loop from i = 1 to N:
- Create every possible subarray of size i.
- Count the frequency of each element in all the subarrays.
- Check if the occurrence of any element is equal to the total number of subarrays of that size
- Store the first element satisfying the above conditions
- Return the resultant array of the minimum common elements.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > Calculate_Min_array(vector< int >& A,
int N)
{
vector< int > ans(N + 1, -1), cnt(N + 1, 0),
res(N + 1, 0);
for ( int i = 1; i <= N; i++) {
for ( int j = 0; j < N - i + 1;
j++) {
for ( int k = j; k < j + i;
k++) {
cnt[A[k]]++;
}
for ( int k = 1; k <= N; k++) {
if (cnt[k]) {
res[k]++;
cnt[k] = 0;
}
}
}
for ( int j = 1; j <= N; j++) {
if (res[j] == (N - i + 1)) {
ans[i] = j;
break ;
}
res[j] = 0;
}
}
return ans;
}
void print(vector< int > vec, int N)
{
vector< int > ans
= Calculate_Min_array(vec, N);
for ( int i = 1; i <= N; i++)
cout << ans[i] << " " ;
cout << "\n" ;
}
int main()
{
vector< int > A = { 1, 2, 3 };
int N = 3;
print(A, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int [] Calculate_Min_array( int A[], int N)
{
int ans[] = new int [N + 1 ];
int cnt[] = new int [N + 1 ];
int res[] = new int [N + 1 ];
for ( int i = 0 ; i < N + 1 ; i++) {
ans[i] = - 1 ;
}
for ( int i = 1 ; i <= N; i++) {
for ( int j = 0 ; j < N - i + 1 ; j++) {
for ( int k = j; k < j + i; k++) {
cnt[A[k]]++;
}
for ( int k = 1 ; k <= N; k++) {
if (cnt[k] != 0 ) {
res[k]++;
cnt[k] = 0 ;
}
}
}
for ( int j = 1 ; j <= N; j++) {
if (res[j] == (N - i + 1 )) {
ans[i] = j;
break ;
}
res[j] = 0 ;
}
}
return ans;
}
public static void print( int vec[], int N)
{
int ans[] = Calculate_Min_array(vec, N);
for ( int i = 1 ; i <= N; i++)
System.out.print(ans[i] + " " );
System.out.println();
}
public static void main(String[] args)
{
int A[] = { 1 , 2 , 3 };
int N = 3 ;
print(A, N);
}
}
|
Python3
def Calculate_Min_array(A, N):
ans = [ - 1 for i in range (N + 1 )]
cnt = [ 0 for i in range (N + 1 )]
res = [ 0 for i in range (N + 1 )]
for i in range ( 1 , N + 1 ):
for j in range (N - i + 1 ):
for k in range (j, j + i):
cnt[A[k]] = cnt[A[k]] + 1
for k in range ( 1 , N + 1 ):
if (cnt[k]):
res[k] + = 1
cnt[k] = 0
for j in range ( 1 ,N + 1 ):
if (res[j] = = (N - i + 1 )):
ans[i] = j
break
res[j] = 0
return ans
def Print (vec,N):
ans = Calculate_Min_array(vec, N)
for i in range ( 1 ,N + 1 ):
print (ans[i] ,end = " " )
print ("")
A = [ 1 , 2 , 3 ]
N = 3
Print (A, N)
|
C#
using System;
class GFG {
static int [] Calculate_Min_array( int [] A, int N)
{
int [] ans = new int [N + 1];
int [] cnt = new int [N + 1];
int [] res = new int [N + 1];
for ( int i = 0; i < N + 1; i++) {
ans[i] = -1;
}
for ( int i = 1; i <= N; i++) {
for ( int j = 0; j < N - i + 1; j++) {
for ( int k = j; k < j + i; k++) {
cnt[A[k]]++;
}
for ( int k = 1; k <= N; k++) {
if (cnt[k] != 0) {
res[k]++;
cnt[k] = 0;
}
}
}
for ( int j = 1; j <= N; j++) {
if (res[j] == (N - i + 1)) {
ans[i] = j;
break ;
}
res[j] = 0;
}
}
return ans;
}
static void print( int [] vec, int N)
{
int [] ans = Calculate_Min_array(vec, N);
for ( int i = 1; i <= N; i++)
Console.Write(ans[i] + " " );
Console.WriteLine();
}
public static int Main()
{
int [] A = new int [] { 1, 2, 3 };
int N = 3;
print(A, N);
return 0;
}
}
|
Javascript
<script>
const Calculate_Min_array = (A, N) => {
let ans = new Array(N + 1).fill(-1), cnt = new Array(N + 1).fill(0),
res = new Array(N + 1).fill(0);
for (let i = 1; i <= N; i++) {
for (let j = 0; j < N - i + 1;
j++) {
for (let k = j; k < j + i;
k++) {
cnt[A[k]]++;
}
for (let k = 1; k <= N; k++) {
if (cnt[k]) {
res[k]++;
cnt[k] = 0;
}
}
}
for (let j = 1; j <= N; j++) {
if (res[j] == (N - i + 1)) {
ans[i] = j;
break ;
}
res[j] = 0;
}
}
return ans;
}
const print = (vec, N) => {
let ans
= Calculate_Min_array(vec, N);
for (let i = 1; i <= N; i++)
document.write(`${ans[i]} `);
document.write( "<br/>" );
}
let A = [1, 2, 3];
let N = 3;
print(A, N);
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: The idea to solve the problem efficiently is as follows:
If two consecutive occurrence of a value A[i] is maximum x, then it is part of all the subarrays of size x but not of all subarrays with size less than x.
Follow the steps below to implement the above idea:
- Create an array (say pos[i]) for each ith element to store the positions of the ith element the array.
- Then calculate the value of the maximum adjacent difference for every element.
- Iterate from i = 1 to N:
- Start another loop from j = maximum adjacent difference two i to N:
- If the answer for jth size subarray is not found then i is the minimum common element for all j sized subarray and continue for higher values of j.
- Otherwise, break from the loop because all the higher values must also be filled
- Return the resultant array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > Calculate_Min_array(vector< int >& A,
int N)
{
vector< int > mx(N + 1, -1), ans(N + 1, -1);
vector< int > pos[N + 1];
for ( int i = 1; i <= N; i++) {
pos[i].push_back(0);
}
for ( int i = 0; i < N; i++) {
int x = A[i];
pos[x].push_back(i + 1);
}
for ( int i = 1; i <= N; i++) {
pos[i].push_back(N + 1);
}
for ( int i = 1; i <= N; i++) {
for ( int j = 0;
j < pos[i].size() - 1; j++) {
mx[i] = max(mx[i],
pos[i][j + 1]
- pos[i][j]);
}
}
for ( int i = 1; i <= N; i++) {
for ( int j = mx[i]; j <= N; j++) {
if (ans[j] != -1)
break ;
ans[j] = i;
}
}
return ans;
}
void print(vector< int > A, int N)
{
vector< int > ans
= Calculate_Min_array(A, N);
for ( int i = 1; i <= N; i++)
cout << ans[i] << " " ;
cout << "\n" ;
}
int main()
{
int N = 3;
vector< int > A = { 1, 2, 3 };
print(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int [] Calculate_Min_array( int [] A, int N)
{
int mx[] = new int [N + 1 ];
int ans[] = new int [N + 1 ];
int [][] pos = new int [N + 1 ][N + 1 ];
for ( int i = 0 ; i <= N; i++) {
mx[i] = - 1 ;
ans[i] = - 1 ;
}
HashMap<Integer, Integer> map = new HashMap<>();
for ( int i = 1 ; i <= N; i++) {
pos[i][ 0 ] = 0 ;
map.put(i, 1 );
}
for ( int i = 0 ; i < N; i++) {
int x = A[i];
int ind = map.get(x);
pos[x][ind] = i + 1 ;
map.put(x, ++ind);
}
for ( int i = 1 ; i <= N; i++) {
int ind = map.get(i);
pos[i][ind] = N + 1 ;
map.put(i, ++ind);
}
for ( int i = 1 ; i <= N; i++) {
for ( int j = 0 ; j < map.get(i) - 1 ; j++) {
mx[i] = Math.max(mx[i], pos[i][j + 1 ]
- pos[i][j]);
}
}
for ( int i = 1 ; i <= N; i++) {
for ( int j = mx[i]; j <= N; j++) {
if (ans[j] != - 1 )
break ;
ans[j] = i;
}
}
return ans;
}
static void print( int [] A, int N)
{
int [] ans = Calculate_Min_array(A, N);
for ( int i = 1 ; i <= N; i++)
System.out.print(ans[i] + " " );
}
public static void main(String[] args)
{
int N = 3 ;
int A[] = { 1 , 2 , 3 };
print(A, N);
}
}
|
Python3
def Calculate_Min_array(A, N):
mx, ans, pos = [ - 1 for i in range (N + 1 )] , [ - 1 for i in range (N + 1 )] , [[] for i in range (N + 1 )]
for i in range ( 1 , N + 1 ):
pos[i].append( 0 )
for i in range (N):
x = A[i]
pos[x].append(i + 1 )
for i in range ( 1 , N + 1 ):
pos[i].append(N + 1 )
for i in range ( 1 , N + 1 ):
for j in range ( len (pos[i]) - 1 ):
mx[i] = max (mx[i], pos[i][j + 1 ] - pos[i][j])
for i in range ( 1 , N + 1 ):
for j in range (mx[i], N + 1 ):
if (ans[j] ! = - 1 ):
break
ans[j] = i
return ans
def Print (A, N):
ans = Calculate_Min_array(A, N)
for i in range ( 1 , N + 1 ):
print (ans[i], end = " " )
print ("")
N = 3
A = [ 1 , 2 , 3 ]
Print (A, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int [] Calculate_Min_array( int [] A, int N)
{
int []mx = new int [N + 1];
int []ans = new int [N + 1];
int [,] pos = new int [N + 1,N + 1];
for ( int i = 0; i <= N; i++) {
mx[i] = -1;
ans[i] = -1;
}
Dictionary< int , int > map = new Dictionary< int , int >();
for ( int i = 1; i <= N; i++) {
pos[i,0] = 0;
map.Add(i, 1);
}
for ( int i = 0; i < N; i++) {
int x = A[i];
int ind = map[x];
pos[x,ind] = i + 1;
map[x] = map[x] + ind++;
}
for ( int i = 1; i <= N; i++) {
int ind = map[i];
pos[i,ind] = N + 1;
map[i] = map[i] + ind++;
}
for ( int i = 1; i <= N; i++) {
for ( int j = 0; j < map[i] - 1; j++) {
mx[i] = Math.Max(mx[i], pos[i,j + 1]
- pos[i,j]);
}
}
for ( int i = 1; i <= N; i++) {
for ( int j = mx[i]; j <= N; j++) {
if (ans[j] != -1)
break ;
ans[j] = i;
}
}
return ans;
}
static void print( int [] A, int N)
{
int [] ans = Calculate_Min_array(A, N);
for ( int i = 1; i <= N; i++)
Console.Write(ans[i] + " " );
}
public static void Main(String[] args)
{
int N = 3;
int []A = { 1, 2, 3 };
print(A, N);
}
}
|
Javascript
<script>
function Calculate_Min_array(A,N)
{
let mx = new Array(N + 1).fill(-1);
let ans = new Array(N + 1).fill(-1);
let pos = new Array(N + 1);
for (let i=0;i<N + 1;i++){
pos[i] = new Array();
}
for (let i = 1; i <= N; i++) {
pos[i].push(0);
}
for (let i = 0; i < N; i++) {
let x = A[i];
pos[x].push(i + 1);
}
for (let i = 1; i <= N; i++) {
pos[i].push(N + 1);
}
for (let i = 1; i <= N; i++) {
for (let j = 0;
j < pos[i].length - 1; j++) {
mx[i] = Math.max(mx[i],
pos[i][j + 1]
- pos[i][j]);
}
}
for (let i = 1; i <= N; i++) {
for (let j = mx[i]; j <= N; j++) {
if (ans[j] != -1)
break ;
ans[j] = i;
}
}
return ans;
}
function print(A,N)
{
let ans = Calculate_Min_array(A, N);
for (let i = 1; i <= N; i++){
document.write(ans[i], " " );
}
document.write( "</br>" );
}
let N = 3;
let A = [ 1, 2, 3 ];
print(A, N)
</script>
|
Time complexity: O(N) Because though nested loops are used but at most N points are filled and one point is visited at most twice
Auxiliary Space: O(N) Though array of vectors is used but total points stored is N
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...