Queries on a Permutation With Key in C++

C++Server Side ProgrammingProgramming

Suppose we have an array queries of positive integers between 1 and m, we have to process all queries, queries[i] (from i=0 to n, n is the size of queries - 1) according to the following rules −

  • At the beginning, we have the permutation P=[1,2,3,...,m].

  • For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P.

We have to find an array containing the result for the given queries.

So, if the input is like queries = [3,1,2,1], m = 5, then the output will be [2,1,2,1], this is because the queries are processed as follow −

  • For index i = 0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then move 3 to the beginning of P resulting in P=[3,1,2,4,5].

  • For index i = 1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then move 1 to the beginning of P resulting in P=[1,3,2,4,5].

  • For index i = 2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then move 2 to the beginning of P resulting in P=[2,1,3,4,5].

  • For index i = 3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then move 1 to the beginning of P resulting in P=[1,2,3,4,5].

  • Finally, the array containing the result is [2,1,2,1].

To solve this, we will follow these steps −

  • Define an array ret

  • Define an array v

  • for initialize i := 0, when i − m, update (increase i by 1), do −

    • insert i + 1 at the end of v

  • for each value x in q, do

    • pos := -1

    • Define an array temp

    • for initialize i := 0, when i < size of v, update (increase i by 1), do −

      • if v[i] is same as x, then −

        • pos := i

        • Come out from the loop

    • insert first element of temp into temp at index v[pos]

    • for initialize i := 0, when i < size of v, update (increase i by 1), do −

      • if i is same as pos, then −

        • Ignore following part, skip to the next iteration

      • insert v[i] at the end of temp

    • v := temp

    • insert pos at the end of ret

  • return ret

Example 

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<int> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
public:
   vector<int> processQueries(vector<int>& q, int m) {
      vector<int> ret;
      vector<int> v;
      for (int i = 0; i < m; i++)
         v.push_back(i + 1);
      for (int x : q) {
         int pos = -1;
         vector<int> temp;
         for (int i = 0; i < v.size(); i++) {
            if (v[i] == x) {
               pos = i;
               break;
            }
         }
         temp.insert(temp.begin(), v[pos]);
         for (int i = 0; i < v.size(); i++) {
            if (i == pos)
               continue;
            temp.push_back(v[i]);
         }
         v = temp;
         ret.push_back(pos);
      }
      return ret;
   }
};
main(){
   Solution ob;
   vector<int> v = {3,1,2,1};
   print_vector(ob.processQueries(v, 5));
}

Input

{3,1,2,1}, 5

Output

[2, 1, 2, 1, ]
raja
Published on 17-Nov-2020 11:48:19
Advertisements