Restore a permutation from the given helper array
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++
#include <bits/stdc++.h>
using namespace std;
void findPerm( int Q[], int n)
{
int minval = 0, qsum = 0;
for ( int i = 0; i < n - 1; i++) {
qsum += Q[i];
if (qsum < minval)
minval = qsum;
}
vector< int > P(n);
P[0] = 1 - minval;
bool permFound = true ;
for ( int i = 0; i < n - 1; i++) {
P[i + 1] = P[i] + Q[i];
if (P[i + 1] > n || P[i + 1] < 1) {
permFound = false ;
break ;
}
}
if (permFound) {
for ( int i = 0; i < n; i++) {
cout << P[i] << " " ;
}
}
else {
cout << -1;
}
}
int main()
{
int Q[] = { -2, 1 };
int n = 1 + ( sizeof (Q) / sizeof ( int ));
findPerm(Q, n);
return 0;
}
|
Java
class GFG
{
static void findPerm( int Q[], int n)
{
int minval = 0 , qsum = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
qsum += Q[i];
if (qsum < minval)
minval = qsum;
}
int []P = new int [n];
P[ 0 ] = 1 - minval;
boolean permFound = true ;
for ( int i = 0 ; i < n - 1 ; i++)
{
P[i + 1 ] = P[i] + Q[i];
if (P[i + 1 ] > n || P[i + 1 ] < 1 )
{
permFound = false ;
break ;
}
}
if (permFound)
{
for ( int i = 0 ; i < n; i++)
{
System.out.print(P[i]+ " " );
}
}
else
{
System.out.print(- 1 );
}
}
public static void main(String[] args)
{
int Q[] = { - 2 , 1 };
int n = 1 + Q.length;
findPerm(Q, n);
}
}
|
Python3
def findPerm(Q, n) :
minval = 0 ; qsum = 0 ;
for i in range (n - 1 ) :
qsum + = Q[i];
if (qsum < minval) :
minval = qsum;
P = [ 0 ] * n;
P[ 0 ] = 1 - minval;
permFound = True ;
for i in range (n - 1 ) :
P[i + 1 ] = P[i] + Q[i];
if (P[i + 1 ] > n or P[i + 1 ] < 1 ) :
permFound = False ;
break ;
if (permFound) :
for i in range (n) :
print (P[i],end = " " );
else :
print ( - 1 );
if __name__ = = "__main__" :
Q = [ - 2 , 1 ];
n = 1 + len (Q) ;
findPerm(Q, n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findPerm( int []Q, int n)
{
int minval = 0, qsum = 0;
for ( int i = 0; i < n - 1; i++)
{
qsum += Q[i];
if (qsum < minval)
minval = qsum;
}
int []P = new int [n];
P[0] = 1 - minval;
bool permFound = true ;
for ( int i = 0; i < n - 1; i++)
{
P[i + 1] = P[i] + Q[i];
if (P[i + 1] > n || P[i + 1] < 1)
{
permFound = false ;
break ;
}
}
if (permFound)
{
for ( int i = 0; i < n; i++)
{
Console.Write(P[i]+ " " );
}
}
else
{
Console.Write(-1);
}
}
public static void Main(String[] args)
{
int []Q = { -2, 1 };
int n = 1 + Q.Length;
findPerm(Q, n);
}
}
|
Javascript
<script>
function findPerm(Q, n)
{
var minval = 0, qsum = 0;
for ( var i = 0; i < n - 1; i++)
{
qsum += Q[i];
if (qsum < minval)
minval = qsum;
}
var P = Array(n);
P[0] = 1 - minval;
var permFound = true ;
for ( var i = 0; i < n - 1; i++)
{
P[i + 1] = P[i] + Q[i];
if (P[i + 1] > n || P[i + 1] < 1)
{
permFound = false ;
break ;
}
}
if (permFound)
{
for ( var i = 0; i < n; i++)
{
document.write( P[i] + " " );
}
}
else
{
document.write( -1);
}
}
var Q = [ -2, 1 ];
var n = 1 + Q.length;
findPerm(Q, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
01 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...