Check for each subarray whether it consists of all natural numbers up to its length or not | C # program to implement the above approach ; Function to check if a subarray of size i exists that contain all the numbers in the range [ 1 , i ] ; Store the position of each element of arr [ ] ; Traverse the array ; Insert the position of arr [ i ] ; Store position of each element from the range [ 1 , N ] ; Iterate over the range [ 1 , N ] ; Insert the index of i into st ; Find the smallest element of st ; Find the largest element of st ; If distance between the largest and smallest element of arr [ ] till i - th index is equal to i ; Driver code
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 ) ; } }