Given an integer N and an arr1[], of (N – 1) integers, the task is to find the sequence arr2[] of N integers in the range [1, N] such that arr1[i] = arr2[i+1] – arr2[i]. The integers in sequence arr1[] lies in range [-N, N].
Examples:
Input: N = 3, arr1[] = {-2, 1}
Output: arr2[] = {3, 1, 2}
Explanation:
arr2[1] – arr2[0] = (1 – 3) = -2 = arr1[0]
arr2[2] – arr2[1] = (2 – 1) = 1 = arr1[1]Input: N = 5, arr1 = {1, 1, 1, 1, 1}
Output: arr2 = {1, 2, 3, 4, 5}
Explanation:
arr2[1] – arr2[0] = (2 – 1) = 1 = arr1[0]
arr2[2] – arr2[1] = (3 – 2) = 1 = arr1[1]
arr2[3] – arr2[2] = (4 – 3) = 1 = arr1[2]
arr2[4] – arr2[3] = (5 – 4) = 1 = arr1[3]
Approach:
Follow the steps to solve the problem:
- Assume the first element of arr2[] to be X.
- The next element will be X + arr1[0].
- The rest of the elements of arr2[] can be represented, w.r.t X.
- It is known that the sequence arr2[] can contain integers in the range [1, N]. So the minimum possible integer would be 1.
- The minimum number of the arr2[] can be found out in terms of X, and equate it with 1 to find the value of X.
- Finally using the values of X, all the other numbers in arr2[] can be found out.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the sequence void find_seq(int arr[], int m, int n) { int b[n]; int x = 0; // initializing 1st element b[0] = x; // Creating sequence in // terms of x for (int i = 0; i < n - 1; i++) { b[i + 1] = x + arr[i] + b[i]; } int mn = n; // Finding min element for (int i = 0; i < n; i++) { mn = min(mn, b[i]); } // Finding value of x x = 1 - mn; // Creating original sequence for (int i = 0; i < n; i++) { b[i] += x; } // Output original sequence for (int i = 0; i < n; i++) { cout << b[i] << " "; } cout << endl; } // Driver function int main() { int N = 3; int arr[] = { -2, 1 }; int M = sizeof(arr) / sizeof(int); find_seq(arr, M, N); return 0; } |
Java
// Java implementation of the above approach class GFG{ // Function to find the sequence static void find_seq(int arr[], int m, int n) { int b[] = new int[n]; int x = 0; // Initializing 1st element b[0] = x; // Creating sequence in // terms of x for(int i = 0; i < n - 1; i++) { b[i + 1] = x + arr[i] + b[i]; } int mn = n; // Finding min element for(int i = 0; i < n; i++) { mn = Math.min(mn, b[i]); } // Finding value of x x = 1 - mn; // Creating original sequence for(int i = 0; i < n; i++) { b[i] += x; } // Output original sequence for(int i = 0; i < n; i++) { System.out.print(b[i] + " "); } System.out.println(); } // Driver code public static void main (String[] args) { int N = 3; int arr[] = new int[]{ -2, 1 }; int M = arr.length; find_seq(arr, M, N); } } // This code is contributed by Pratima Pandey |
Python3
# Python3 program for the above approach # Function to find the sequence def find_seq(arr, m, n): b = [] x = 0 # Initializing 1st element b.append(x) # Creating sequence in # terms of x for i in range(n - 1): b.append(x + arr[i] + b[i]) mn = n # Finding min element for i in range(n): mn = min(mn, b[i]) # Finding value of x x = 1 - mn # Creating original sequence for i in range(n): b[i] += x # Output original sequence for i in range(n): print(b[i], end = ' ') print() # Driver code if __name__=='__main__': N = 3 arr = [ -2, 1 ] M = len(arr) find_seq(arr, M, N) # This code is contributed by rutvik_56 |
C#
// C# implementation of the above approach using System; class GFG{ // Function to find the sequence static void find_seq(int []arr, int m, int n) { int []b = new int[n]; int x = 0; // Initializing 1st element b[0] = x; // Creating sequence in // terms of x for(int i = 0; i < n - 1; i++) { b[i + 1] = x + arr[i] + b[i]; } int mn = n; // Finding min element for(int i = 0; i < n; i++) { mn = Math.Min(mn, b[i]); } // Finding value of x x = 1 - mn; // Creating original sequence for(int i = 0; i < n; i++) { b[i] += x; } // Output original sequence for(int i = 0; i < n; i++) { Console.Write(b[i] + " "); } Console.WriteLine(); } // Driver code public static void Main(String[] args) { int N = 3; int []arr = new int[]{ -2, 1 }; int M = arr.Length; find_seq(arr, M, N); } } // This code is contributed by 29AjayKumar |
3 1 2
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find Array formed by adding each element of given array with largest element in new array to its left
- Sort elements of array whose modulo with K yields P
- Count pairs from two arrays whose modulo operation yields K
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Maximize the number of indices such that element is greater than element to its left
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Form an array of distinct elements with each element as sum of an element from each array
- Maximum set bit count from pairs of integers from 0 to N that yields a sum as N
- Range Query on array whose each element is XOR of index value and previous element
- Find the difference of count of equal elements on the right and the left for each element
- Largest element smaller than current element on left for every element in Array
- Array formed from difference of each element from the largest element in the given array
- Sum of product of each element with each element after it
- Replace elements with absolute difference of smallest element on left and largest element on right
- Finding 'k' such that its modulus with each array element is same
- Count of right shifts for each array element to be in its sorted position
- Count of permutations of an Array having each element as a multiple or a factor of its index
- Replace each element by the difference of the total size of the array and frequency of that element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

