Given an array arr[] of size N, the task to check if this array is build under following constraints:
- The array can only contain numbers from 1 to N.
- We have to build the array sequentially. It means at first we place 1 then 2 and so on upto N.
- If the array is empty then we can place a number at any position.
- If it is not empty then we can place the next element at the next position of the previous element. In case if the next position is out of array size or already filled up then we can choose any position which is unoccupied and place the number.
Examples:
Input: arr[] = {2, 3, 4, 5, 1}
Output: YES
Explanation:
Initially, the array is empty. So we can place 1 at any position.
We place at position 5(1-based indexing).
As the next position is out of array size.
so we can place 2 at any position. We place it in 1st position.
Then we place 3 at 2nd position, 4 at 3rd position, and 5 at 4th position.
So we can build such an array.Input: arr[] = {1, 5, 2, 4, 3}
Output: NO
Explanation:
At first we can place 1 at 1st position. Then we have to place 2 at 2nd place
as the 2nd position is empty but 2 is placed at position 3 in that given array.
So such array is not possible to build.
Approach:
- First, Store the indices of every array elements in a map.
- Store the position of the next element in ‘next’. Initially, as the array is empty, next contains the position of 1.
- Iterate over [1, N] and check if the current element is present at next index. If not, return -1.
- In each iteration, mark the current position as visited and update the next to the possible index for next value. If the next index (i + 1) of the current index is not visited, then update next to ( i + 1). Otherwise, if the next possible index exceeds the array indices range, store the position of the next element from the map, as it can be placed at any index before the current index in the map.
- On complete traversal of the array, return true, as all indices have been places at their respective next indices.
Below is the implementation of the above approach.
C++
// C++ program to Check If we // can build the given array // under given constraints. #include <bits/stdc++.h> using namespace std; // Function return true if we // can build the array bool CheckArray( int a[], int n) { int i, f = 0, next; // First one is to keep position // of each element // Second one is for marking // the current position map< int , int > pos, vis; for (i = 0; i < n; i++) { pos[a[i]] = i; } // Initially next contains // the position of 1. next = pos[1]; for (i = 1; i <= n; i++) { // Mark the current // position. vis[next] = 1; // If the element is not // present at that position // then it is impossible // to build if (i != a[next]) { return false ; } // Updating the next if (next + 1 == n || vis[next + 1]) { // If the next position is // out of array size or next // position is not empty then // we use the map to find the // position of the next element next = pos[i + 1]; } else // Else just increment it next++; } return true ; } // Driver code int main() { int arr[] = { 2, 3, 4, 5, 1 }; int N = sizeof (arr) / sizeof (arr[0]); if (CheckArray(arr, N)) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; } |
Python3
# Python3 program to check if we # can build the given array # under given constraints. # Function return true if we # can build the array def CheckArray(a, n): f = 0 # First one is to keep position # of each element # Second one is for marking # the current position pos = {} vis = {} for i in range (n): pos[a[i]] = i # Initially next contains # the position of 1. next = pos[ 1 ] for i in range ( 1 , n + 1 ): # Mark the current # position. vis[ next ] = 1 # If the element is not # present at that position # then it is impossible # to build if (i ! = a[ next ]): return False # Updating the next if ( next + 1 = = n or ( next + 1 in vis and vis[ next + 1 ])): # If the next position is # out of array size or next # position is not empty then # we use the map to find the # position of the next element if (i + 1 in pos): next = pos[i + 1 ] else : # Else just increment it next + = 1 return True # Driver code arr = [ 2 , 3 , 4 , 5 , 1 ] N = len (arr) if (CheckArray(arr, N)): print ( 'YES' ) else : print ( 'NO' ) # This code is contributed by yatinagg |
YES
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Check if the given permutation is a valid BFS of a given Tree
- Check if all array elements can be converted to K using given operations
- Check if the given permutation is a valid DFS of graph
- Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction
- Check if the given array is same as its inverse permutation
- Check if given array can be made 0 with given operations performed any number of times
- Check if an Array is a permutation of numbers from 1 to N : Set 2
- Check if two strings are permutation of each other
- Check if any permutation of string is a K times repeated string
- Check if an Array is a permutation of numbers from 1 to N
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Maximize the Sum of the given array using given operations
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Sort decreasing permutation of N using triple swaps
- Sort permutation of N natural numbers using triple cyclic right swaps
- Check if its possible to make sum of the array odd with given Operations
- Check if all array elements can be removed by the given operations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : yatinagg