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 2Input: 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++ 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 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 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# 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 |
<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> |
3 1 2
Time Complexity: O(n)
Auxiliary Space: O(n)