Given an array **arr[]** of size **N**, the task is to construct a new array **B[]** using elements of array **A** such that:

- If the element is not present in
**B[]**, then append it to the end. - If the element is present in
**B[]**, then first increment its leftmost occurrence by 1 and then append this element to the end of the array.

**Examples:**

Input:arr[] = {1, 2, 1, 2}

Output:{ 3, 2, 1, 2 }

Explanation:

arr[0] = 1, B = {}. 1 is not present in B. So append it at the end. Therefore, B = {1}

arr[1] = 2, B = {1}. 2 is not present in B. So append it at the end. Therefore, B[] = {1, 2}

arr[2] = 1, B = {1, 2}. 1 is already present in B[]. So increment B[0] by 1 and append 1 at the end. Therefore B[] = {2, 2, 1}

arr[3] = 2, B = {2, 2, 1}. 2 is already present in B[]. So increment B[0] by 1 and append 2 at the end. Therefore B[] = {3, 2, 1, 2}

Input:arr[] = {2, 5, 4, 2, 8, 4, 2}

Output:{3, 5, 5, 3, 8, 4, 2}

**Naive Approach:** For every element in the array A, check if it is present in array B or not. If the element exists, then increment the leftmost occurrence by one. Finally, add the element at the end of the array B[].

**Time Complexity:** O(N^{2})

**Efficient Approach:** The idea is to use a map to store all the indices of every element. The array B[] is generated as:

- For every element in the array
**arr[]**, it is checked if the element is already present in the array**B[]**or not. - If the element is not present in the array
**B[]**, then the element is added to the array**B[]**and its index is added to the**map**. Since this is the first occurrence of the element, this index becomes the**left-most index**of the element. - If the element is already present in the array
**B[]**, then the**left-most**index is returned and the element at that index is**incremented**by one. When the value is incremented, the left-most index of the old value is updated along with the index of the new value.

Below is the implementation of the above approach:

`// C++ program to generate an array ` `// from a given array under the ` `// given conditions ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to generate an array ` `// from a given array under the ` `// given conditions ` `void` `newArray(` `int` `A[], ` `int` `n) ` `{ ` ` ` `// To maintain indexes of the ` ` ` `// elements in sorted order ` ` ` `unordered_map<` `int` `, set<` `int` `> > idx; ` ` ` ` ` `// Initialize new array B ` ` ` `std::vector<` `int` `> B; ` ` ` ` ` `// For every element in the given ` ` ` `// array arr[] ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// Check if the element is present ` ` ` `// in the array B[] ` ` ` `if` `(idx.find(A[i]) != idx.end()) { ` ` ` ` ` `// Get the leftmost position ` ` ` `// in the array B ` ` ` `int` `pos = *idx[A[i]].begin(); ` ` ` ` ` `// Remove the leftmost position ` ` ` `idx[A[i]].erase(idx[A[i]].begin()); ` ` ` ` ` `// Increment the value at ` ` ` `// the leftmost position ` ` ` `B[pos]++; ` ` ` ` ` `// Insert new value position ` ` ` `// in the map ` ` ` `idx[B[pos]].insert(pos); ` ` ` `} ` ` ` ` ` `// Append arr[i] at the end ` ` ` `// of the array B ` ` ` `B.push_back(A[i]); ` ` ` ` ` `// Insert its position in hash-map ` ` ` `idx[A[i]].insert(i); ` ` ` `} ` ` ` ` ` `// Print the generated array ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `cout << B[i] << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 1, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `newArray(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3 2 1 2

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:

- Generate an array B[] from the given array A[] which satisfies the given conditions
- Generate elements of the array following given conditions
- Generate an array of size K which satisfies the given conditions
- Generate a string from an array of alphanumeric strings based on given conditions
- Generate N integers satisfying the given conditions
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Count possible permutations of given array satisfying the given conditions
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Queries to search for an element in an array and modify the array based on given conditions
- Find an integer in the given range that satisfies the given conditions
- Change the given string according to the given conditions
- Count valid pairs in the array satisfying given conditions
- Split the array into equal sum parts according to given conditions
- Maximum length sub-array which satisfies the given conditions
- Count of operations required to update the array such that it satisfies the given conditions
- Maximum length sub-array which satisfies the given conditions
- Count of triplets in an array that satisfy the given conditions
- Find an array of size N that satisfies the given conditions
- Maximum items that can be bought from the cost Array based on given conditions
- Maximize the last Array element as per the given conditions

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.