Find permutation array from the cumulative sum array Last Updated : 01 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 3Input: 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++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find the valid permutation void getPermutation(int a[], int n) { // Find the array from the cumulative sum vector<int> ans(n); ans[0] = a[0]; for (int i = 1; i < n; i++) ans[i] = a[i] - a[i - 1]; // To mark the occurrence of an element bool present[n + 1] = { false }; for (int i = 0; i < ans.size(); i++) { // If current element has already // been seen previously if (present[ans[i]]) { cout << "-1"; return; } // Mark the current element's occurrence else present[ans[i]] = true; } // Print the required permutation for (int i = 0; i < n; i++) cout << ans[i] << " "; } // Driver code int main() { int a[] = { 2, 3, 6 }; int n = sizeof(a) / sizeof(a[0]); getPermutation(a, n); return 0; } Java // Java implementation of the approach class GFG { // Function to find the valid permutation static void getPermutation(int a[], int n) { // Find the array from the cumulative sum int []ans = new int[n]; ans[0] = a[0]; for (int i = 1; i < n; i++) ans[i] = a[i] - a[i - 1]; // To mark the occurrence of an element boolean []present = new boolean[n + 1]; for (int i = 0; i < ans.length; i++) { // If current element has already // been seen previously if (present[ans[i]]) { System.out.print("-1"); return; } // Mark the current element's occurrence else present[ans[i]] = true; } // Print the required permutation for (int i = 0; i < n; i++) System.out.print(ans[i] + " "); } // Driver code public static void main(String[] args) { int a[] = { 2, 3, 6 }; int n = a.length; getPermutation(a, n); } } // This code is contributed by Rajput-Ji Python3 # Python3 implementation of the approach # Function to find the valid permutation def getPermutation(a, n) : # Find the array from the cumulative sum ans = [0] * n; ans[0] = a[0]; for i in range(1, n) : ans[i] = a[i] - a[i - 1]; # To mark the occurrence of an element present = [0] * (n + 1); for i in range(n) : # If current element has already # been seen previously if (present[ans[i]]) : print("-1", end = ""); return; # Mark the current element's occurrence else : present[ans[i]] = True; # Print the required permutation for i in range(n) : print(ans[i], end = " "); # Driver code if __name__ == "__main__" : a = [ 2, 3, 6 ]; n = len(a); getPermutation(a, n); # This code is contributed by AnkitRai01 C# // C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Function to find the valid permutation static void getPermutation(int [] a, int n) { // Find the array from the cumulative sum List<int> ans = new List<int>(); ans.Add(a[0]); for (int i = 1; i < n; i++) ans.Add(a[i] - a[i - 1]); // To mark the occurrence of an element 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 current element has already // been seen previously if (present[ans[i]] == 1) { Console.Write("-1"); return; } // Mark the current element's occurrence else present[ans[i]] = 1; } // Print the required permutation for (int i = 0; i < n; i++) Console.Write(ans[i] + " "); } // Driver code static public void Main() { int[] a = { 2,3,6}; int n = a.Length; getPermutation(a, n); } } // This code is contributed by mohit kumar 29 JavaScript <script> // Javascript implementation of the approach // Function to find the valid permutation function getPermutation(a, n) { // Find the array from the cumulative sum var ans = Array(n); ans[0] = a[0]; for (var i = 1; i < n; i++) ans[i] = a[i] - a[i - 1]; // To mark the occurrence of an element var present = Array(n+1).fill(false); for (var i = 0; i < ans.length; i++) { // If current element has already // been seen previously if (present[ans[i]]) { document.write( "-1"); return; } // Mark the current element's occurrence else present[ans[i]] = true; } // Print the required permutation for (var i = 0; i < n; i++) document.write( ans[i] + " "); } // Driver code var a = [2, 3, 6]; var n = a.length; getPermutation(a, n); // This code is contributed by rrrtnx. </script> Output: 2 1 3 Time Complexity: O(n) Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Calculate sum of the array generated by given operations S souradeep Follow Improve Article Tags : Greedy Mathematical DSA Arrays permutation +1 More Practice Tags : ArraysGreedyMathematicalpermutation Similar Reads Calculate sum of the array generated by given operations Given an array arr[] consisting of N strings, the task is to find the total sum of the array brr[] (initially empty) constructed by performing following operations while traversing the given array arr[]: If the array arr[] contains an integer, then insert that integer into the array brr[].If the arr 8 min read Count of permutations of an Array having maximum MEXs sum of prefix arrays Given an array arr of size N, the task is to find the number of its permutations such that the sum of MEXs of its prefix arrays is maximum. Note: MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. Example: Input: arr[] = {1, 0, 1}Output: 2Expla 10 min read Construct an array from its pair-sum array Given a pair-sum array arr[], construct the original array res[] where each element in arr represents the sum of a unique pair from res in a specific order, starting with res[0] + res[1], then res[0] + res[2], and so on through all combinations where the first index is less than the second. Note: Th 5 min read Count of subarrays which forms a permutation from given Array elements Given an array A[] consisting of integers [1, N], the task is to count the total number of subarrays of all possible lengths x (1 ? x ? N), consisting of a permutation of integers [1, x] from the given array. Examples: Input: A[] = {3, 1, 2, 5, 4} Output: 4 Explanation: Subarrays forming a permutati 6 min read Print modified array after multiple array range increment operations Given an array containing n integers and a value d. m queries are given. Each query has two values start and end. For each query, the problem is to increment the values from the start to end index in the given array by the given value d. A linear time-efficient solution is required for handling such 10 min read Find the indices which will hold the Array sum after given operations Given an array arr[], the task is to print the indices that have the highest probability of holding the entire sum of the array after performing the given operations: Choose any two elements say arr[i] and arr[j], store their sum in a variable KAssign K at index of max(arr[i], arr[j]) and assign 0 a 10 min read Like