# Find the permutation p from the array q such that q[i] = p[i+1] – p[i]

Given an array Q[] of length N, the task is to find the permutation P[] of integers from the range [1, N + 1] such that Q[i] = P[i + 1] – P[i] for all valid i. If it is not possible then print -1.

Examples:

Input: Q[] = {-2, 1}
Output: 3 1 2
q = p – p = 1 – 3 = -2
q = p – p = 2 – 1 = 1

Input: Q[] = {1, 1, 1, 1}
Output: 1 2 3 4 5

Input: Q[] = {-1, 2, 2}
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
Let,

P = x then P = P + (P – P) = x + Q

and, P = P + (P – P) + (P – P) = x + Q + Q.

Similarly,

P[n] = x + Q + Q + Q[2 ] + ….. + Q[N – 1].

It means that the sequence p’ = 0, Q, Q + Q, ….., + Q + Q + Q + ….. + Q[N – 1] is the required permutation if we add x to each element.

To find the value of x, find an i such that p'[i] is minimum.

As, p'[i] + x is the minimum value in the series then it must be equal to 1 as series can have values from [1, N].
So x = 1 – p'[i].

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to return the minimum ` `// value of x from the given array q ` `int` `Get_Minimum(vector<``int``> q) ` `{ ` `    ``int` `minimum = 0; ` `    ``int` `sum = 0; ` `    ``for``(``int` `i = 0; i < q.size() - 1; i++) ` `    ``{ ` `        ``sum += q[i]; ` `        ``if` `(sum < minimum) ` `            ``minimum = sum; ` `    ``} ` `    ``return` `minimum; ` `} ` ` `  `// Function to return the required permutation ` `vector<``int``> Find_Permutation(vector<``int``> q, ``int` `n) ` `{ ` `    ``vector<``int``> p(n, 0); ` `    ``int` `min_value = Get_Minimum(q); ` ` `  `    ``// Set the value of p i.e. x = p ` `    ``p = 1 - min_value; ` ` `  `    ``// Iterate over array q[] ` `    ``for` `(``int` `i = 0; i < n - 1; i++) ` `        ``p[i + 1] = p[i] + q[i]; ` ` `  `    ``bool` `okay = ``true``; ` ` `  `    ``// Check if formed permutation ` `    ``// is correct or not ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``if` `(p[i] < 1 or p[i] > n) ` `            ``okay = ``false``; ` `        ``set<``int``> w(p.begin(), p.end()); ` `        ``if` `(w.size() != n) ` `            ``okay = ``false``; ` `    ``} ` ` `  `    ``// Return the permutation p ` `    ``if` `(okay) ` `        ``return` `p; ` `    ``else` `        ``return` `{-1}; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector<``int``> q = {-2, 1}; ` `    ``int` `n = q.size() + 1; ` `    ``cout << ``"[ "``; ` `    ``for` `(``int` `i:Find_Permutation(q, n)) ` `        ``cout << i << ``" "``; ` `    ``cout << ``"]"``;      ` `} ` ` `  `// This code is contributed by Mohit Kumar `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to return the minimum ` `// value of x from the given array q ` `static` `int` `Get_Minimum(``int` `[] q) ` `{ ` `    ``int` `minimum = ``0``; ` `    ``int` `sum = ``0``; ` `    ``for``(``int` `i = ``0``; i < q.length - ``1``; i++) ` `    ``{ ` `        ``sum += q[i]; ` `        ``if` `(sum < minimum) ` `            ``minimum = sum; ` `    ``} ` `    ``return` `minimum; ` `} ` ` `  `// Function to return the required permutation ` `static` `int` `[] Find_Permutation(``int` `[] q, ``int` `n) ` `{ ` `    ``int` `[] p = ``new` `int``[n]; ` `    ``int` `min_value = Get_Minimum(q); ` ` `  `    ``// Set the value of p i.e. x = p ` `    ``p[``0``] = ``1` `- min_value; ` ` `  `    ``// Iterate over array q[] ` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++) ` `        ``p[i + ``1``] = p[i] + q[i]; ` ` `  `    ``boolean` `okay = ``true``; ` ` `  `    ``// Check if formed permutation ` `    ``// is correct or not ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``if` `(p[i] < ``1` `|| p[i] > n) ` `            ``okay = ``false``; ` `        ``Set w = ``new` `HashSet<>(); ` `        ``if` `(w.size() != n) ` `            ``okay = ``true``; ` `    ``} ` ` `  `    ``// Return the permutation p ` `    ``if` `(okay) ` `        ``return` `p; ` `    ``else` `        ``return` `new` `int` `[]{-``1``}; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[])  ` `{ ` `    ``int` `[]q = {-``2``, ``1``}; ` `    ``int` `n = q.length + ``1``; ` `    ``System.out.print(``"[ "``); ` `    ``for` `(``int` `i:Find_Permutation(q, n)) ` `        ``System.out.print(i + ``" "``); ` `    ``System.out.print(``"]"``); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the minimum  ` `# value of x from the given array q ` `def` `Get_Minimum(q): ` `    ``minimum ``=` `0` `    ``sum` `=` `0` `    ``for` `i ``in` `range``(n ``-` `1``): ` `        ``sum` `+``=` `q[i] ` `        ``if` `sum` `< minimum: ` `            ``minimum ``=` `sum` `    ``return` `minimum ` ` `  `# Function to return the  ` `# required permutation ` `def` `Find_Permutation(q): ` `    ``p ``=` `[``0``] ``*` `n ` `    ``min_value ``=` `Get_Minimum(q) ` ` `  `    ``# Set the value of p  ` `    ``# i.e. x = p ` `    ``p[``0``]``=` `1` `-` `min_value ` ` `  `    ``# Iterate over array q[] ` `    ``for` `i ``in` `range``(n ``-` `1``): ` `        ``p[i ``+` `1``] ``=` `p[i] ``+` `q[i] ` ` `  `    ``okay ``=` `True` ` `  `    ``# Check if formed permutation  ` `    ``# is correct or not ` `    ``for` `i ``in` `range``(n): ` `        ``if` `p[i] < ``1` `or` `p[i] > n: ` `            ``okay ``=` `False` `    ``if` `len``(``set``(p)) !``=` `n: ` `        ``okay ``=` `False` ` `  `    ``# Return the permutation p ` `    ``if` `okay: ` `        ``return` `p ` `    ``else``: ` `        ``return` `-``1` ` `  `# Driver code ` `if` `__name__``=``=``"__main__"``: ` `    ``q ``=` `[``-``2``, ``1``] ` `    ``n ``=` `len``(q) ``+` `1` `    ``print``(Find_Permutation(q)) `

