Skip to content
Related Articles

Related Articles

Generate an array using given conditions from a given array
  • Last Updated : 15 Jun, 2020

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

  1. If the element is not present in B[], then append it to the end.
  2. 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(N2)



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;
}
Output:
3 2 1 2

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :