
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Queries on a Permutation With Key in C++
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 −
#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, ]
- Related Articles
- Media queries with CSS3
- Previous Permutation With One Swap in Python
- Queries on count of points lie inside a circle in C++
- Queries on insertion of an element in a Bitonic Sequence in C++
- Performing regex Queries with PyMongo?
- Multi-key Indexing on an entire array with MongoDB?
- Create a responsive navigation menu with CSS Media Queries
- Responsive Web Design with Media Queries in CSS
- JavaScript Trigger a button on ENTER key
- How to get primary key value (auto-generated keys) from inserted queries using JDBC?
- Key Points on Automation
- C++ program to find permutation with n peaks
- How to use media queries with JavaScript?
- Changing the primary key on a MongoDB collection?
- How do I make case-insensitive queries on MongoDB?
