Check for each subarray whether it consists of all natural numbers up to its length or not
Last Updated :
28 Nov, 2021
Given an array, arr[] representing a permutation of first N natural numbers in the range [1, N], the task for each ith index is to check if an i-length subarray exists or not such that it contains all the numbers in the range [1, i].
Note: 1 – based indexing used.
Examples:
Input: arr[] = {4, 5, 1, 3, 2}
Output: True False True False True
Explanation:
For i = 1, the subarray {arr[2]} contains all the numbers from [1, i] and of size i(=1). Therefore, the required output is true.
For i = 2, no subarray of size 2 exists which contains all the numbers in the range[1, i]. Therefore, the required output is false.
For i = 3, the subarray {arr[2], arr[3], arr[4]} contains all the numbers from [1, i] and of size i(=3). Therefore, the required output is true.
For i = 4, no subarray of size 4 exists which contains all the numbers in the range[1, i]. Therefore, the required output is false.
For i = 5, the subarray {arr[0], arr[1], arr[2], arr[3], arr[4]} contains all the numbers from [1, i] and of size i(=5). Therefore, the required output is true.
Input: arr = {1, 4, 3, 2}
Output: True False False True
Naive Approach: The idea is to traverse the array and for each index, check if there is a subarray of size i which contains all the numbers in the range [1, i] or not. If found to be true, then print True. Otherwise, print False.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The problem can be solved using Hashing to store the position of each element of the given array efficiently. Follow the steps below to solve the problem:
- Create a map, say, Map, to store the position of each element of the given array.
- Traverse the array and store the position of each element of the array onto Map.
- Create a set, say st to store the indexes of each element from the range [1, N].
- Initialize two variables, say Min and Max, to store the smallest and the largest elements present in st.
- Iterate over the range [1, N] and insert the value of Map[i] into st and check if Max – Min + 1 = i or not. If found to be true, then print True.
- Otherwise, print False.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checksubarrayExist1_N( int arr[], int N)
{
unordered_map< int , int > pos;
for ( int i = 0; i < N; i++) {
pos[arr[i]] = i;
}
set< int > st;
for ( int i = 1; i <= N; i++) {
st.insert(pos[i]);
int Min = *(st.begin());
int Max = *(st.rbegin());
if (Max - Min + 1 == i) {
cout << "True " ;
}
else {
cout << "False " ;
}
}
}
int main()
{
int arr[] = { 1, 4, 3, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
checksubarrayExist1_N(arr, N);
}
|
Java
import java.util.*;
class GFG {
static void checksubarrayExist1_N( int arr[], int N)
{
Map<Integer, Integer> pos= new HashMap<>();
for ( int i = 0 ; i < N; i++) {
pos.put(arr[i],i);
}
Set<Integer> st= new HashSet<>();
for ( int i = 1 ; i <= N; i++) {
st.add(pos.get(i));
int Min = Collections.min(st);
int Max = Collections.max(st);
if (Max - Min + 1 == i) {
System.out.print( "True " );
}
else {
System.out.print( "False " );
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 4 , 3 , 2 };
int N = arr.length;
checksubarrayExist1_N(arr, N);
}
}
|
Python3
def checksubarrayExist1_N(arr, N):
pos = {}
for i in range (N):
pos[arr[i]] = i
st = {}
for i in range ( 1 , N + 1 ):
st[pos[i]] = 1
Min = sorted ( list (st.keys()))[ 0 ]
Max = sorted ( list (st.keys()))[ - 1 ]
if ( Max - Min + 1 = = i):
print ( "True" , end = " " )
else :
print ( "False" , end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 4 , 3 , 2 ]
N = len (arr)
checksubarrayExist1_N(arr, N)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
static void checksubarrayExist1_N( int [] arr, int N)
{
Dictionary< int ,
int > pos = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
pos[arr[i]] = i;
}
HashSet< int > st = new HashSet< int >();
for ( int i = 1; i <= N; i++)
{
st.Add(pos[i]);
int Min = st.Min();
int Max = st.Max();
if (Max - Min + 1 == i)
{
Console.Write( "True " );
}
else
{
Console.Write( "False " );
}
}
}
public static void Main( string [] args)
{
int [] arr = { 1, 4, 3, 2 };
int N = arr.Length;
checksubarrayExist1_N(arr, N);
}
}
|
Javascript
<script>
function checksubarrayExist1_N(arr, N)
{
let pos = new Map();
for (let i = 0; i < N; i++) {
pos.set(arr[i],i);
}
let st= new Set();
for (let i = 1; i <= N; i++) {
st.add(pos.get(i));
let Min = Math.min(...st);
let Max = Math.max(...st);
if (Max - Min + 1 == i) {
document.write( "True " );
}
else {
document.write( "False " );
}
}
}
let arr = [ 1, 4, 3, 2 ];
let N = arr.length;
checksubarrayExist1_N(arr, N);
</script>
|
Output:
True False False True
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...