## C#

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to return the minimum ` `// value of x from the given array q ` `static` `int` `Get_Minimum(``int` `[] q) ` `{ ` `    ``int` `minimum = 0; ` `    ``int` `sum = 0; ` `    ``for``(``int` `i = 0; i < q.Length - 1; i++) ` `    ``{ ` `        ``sum += q[i]; ` `        ``if` `(sum < minimum) ` `            ``minimum = sum; ` `    ``} ` `    ``return` `minimum; ` `} ` ` `  `// Function to return the required permutation ` `static` `int` `[] Find_Permutation(``int` `[] q, ``int` `n) ` `{ ` `    ``int` `[] p = ``new` `int``[n]; ` `    ``int` `min_value = Get_Minimum(q); ` ` `  `    ``// Set the value of p i.e. x = p ` `    ``p = 1 - min_value; ` ` `  `    ``// Iterate over array q[] ` `    ``for` `(``int` `i = 0; i < n - 1; i++) ` `        ``p[i + 1] = p[i] + q[i]; ` ` `  `    ``bool` `okay = ``true``; ` ` `  `    ``// Check if formed permutation ` `    ``// is correct or not ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``if` `(p[i] < 1 || p[i] > n) ` `            ``okay = ``false``; ` `        ``HashSet<``int``> w = ``new` `HashSet<``int``>(); ` `        ``if` `(w.Count != n) ` `            ``okay = ``true``; ` `    ``} ` ` `  `    ``// Return the permutation p ` `    ``if` `(okay) ` `        ``return` `p; ` `    ``else` `        ``return` `new` `int` `[]{-1}; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args)  ` `{ ` `    ``int` `[]q = {-2, 1}; ` `    ``int` `n = q.Length + 1; ` `    ``Console.Write(``"[ "``); ` `    ``foreach` `(``int` `i ``in` `Find_Permutation(q, n)) ` `        ``Console.Write(i + ``" "``); ` `    ``Console.Write(``"]"``); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```[3, 1, 2]
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.