# Program for K Most Recently Used (MRU) Apps in C++

GIVEN: Number k and an array arr[n], containing n number of integer elements which are storing the id’s of the opened apps in a system.

PROBLEM: To print the contents of the array when the user using the system presses Alt + Tab exactly K number of times.

The position of every id represents different apps in a system as follows:

• Id in arr is the id of an app which is currently in use.
• Id in arr is the id of an app which was most recently used.
• Id in arr[n-1] is the id of an app which was least recently used.

NOTE: After pressing Alt + Tab key, app opening pointer will move through apps from 0th index towards right, depending upon the number of presses, so the app on which the press ends will shift to 0th index, because that will become the most recently opened app.

EXAMPLE:

```Input: arr[] = {1, 2, 3, 4, 5}, k=2
Output: 3 1 2 4 5
Explanation: We wished to switched the app with id 3, so it will become the currently
active app and other active apps will be the most recently used now

Input: arr[] = {6, 1, 9, 5, 3}, k=3
Output: 5 6 1 9 3

```

APPROACH:

• Take an array arr[n] and k as an input.
• Get the index i.e, k of the app which a user wants to switch.
• Make the id at the index k as current and then arrange then as in they are in a order.
• Print the result.

### Program for K Most Recently Used (MRU) Apps in C++:

```#include <bits/stdc++.h>
using namespace std;
// Function to update the array in most recently used fashion
void recently(int* arr, int size, int elem) {
int index = 0;
index = (elem % size);
int temp = index, id = arr[index];
while (temp > 0) {
arr[temp] = arr[--temp];
}
arr = id;
}
//print array elements
void print(int* arr, int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
}
int main() {
int elem = 3;
int arr[] = { 6, 1, 9, 5, 3 };
int size = sizeof(arr) / sizeof(arr);
recently(arr, size, elem);
cout<<"array in most recently used fashion : ";
print(arr, size);
return 0;
}```

### Output:

```\$g++ -o main *.cpp
\$main
array in most recently used fashion : 5 1 9 5 3```