The Wayback Machine - https://web.archive.org/web/20210324122917/https://www.geeksforgeeks.org/permutation-of-first-n-natural-numbers-having-given-array-as-the-prefix-maximum-array/
Skip to content
Related Articles

Related Articles

Permutation of first N natural numbers having given array as the prefix maximum array
  • Last Updated : 23 Mar, 2021

Given an array arr[] consisting of N positive integers, the task is to find the permutation of first N natural numbers such that the given array arr[] is the prefix maximum array of that permutation. If no such permutation exists, then print “-1”.

Examples:

Input: arr[] = {1, 3, 4, 5, 5}
Output: 1 3 4 5 2
Explanation:
The prefix maximum array of the permutation {1, 3, 4, 5, 2} is {1, 3, 4, 5, 5}.

Input: arr[] = {1, 1, 3, 4}
Output: -1

Naive Approach: The simplest approach to solve the given problem is to generate all possible permutations of the first N natural numbers and check if there exists any permutation whose prefix maximum array is the array arr[] or not. If any such permutation is found, then print that permutation. Otherwise, print “-1”.
Time Complexity: O(N * N!)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the observation that the first occurrence of every number in the arr[] will be at the same place as that in the required permutation. Therefore, after filling the first occurrence of all the elements at their correct positions, fill the remaining numbers in ascending order. Follow the steps below to solve the problem:

  • Initialize an array ans[] of size N with all elements as 0 and a vector V[] to store all the unvisited elements in the array.
  • Initialize a HashMap M to store the index of the first occurrence of elements
  • Traverse the array arr[] and if arr[i] is not present in M, then store the index of arr[i] in M and update ans[i] to arr[i].
  • Iterate over the range [1, N] using the variable i and check if i is not present in M, store it in the vector V[].
  • Traverse the array ans[] and if the value of ans[i] is 0, then update ans[i] to V[j] and increment j by 1.
  • After completing the above steps, check if the maximum element prefix array is the same as arr[]. If found to be true, then print the array ans[] as the result. Otherwise, print “-1”.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if prefix maximum
// array of ans[] is equal to arr[] or not
bool checkPermutation(
    int ans[], int a[], int n)
{
    // Stores the maximum
    // encountered so far
    int Max = INT_MIN;
  
    // Traverse the array ans[]
    for (int i = 0; i < n; i++) {
  
        // Store the maximum
        // value up to i-th index
        Max = max(Max, ans[i]);
  
        if (Max != a[i])
            return false;
    }
  
    // Otherwise, return false
    return true;
}
  
// Function to find the permutation
// whose prefix maximum array is
// same as the given array a[]
void findPermutation(int a[], int n)
{
    // Stores the required permutation
    int ans[n] = { 0 };
  
    // Stores the index of first
    // occurrence of elements
    unordered_map<int, int> um;
  
    // Traverse the array a[]
    for (int i = 0; i < n; i++) {
  
        // If a[i] is not present
        // in um, then store it in um
        if (um.find(a[i]) == um.end()) {
  
            // Update the ans[i] to a[i]
            ans[i] = a[i];
            um[a[i]] = i;
        }
    }
  
    // Stores the unvisited numbers
    vector<int> v;
    int j = 0;
  
    // Fill the array v[]
    for (int i = 1; i <= n; i++) {
  
        // Store the index
        if (um.find(i) == um.end()) {
            v.push_back(i);
        }
    }
  
    // Travere the array ans[]
    for (int i = 0; i < n; i++) {
  
        // Fill v[j] at indices
        // where ans[i] is 0
        if (ans[i] == 0) {
  
            ans[i] = v[j];
            j++;
        }
    }
  
    // If the current permutation is
    // same as the maximum prefix
    // array of the given array a[]
    if (checkPermutation(ans, a, n)) {
  
        // If found to be true, then
        // print the permutation
        for (int i = 0; i < n; i++) {
            cout << ans[i] << " ";
        }
    }
  
    // Otherwise, print -1
    else
        cout << "-1";
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 3, 4, 5, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    findPermutation(arr, N);
  
    return 0;
}
Output:
1 3 4 5 2

Time Complexity: O(N)
Auxiliary Space: O(N)




My Personal Notes arrow_drop_up
Recommended Articles
Page :