Program for K Most Recently Used (MRU) Apps

29 Aug, 2021
Given an integer K and an array arr[] of N integers which contains the ids of the opened apps in a system where  
arr[0] is the app currently in usearr[1] is the app which was most recently used andarr[N – 1] is the app which was least recently used.
arr[0] is the app currently in use
arr[1] is the app which was most recently used and
arr[N – 1] is the app which was least recently used.
The task is to print the contents of the array when the user using the system presses Alt + Tab exactly K number of times. Note that 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.Examples:  
Input: arr[] = {3, 5, 2, 4, 1}, K = 3 Output: 4 3 5 2 1 User want to switch to the app with id 4, it’ll become the currently active app and the previously active app (with id 3) will be the most recently used app.Input: arr[] = {5, 7, 2, 3, 4, 1, 6}, K = 10 Output: 3 5 7 2 4 1 6  
 
Approach: Get the index of the app to which the user wants to switch i.e. appIndex = K % N. Now, the current active app will be arr[appIndex] and all the other apps in the index range [0, appIndex – 1] will have to be shifted by 1 element towards the right.Below is the implementation of the above approach:  
C++14
C#
Javascript
Java
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to update the array// in most recently used fashionvoid mostRecentlyUsedApps(int* arr, int N, int K){    int app_index = 0;     // Finding the end index after K presses    app_index = (K % N);     // Shifting elements by 1 towards the found index    // on which the K press ends    int x = app_index, app_id = arr[app_index];    while (x > 0) {        arr[x] = arr[--x];    }     // Update the current active app    arr[0] = app_id;} // Utility function to print// the contents of the arrayvoid printArray(int* arr, int N){    for (int i = 0; i < N; i++)        cout << arr[i] << " ";} // Driver codeint main(){    int K = 3;    int arr[] = { 3, 5, 2, 4, 1 };    int N = sizeof(arr) / sizeof(arr[0]);     mostRecentlyUsedApps(arr, N, K);    printArray(arr, N);    return 0;}
// C# implementation of the approachusing System; class GFG{     // Function to update the array// in most recently used fashionstatic void mostRecentlyUsedApps(int []arr, int N, int K){    int app_index = 0;     // Finding the end index after K presses    app_index = (K % N);     // Shifting elements by 1 towards the found index    // on which the K press ends    int x = app_index, app_id = arr[app_index];    while (x > 0)    {        arr[x] = arr[--x];    }     // Update the current active app    arr[0] = app_id;} // Utility function to print// the contents of the arraystatic void printArray(int []arr, int N){    for (int i = 0; i < N; i++)        Console.Write(arr[i]+" ");} // Driver codestatic void Main(){    int K = 3;    int []arr = { 3, 5, 2, 4, 1 };    int N = arr.Length;     mostRecentlyUsedApps(arr, N, K);    printArray(arr, N);}} // This code is contributed by mits
<script>// Javascript implementation of the approach // Function to update the array// in most recently used fashionfunction mostRecentlyUsedApps(arr, N, K) {    let app_index = 0;     // Finding the end index after K presses    app_index = (K % N);     // Shifting elements by 1 towards the found index    // on which the K press ends    let x = app_index, app_id = arr[app_index];    while (x > 0) {        arr[x] = arr[--x];    }     // Update the current active app    arr[0] = app_id;} // Utility function to print// the contents of the arrayfunction printArray(arr, N) {    for (let i = 0; i < N; i++)        document.write(arr[i] + " ");} // Driver code let K = 3;let arr = [3, 5, 2, 4, 1];let N = arr.length; mostRecentlyUsedApps(arr, N, K);printArray(arr, N); // This code is contributed by gfgking.</script>
/*package whatever //do not write package name here */ import java.io.*; class GFG {    public static void main(String[] args)    {        int[] num = { 3, 5, 2, 4, 1 };        int size = num.length;        // 8,6,7,9,0,2,1,12,89        int d = 3;         int appIndex = d % size;        int appId = num[appIndex];         for (int i = appIndex; i > 0; i--) {            num[i] = num[i - 1];        }        num[0] = appId;         for (int i = 0; i < num.length; i++) {            System.out.print(" " + num[i]);        }    }}
4 3 5 2 1 
Time Complexity: O(N) 
Mithun Kumar
gfgking
pradyumnadas
Microsoft
programming-puzzle
Arrays
Technical Scripter
Microsoft
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in Java
Linear Search
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Introduction to Arrays
K'th Smallest/Largest Element in Unsorted Array | Set 1
Subset Sum Problem | DP-25
Introduction to Data Structures
Python | Using 2D arrays/lists the right way
Search an element in a sorted and rotated array
Find Second largest element in an array