Find permutation array from the cumulative sum array
Last Updated :
01 Mar, 2022
Given an array arr[] of N elements where each arr[i] is the cumulative sum of the subarray P[0…i] of another array P[] where P is the permutation of the integers from 1 to N. The task is to find the array P[], if no such P exists then print -1.
Examples:
Input: arr[] = {2, 3, 6}
Output: 2 1 3
Input: arr[] = {1, 2, 2, 4}
Output: -1
Approach:
- The first element of the cumulative array must be the first element of permutation array and the element at the ith position will be arr[i] – arr[i – 1] as arr[] is the cumulative sum array of the permutation array.
- So, find the array from the cumulative sum array and then mark the occurrence of every element from 1 to N that is present in the generated array.
- If any element is appearing more than once then the permutation is invalid else print the permutation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getPermutation( int a[], int n)
{
vector< int > ans(n);
ans[0] = a[0];
for ( int i = 1; i < n; i++)
ans[i] = a[i] - a[i - 1];
bool present[n + 1] = { false };
for ( int i = 0; i < ans.size(); i++) {
if (present[ans[i]]) {
cout << "-1" ;
return ;
}
else
present[ans[i]] = true ;
}
for ( int i = 0; i < n; i++)
cout << ans[i] << " " ;
}
int main()
{
int a[] = { 2, 3, 6 };
int n = sizeof (a) / sizeof (a[0]);
getPermutation(a, n);
return 0;
}
|
Java
class GFG
{
static void getPermutation( int a[], int n)
{
int []ans = new int [n];
ans[ 0 ] = a[ 0 ];
for ( int i = 1 ; i < n; i++)
ans[i] = a[i] - a[i - 1 ];
boolean []present = new boolean [n + 1 ];
for ( int i = 0 ; i < ans.length; i++)
{
if (present[ans[i]])
{
System.out.print( "-1" );
return ;
}
else
present[ans[i]] = true ;
}
for ( int i = 0 ; i < n; i++)
System.out.print(ans[i] + " " );
}
public static void main(String[] args)
{
int a[] = { 2 , 3 , 6 };
int n = a.length;
getPermutation(a, n);
}
}
|
Python3
def getPermutation(a, n) :
ans = [ 0 ] * n;
ans[ 0 ] = a[ 0 ];
for i in range ( 1 , n) :
ans[i] = a[i] - a[i - 1 ];
present = [ 0 ] * (n + 1 );
for i in range (n) :
if (present[ans[i]]) :
print ( "-1" , end = "");
return ;
else :
present[ans[i]] = True ;
for i in range (n) :
print (ans[i], end = " " );
if __name__ = = "__main__" :
a = [ 2 , 3 , 6 ];
n = len (a);
getPermutation(a, n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void getPermutation( int [] a, int n)
{
List< int > ans = new List< int >();
ans.Add(a[0]);
for ( int i = 1; i < n; i++)
ans.Add(a[i] - a[i - 1]);
List< int > present = new List< int >();
for ( int i = 0; i < n+1; i++)
present.Add(0);
for ( int i = 0; i < ans.Count; i++)
{
if (present[ans[i]] == 1)
{
Console.Write( "-1" );
return ;
}
else
present[ans[i]] = 1;
}
for ( int i = 0; i < n; i++)
Console.Write(ans[i] + " " );
}
static public void Main()
{
int [] a = { 2,3,6};
int n = a.Length;
getPermutation(a, n);
}
}
|
Javascript
<script>
function getPermutation(a, n)
{
var ans = Array(n);
ans[0] = a[0];
for ( var i = 1; i < n; i++)
ans[i] = a[i] - a[i - 1];
var present = Array(n+1).fill( false );
for ( var i = 0; i < ans.length; i++) {
if (present[ans[i]]) {
document.write( "-1" );
return ;
}
else
present[ans[i]] = true ;
}
for ( var i = 0; i < n; i++)
document.write( ans[i] + " " );
}
var a = [2, 3, 6];
var n = a.length;
getPermutation(a, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...