Restore a permutation from the given helper array
Last Updated :
01 Mar, 2022
Given an array Q[] of size N - 1 such that each Q[i] = P[i + 1] - P[i] where P[] is the permutation of the first N natural numbers, the task is to find this permutation. If no valid permutation P[] can be found then print -1.
Examples:
Input: Q[] = {-2, 1}
Output: 3 1 2
Input: Q[] = {1, 1, 1, 1}
Output: 1 2 3 4 5
Approach: This is a mathematical algorithmic question. Lets denote P[i] = x. Therefore P[i + 1] = P[i] + (P[i + 1] - P[i]) = x + Q[i] (Since Q[i] = P[i + 1] - P[i]).
Therefore, P[i + 2]= P[i] + (P[i + 1] - P[i]) + (P[i + 2] - P[i + 1]) = x + Q[i] + Q[i + 1]. Observe, the pattern forming here. P is nothing but [x, x + Q[1], x + Q[1] + Q[2] + ... + x + Q[1] + Q[2] + ... + Q[n - 1]] where x = P[i] which is still unknown.
Lets have a permutation P' where P'[i] = P[i] - x. Therefore, P' = [0, Q[1], Q[1] + Q[2], Q[1] + Q[2] + Q[3], ..., Q[1] + Q[2] + ... + Q[n - 1]].
To find x, lets find the smallest element in P'. Let it be P'[k]. Therefore, x = 1 - P'[k]. This is because, the original permutation P has integers from 1 to n and so 1 can be the minimum element in P. After finding x, add x to each P' to get the original permutation P.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the required permutation
void findPerm(int Q[], int n)
{
int minval = 0, qsum = 0;
for (int i = 0; i < n - 1; i++) {
// Each element in P' is like a
// cumulative sum in Q
qsum += Q[i];
// minval is the minimum
// value in P'
if (qsum < minval)
minval = qsum;
}
vector<int> P(n);
P[0] = 1 - minval;
// To check if each entry in P
// is from the range [1, n]
bool permFound = true;
for (int i = 0; i < n - 1; i++) {
P[i + 1] = P[i] + Q[i];
// Invalid permutation
if (P[i + 1] > n || P[i + 1] < 1) {
permFound = false;
break;
}
}
// If a valid permutation exists
if (permFound) {
// Print the permutation
for (int i = 0; i < n; i++) {
cout << P[i] << " ";
}
}
else {
// No valid permutation
cout << -1;
}
}
// Driver code
int main()
{
int Q[] = { -2, 1 };
int n = 1 + (sizeof(Q) / sizeof(int));
findPerm(Q, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to find the required permutation
static void findPerm(int Q[], int n)
{
int minval = 0, qsum = 0;
for (int i = 0; i < n - 1; i++)
{
// Each element in P' is like a
// cumulative sum in Q
qsum += Q[i];
// minval is the minimum
// value in P'
if (qsum < minval)
minval = qsum;
}
int []P = new int[n];
P[0] = 1 - minval;
// To check if each entry in P
// is from the range [1, n]
boolean permFound = true;
for (int i = 0; i < n - 1; i++)
{
P[i + 1] = P[i] + Q[i];
// Invalid permutation
if (P[i + 1] > n || P[i + 1] < 1)
{
permFound = false;
break;
}
}
// If a valid permutation exists
if (permFound)
{
// Print the permutation
for (int i = 0; i < n; i++)
{
System.out.print(P[i]+ " ");
}
}
else
{
// No valid permutation
System.out.print(-1);
}
}
// Driver code
public static void main(String[] args)
{
int Q[] = { -2, 1 };
int n = 1 + Q.length;
findPerm(Q, n);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to find the required permutation
def findPerm(Q, n) :
minval = 0; qsum = 0;
for i in range(n - 1) :
# Each element in P' is like a
# cumulative sum in Q
qsum += Q[i];
# minval is the minimum
# value in P'
if (qsum < minval) :
minval = qsum;
P = [0]*n;
P[0] = 1 - minval;
# To check if each entry in P
# is from the range [1, n]
permFound = True;
for i in range(n - 1) :
P[i + 1] = P[i] + Q[i];
# Invalid permutation
if (P[i + 1] > n or P[i + 1] < 1) :
permFound = False;
break;
# If a valid permutation exists
if (permFound) :
# Print the permutation
for i in range(n) :
print(P[i],end=" ");
else :
# No valid permutation
print(-1);
# Driver code
if __name__ == "__main__" :
Q = [ -2, 1 ];
n = 1 + len(Q) ;
findPerm(Q, n);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the required permutation
static void findPerm(int []Q, int n)
{
int minval = 0, qsum = 0;
for (int i = 0; i < n - 1; i++)
{
// Each element in P' is like a
// cumulative sum in Q
qsum += Q[i];
// minval is the minimum
// value in P'
if (qsum < minval)
minval = qsum;
}
int []P = new int[n];
P[0] = 1 - minval;
// To check if each entry in P
// is from the range [1, n]
bool permFound = true;
for (int i = 0; i < n - 1; i++)
{
P[i + 1] = P[i] + Q[i];
// Invalid permutation
if (P[i + 1] > n || P[i + 1] < 1)
{
permFound = false;
break;
}
}
// If a valid permutation exists
if (permFound)
{
// Print the permutation
for (int i = 0; i < n; i++)
{
Console.Write(P[i]+ " ");
}
}
else
{
// No valid permutation
Console.Write(-1);
}
}
// Driver code
public static void Main(String[] args)
{
int []Q = { -2, 1 };
int n = 1 + Q.Length;
findPerm(Q, n);
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// Javascript implementation of the approach
// Function to find the required permutation
function findPerm(Q, n)
{
var minval = 0, qsum = 0;
for(var i = 0; i < n - 1; i++)
{
// Each element in P' is like a
// cumulative sum in Q
qsum += Q[i];
// minval is the minimum
// value in P'
if (qsum < minval)
minval = qsum;
}
var P = Array(n);
P[0] = 1 - minval;
// To check if each entry in P
// is from the range [1, n]
var permFound = true;
for(var i = 0; i < n - 1; i++)
{
P[i + 1] = P[i] + Q[i];
// Invalid permutation
if (P[i + 1] > n || P[i + 1] < 1)
{
permFound = false;
break;
}
}
// If a valid permutation exists
if (permFound)
{
// Print the permutation
for(var i = 0; i < n; i++)
{
document.write( P[i] + " ");
}
}
else
{
// No valid permutation
document.write( -1);
}
}
// Driver code
var Q = [ -2, 1 ];
var n = 1 + Q.length;
findPerm(Q, n);
// This code is contributed by famously
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Apply Given Permutation on Array K times Given two arrays arr[] and P[] of length N and an integer K. The task is to find the final array if permutation P[] is applied on given array arr[] for K times. If we apply a permutation P[] on array arr[] we get a new array arr', such that arr'[i] = arr[P[i]].Examples: Input: N = 4, K = 2, P = {2,
6 min read
Change the array into a permutation of numbers from 1 to n Given an array A of n elements. We need to change the array into a permutation of numbers from 1 to n using minimum replacements in the array. Examples: Input : A[] = {2, 2, 3, 3} Output : 2 1 3 4 Explanation: To make it a permutation of 1 to 4, 1 and 4 are missing from the array. So replace 2, 3 wi
5 min read
Minimize operations to make Array a permutation Given an array arr of n integers. You want to make this array a permutation of integers 1 to n. In one operation you can choose two integers i (0 ⤠i < n) and x (x > 0), then replace arr[i] with arr[i] mod x, the task is to determine the minimum number of operations to achieve this goal otherw
7 min read
Different Ways to Generate Permutations of an Array Permutations are like the magic wand of combinatorics, allowing us to explore the countless ways elements can be rearranged within an array. Whether you're a coder, a math enthusiast, or someone on a quest to solve a complex problem, understanding how to generate all permutations of an array is a va
6 min read
What is a Permutation Array in DSA? A permutation array, often called a permutation or permuted array, is an arrangement of elements from a source array in a specific order different from their original placement. The critical characteristic of a permutation array is that it contains all the elements from the source array but in a dif
7 min read