Print Array after moving first occurrence of given element to end in given Array for Q queries
Given an array arr[] of N integers and an array query[] having Q integers, the task is to print the array arr[] after moving the first occurrence of query[i] to the end of the array arr[] for each i in the range [0, Q).
Example:
Input: arr[] = {1, 3, 1, 3}, query[] = {3, 1}
Output: 1 3 3 1
Explanation: In 1st iteration, send first occurrence of query[0] to the end, i.e, send first occurrence of 3 to the end.
Hence, the array becomes arr[] = {1, 1, 3, 3}.
In 2nd iteration, send first occurrence of query[1] to the end.
Hence, array arr[] = {1, 3, 3, 1} which is the required array.Input: arr[] = {1, 2, 3, 4, 5}, query[] = {4, 3}
Output: 1 2 5 4 3
Approach: The given problem can be solved using hashing. The idea is to store the list of indices of each integer in a set data structure, and for an operation on the current integer, remove the 1st index from the set representing the index of the first occurrence of the current integer and insert the index of the last index into the set. Below are the steps to follow:
- Create an unordered map m, storing the set of indices for each index.
- Iterate the array arr[] and insert all the indices into their respective sets in the map m.
- Iterate the array query[] using a variable i and perform the following operations:
- Remove the first element representing the first occurrence from the set m[query[i]].
- Insert the index of the last element i.e, N+i into the set m[query[i]].
- Print the elements in increasing order of their final indices which is the required answer.
Below is the implementation of the above approach:
C++14
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function to print the array after // performing the given operations void sendToLast(vector< int > arr, vector< int > query) { // Stores index of present // integers in a set unordered_map< int , set< int > > m; // Loop to insert indices // into the given map for ( int i = 0; i < arr.size(); i++) { m[arr[i]].insert(i); } // Loop to iterate the // query array for ( int i = 0; i < query.size(); i++) { // Erase the index of current // element from the map m[query[i]].erase(*m[query[i]].begin()); // Insert new location m[query[i]].insert(arr.size() + i); } // Vector of pair to store index // value pair vector<pair< int , int > > v; // Insert all index value pair // into the vector v for ( auto x : m) { for ( auto y : x.second) { v.push_back({ y, x.first }); } } // Sort v in increasing order // of the index sort(v.begin(), v.end()); // Print array for ( int i = 0; i < v.size(); i++) { cout << v[i].second << " " ; } } // Driver Code int main() { vector< int > arr{ 1, 3, 1, 3 }; vector< int > query{ 3, 1 }; sendToLast(arr, query); return 0; } |
Python3
# Python program for the above approach # Function to print array after # performing the given operations def sendToLast(arr, query): # Stores index of present # integers in a set m = {} # Loop to insert indices # into the given map for i in range ( len (arr)): if arr[i] not in m: m[arr[i]] = [] m[arr[i]].append(i) # Loop to iterate the # query array for i in range ( len (query)): # Erase the index of current # element from the map m[query[i]] = m[query[i]][ 1 :] #-.erase(*m[query[i]].begin()) # Insert new location m[query[i]].append( len (arr) + i) # Vector of pair to store index # value pair v = [] # Insert all index value pair # into the vector v for x in m: for y in m[x]: v.append([y, x]) # Sort v in increasing order # of the index v.sort() # Print array for i in range ( len (v)): print (v[i][ 1 ], end = " " ) # Driver Code arr = [ 1 , 3 , 1 , 3 ] query = [ 3 , 1 ] sendToLast(arr, query) # This code is contributed by Shubham Singh |
1 3 3 1
Time Complexity: O(Q * log N)
Auxiliary Space: O(N)