Given an array arr[] consisting of (N – 1) , the task is to construct a permutation array P[] consisting of the first N Natural Numbers such that arr[i] = (P[i +1] – P[i]) . If there exists no such permutation, then print “-1” .
Examples:
Input: arr[] = {-1, 2, -3, -1}Output: 4 3 5 2 1Explanation: For the array {4, 3, 5, 2, 1}, the adjacent difference array of consecutive elements is {4 – 3, 5 – 3, 2 – 5, 1 – 2} = {-1, 2, -3, -1} which is the same as the array arr[].
Input: arr[] = {1, 1, 1, 1}Output: 1 2 3 4 5
Approach: The given problem can be solved by considering the first element of the permutation as 0 and then constructing a new permutation array by using the given array arr[]. After this, add the minimum element of the new array to each element to make the array elements over the range [1, N] . Follow the steps below to solve the problem:
Initialize an array, say perm[] of size N to store the resultant permutation. Initialize perm[0] as 0 , and also initialize a variable, say lastEle as 0 . Iterate over the range [1, N] using the variable i, and add the value of arr[i – 1] to the element lastEle and update the value of perm[i] as lastEle .Initialize a variable, say minimumElement to the minimum element of the array perm[] . Initialize a HashSet of integers st , to store all elements of the permutation. Also, initialize a variable mx as 0 to store the maximum element in the perm[] array. Traverse through the perm[] array and add the value of (-sm) + 1 to the value perm[i] , update the value of mx as max(mx, perm[i]) and add perm[i] to st .After completing the above steps, if the value of mx and the size of HashSet st is N , then print the array perm[] as the resultant array. Otherwise, print -1 .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPermutation(int A[], int N)
{
int lasEle = 0;
int perm[N];
perm[0] = 0;
for (int i = 1; i < N; i++) {
lasEle += A[i - 1];
perm[i] = lasEle;
}
int sm = *min_element(perm, perm + N);
unordered_set<int> st;
int mx = 0;
for (int i = 0; i < N; i++) {
perm[i] += (-sm) + 1;
mx = max(mx, perm[i]);
st.insert(perm[i]);
}
if (mx == N and st.size() == N) {
for (int i = 0; i < N; i++) {
cout << perm[i] << " ";
}
}
else {
cout << -1 << " ";
}
}
int main()
{
int arr[] = { -1, 2, -3, -1 };
int N = sizeof(arr) / sizeof(arr[0]);
findPermutation(arr, N + 1);
return 0;
}
Java
import java.util.*;
class GFG{
static void findPermutation(int []A, int N)
{
int lasEle = 0;
int []perm = new int[N];
perm[0] = 0;
for (int i = 1; i < N; i++) {
lasEle += A[i - 1];
perm[i] = lasEle;
}
int sm = perm[0];
for (int i = 0; i < perm.length; i++) {
if(perm[i] <sm)
sm = perm[i];
}
Set<Integer> st = new HashSet<Integer>();
int mx = 0;
for (int i = 0; i < N; i++) {
perm[i] += (-sm) + 1;
mx = Math.max(mx, perm[i]);
st.add(perm[i]);
}
if (mx == N && st.size() == N) {
for (int i = 0; i < N; i++) {
System.out.print(perm[i]+" ");
}
}
else {
System.out.print(-1);
}
}
public static void main(String args[])
{
int arr[] = { -1, 2, -3, -1 };
int N = arr.length;
findPermutation(arr, N + 1);
}
}
Python3
def findPermutation(A, N):
lasEle = 0
perm = [0]*N
perm[0] = 0
for i in range(1,N):
lasEle += A[i - 1]
perm[i] = lasEle
sm = min(perm)
st = {}
mx = 0
for i in range(N):
perm[i] += (-sm) + 1
mx = max(mx, perm[i])
st[perm[i]] = 1
if (mx == N and len(st) == N):
for i in range(N):
print(perm[i],end=" ")
else:
print(-1,end=" ")
if __name__ == '__main__':
arr = [-1, 2, -3, -1]
N = len(arr)
findPermutation(arr, N + 1)
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static void findPermutation(int[] A, int N)
{
int lasEle = 0;
int[] perm = new int[N];
perm[0] = 0;
for (int i = 1; i < N; i++) {
lasEle += A[i - 1];
perm[i] = lasEle;
}
int sm = perm.Min();
List<int> st = new List<int>();
int mx = 0;
for (int i = 0; i < N; i++) {
perm[i] += (-sm) + 1;
mx = Math.Max(mx, perm[i]);
st.Add(perm[i]);
}
if (mx == N && st.Count == N) {
for (int i = 0; i < N; i++) {
Console.Write(perm[i] + " ");
}
}
else {
Console.Write(-1 + " ");
}
}
static void Main()
{
int[] arr= { -1, 2, -3, -1 };
int N = arr.Length;
findPermutation(arr, N + 1);
}
}
Javascript
<script>
function findPermutation(A, N) {
let lasEle = 0;
let perm = new Array(N);
perm[0] = 0;
for (let i = 1; i < N; i++) {
lasEle += A[i - 1];
perm[i] = lasEle;
}
let temp = [...perm];
let sm = temp.sort((a, b) => a - b)[0]
let st = new Set();
let mx = 0;
for (let i = 0; i < N; i++) {
perm[i] += (-sm) + 1;
mx = Math.max(mx, perm[i]);
st.add(perm[i]);
}
if (mx == N && st.size == N) {
for (let i of perm) {
document.write(i + " ")
}
}
else {
document.write(-1 + " ");
}
}
let arr = [-1, 2, -3, -1];
let N = arr.length
findPermutation(arr, N + 1);
</script>
Time Complexity: O(N)Auxiliary Space: O(N)
Last Updated :
25 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...