# 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[0] = p[1] – p[0] = 1 – 3 = -2

q[1] = p[2] – p[1] = 2 – 1 = 1

Input:Q[] = {1, 1, 1, 1}

Output:1 2 3 4 5

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

Output:-1

**Approach:**

Let,

P[0] = xthenP[1] = P[0] + (P[1] – P[0]) = x + Q[0]and,

P[2] = P[0] + (P[1] – P[0]) + (P[2] – P[1]) = x + Q[0] + Q[1].

Similarly,

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

It means that the sequence **p’ = 0, Q[1], Q[1] + Q[2], ….., + Q[1] + Q[2] + Q[3] + ….. + 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:

`# 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[0] ` ` ` `# i.e. x = p[0] ` ` ` `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)) ` |

*chevron_right*

*filter_none*

**Output:**

[3, 1, 2]

## Recommended Posts:

- Permutation of an array that has smaller values from another array
- Minimum number of given operations required to convert a permutation into an identity permutation
- Change the array into a permutation of numbers from 1 to n
- How to find Lexicographically previous permutation?
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Find the good permutation of first N natural numbers
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Find minimum value to assign all array elements so that array product becomes greater
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Find pairs in array whose sums already exist in array
- Find the Initial Array from given array after range sum queries

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.