# Generate original array from an array that store the counts of greater elements on right

Given an array of integers greater[] in which every value of array represent how many elements are greater to it’s on right side in an unknown array arr[]. Our task is to generate original array arr[]. It may be assumed that the original array contains elements in range from 1 to n and all elements are unique
Examples:

```Input  : greater[] = { 6, 3, 2, 1, 0, 0, 0 }
Output : arr[] = [ 1, 4, 5, 6, 7, 3, 2 ]

Input  : greater[] = { 0, 0, 0, 0, 0 }
Output : arr[] = [ 5, 4, 3, 2, 1 ]
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We consider an array of elements temp[] = {1, 2, 3, 4, .. n}. We know value of greater indicates count of elements greater than arr. We can observe that (n – greater)-th element of temp[] can be put at arr. So we put this at arr and remove this from temp[]. We repeat above process for remaining elements. For every element greater[i], we put (n – greater[i] – i)-th element of temp[] in arr[i] and remove it from temp[].

Below is the implementation of above idea

 `// C++ program to generate original array  ` `// from an array that stores counts of  ` `// greater elements on right. ` `#include ` `using` `namespace` `std; ` ` `  `void` `originalArray(``int` `greater[], ``int` `n) ` `{ ` `    ``// Array that is used to include every ` `    ``// element only once ` `    ``vector<``int``> temp; ` `    ``for` `(``int` `i = 0; i <= n; i++) ` `        ``temp.push_back(i); ` ` `  `    ``// Traverse the array element ` `    ``int` `arr[n]; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// find the K-th (n-greater[i]-i)  ` `        ``// smallest element in Include_Array ` `        ``int` `k = n - greater[i] - i; ` ` `  `        ``arr[i] = temp[k]; ` ` `  `        ``// remove current k-th element  ` `        ``// from Include array ` `        ``temp.erase(temp.begin() + k); ` `    ``} ` ` `  `    ``// print resultant array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `} ` ` `  `// driver program to test above function ` `int` `main() ` `{ ` `    ``int` `Arr[] = { 6, 3, 2, 1, 0, 1, 0 }; ` `    ``int` `n = ``sizeof``(Arr) / ``sizeof``(Arr); ` `    ``originalArray(Arr, n); ` `    ``return` `0; ` `} `

 `// Java program to generate original array  ` `// from an array that stores counts of  ` `// greater elements on right. ` `import` `java.util.Vector; ` ` `  `class` `GFG  ` `{ ` `     `  `static` `void` `originalArray(``int` `greater[], ``int` `n)  ` `{  ` `    ``// Array that is used to include every  ` `    ``// element only once  ` `    ``Vector temp = ``new` `Vector();  ` `    ``for` `(``int` `i = ``0``; i <= n; i++)  ` `        ``temp.add(i);  ` ` `  `    ``// Traverse the array element  ` `    ``int` `arr[] = ``new` `int``[n];  ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{  ` ` `  `        ``// find the K-th (n-greater[i]-i)  ` `        ``// smallest element in Include_Array  ` `        ``int` `k = n - greater[i] - i;  ` ` `  `        ``arr[i] = temp.get(k);  ` ` `  `        ``// remove current k-th element  ` `        ``// from Include array  ` `        ``temp.remove(k);  ` `    ``}  ` ` `  `    ``// print resultant array  ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `            ``System.out.print(arr[i] + ``" "``);  ` `}  ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `Arr[] = { ``6``, ``3``, ``2``, ``1``, ``0``, ``1``, ``0` `};  ` `    ``int` `n = Arr.length;  ` `    ``originalArray(Arr, n); ` `} ` `}  ` ` `  `// This code is contributed by Rajput-Ji `

 `# Python3 program original array from an ` `# array that stores counts of greater  ` `# elements on right ` `def` `originalArray(greater, n): ` `     `  `    ``# array that is used to include  ` `    ``# every element only once ` `    ``temp ``=` `[] ` `     `  `    ``for` `i ``in` `range``(n ``+` `1``): ` `        ``temp.append(i) ` `         `  `    ``# traverse the array element ` `    ``arr ``=` `[``0` `for` `i ``in` `range``(n)] ` `     `  `    ``for` `i ``in` `range``(n): ` ` `  `        ``# find the Kth (n-greater[i]-i) ` `        ``# smallest element in Include_array ` `        ``k ``=` `n ``-` `greater[i] ``-` `i ` `         `  `        ``arr[i] ``=` `temp[k] ` `         `  `        ``# remove current kth element ` `        ``# from include array ` `        ``del` `temp[k] ` `         `  `    ``for` `i ``in` `range``(n): ` `        ``print``(arr[i], end ``=` `" "``) ` `         `  `# Driver code ` `arr ``=` `[``6``, ``3``, ``2``, ``1``, ``0``, ``1``, ``0``] ` `n ``=` `len``(arr) ` `originalArray(arr, n) ` ` `  `# This code is contributed  ` `# by Mohit Kumar `

 `// C# program to generate original array  ` `// from an array that stores counts of  ` `// greater elements on right. ` `using` `System; ` `using` `System.Collections.Generic;  ` ` `  `class` `GFG  ` `{ ` `     `  `static` `void` `originalArray(``int` `[]greater, ``int` `n)  ` `{  ` `    ``// Array that is used to include every  ` `    ``// element only once  ` `    ``List<``int``> temp = ``new` `List<``int``>();  ` `    ``for` `(``int` `i = 0; i <= n; i++)  ` `        ``temp.Add(i);  ` ` `  `    ``// Traverse the array element  ` `    ``int` `[]arr = ``new` `int``[n];  ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{  ` ` `  `        ``// find the K-th (n-greater[i]-i)  ` `        ``// smallest element in Include_Array  ` `        ``int` `k = n - greater[i] - i;  ` ` `  `        ``arr[i] = temp[k];  ` ` `  `        ``// remove current k-th element  ` `        ``// from Include array  ` `        ``temp.RemoveAt(k);  ` `    ``}  ` ` `  `    ``// print resultant array  ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `            ``Console.Write(arr[i] + ``" "``);  ` `}  ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `[]Arr = { 6, 3, 2, 1, 0, 1, 0 };  ` `    ``int` `n = Arr.Length;  ` `    ``originalArray(Arr, n); ` `} ` `}  ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:
```1 4 5 6 7 2 3
```

Time Complexity : (n2) (Erase operation takes O(n) in vector)

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :