Given an array **arr[]** consisting of **N** integers, read every element of the array one by one and perform the following operations:

- If the current element
**arr[i]**had previously occurred in the array, increase its first occurrence by**K**. - Otherwise, insert
**arr[i]**into the sequence

The task is to print the final sequence of integers obtained by performing the above operations

**Examples:**

Input:arr[] = {1, 2, 3, 2, 3}, K = 1Output:[1, 4, 3, 2, 3]Explanation:Arrival : 1

Since 1 is the first element in the stream, simply insert it into the solution.

Therefore, b[] = [1]

Arrival: 2

Since 2 is not existing in the array, simply insert it into the solution.

Therefore, b[] = [1, 2]Arrival: 3

Since 3 is not existing in the array, simply insert it into the solution.

Therefore, b[] = [1, 2, 3]Arrival: 2

Since 2 already exists, increasing its first occurrence by K(=1)modifies the array b[] to [1, 3, 3, 2]Arrival: 3

Since 3 already exists, increasing its first occurrence by K(=1)modifies the array b[] to [1, 4, 3, 2, 3]

Input:arr[] = {1, 4, 1, 1, 4}, K = 6Output: [7, 10, 7, 1, 4]

**Naive Approach:** The simplest approach to solve the problem is to traverse the array, and for every array element **arr[i]**, traverse in the range **[0, i – 1]** to check if **arr[i]** is already present in the array or not. If found to be true, increase the first occurrence of **arr[i] **by **K**.**Time Complexity:** O(N^{2})**Auxiliary Space:** O(1)

**Efficient Approach:** The above approach can be optimized using **Hashing**. Follow the steps below to solve the problem:

- Traverse the array and store the occurrence of every array element in a Map paired with the index of its occurrence in increasing order.
- If
**arr[i]**is found to be already present in the**Map**, remove the first occurrence of**arr[i**] from the**Map**. Insert that index paired with**arr[i] + K**as the key back into the**Map**. - Repeat the above steps for all array elements. Once, the entire array is traversed, obtain the sequence of integers from the
**Map**and print the final sequence.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Print the required final sequence ` `void` `printSequence(vector<` `int` `>& A, ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Stores the array element-index pairs ` ` ` `unordered_map<` `int` `, set<` `int` `> > mp; ` ` ` ` ` `// Stores the required sequence ` ` ` `vector<` `int` `> sol; ` ` ` ` ` `// Insert all array elements ` ` ` `for` `(` `int` `x : A) ` ` ` `sol.push_back(x); ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// If current element has ` ` ` `// not occurred previously ` ` ` `if` `(mp.find(sol[i]) == mp.end() ` ` ` `|| mp[sol[i]].size() == 0) { ` ` ` `mp[sol[i]].insert(i); ` ` ` `} ` ` ` ` ` `// Otherwise ` ` ` `else` `{ ` ` ` ` ` `// Iterator to the first index ` ` ` `// containing sol[i] ` ` ` `auto` `idxx = mp[sol[i]].begin(); ` ` ` ` ` `int` `idx = *idxx; ` ` ` ` ` `// Remove that occurrence ` ` ` `mp[sol[i]].erase(idxx); ` ` ` ` ` `// Increment by K ` ` ` `sol[idx] += k; ` ` ` ` ` `// Insert the incremented ` ` ` `// element at that index ` ` ` `mp[sol[idx]].insert(idx); ` ` ` `mp[sol[i]].insert(i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the final sequence ` ` ` `for` `(` `int` `x : sol) { ` ` ` `cout << x << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` `int` `K = 6; ` ` ` `vector<` `int` `> A = { 1, 4, 1, 1, 4 }; ` ` ` `printSequence(A, N, K); ` `}` |

*chevron_right*

*filter_none*

**Output:**

7 10 7 1 4

**Time Complexity:** O(NlogN)**Auxiliary Space:** O(N)

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:

- Group multiple occurrence of array elements ordered by first occurrence
- Maximize distinct elements by incrementing/decrementing an element or keeping it same
- Find starting index for every occurrence of given array B in array A using Z-Algorithm
- Queries to search for an element in an array and modify the array based on given conditions
- Modify the string such that every character gets replaced with the next character in the keyboard
- Insert duplicate of K adjacent to it for it's every occurrence in array
- Largest element smaller than current element on left for every element in Array
- Remove the first and last occurrence of a given Character from a String
- Find index of first occurrence when an unsorted array is sorted
- Count all elements in the array which appears at least K times after their first occurrence
- Modify given array to make sum of odd and even indexed elements same
- Closest greater element for every array element from another array
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Modify a binary array to Bitwise AND of all elements as 1
- Modify array by merging elements with addition such that it consists of only Primes.
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Find the element that appears once in an array where every other element appears twice
- Find last element after deleting every second element in array of n integers
- Replace every array element by Bitwise Xor of previous and next 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.