Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Program for K Most Recently Used (MRU) Apps
Next article icon

Program for K Most Recently Used (MRU) Apps

Last Updated : 15 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer K and an array arr[] of N integers which contains the ids of the opened apps in a system where 

  1. arr[0] is the app currently in use
  2. arr[1] is the app which was most recently used and
  3. 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++ implementation of the approach #include <bits/stdc++.h> using namespace std;  // Function to update the array // in most recently used fashion 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 array void printArray(int* arr, int N) {     for (int i = 0; i < N; i++)         cout << arr[i] << " "; }  // Driver code int 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; } 
Java
/*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]);         }     } } 
Python3
# Python implementation of the approach  # Function to update the array # in most recently used fashion def mostRecentlyUsedApps(arr, N, K):      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     x = app_index     app_id = arr[app_index]     while (x > 0):         arr[x] = arr[x - 1]         x -= 1      # Update the current active app     arr[0] = app_id  # Utility function to print # the contents of the array def printArray(arr, N):      for i in range(N):         print(arr[i], end=" ")  # Driver code K = 3 arr = [3, 5, 2, 4, 1] N = len(arr)  mostRecentlyUsedApps(arr, N, K) printArray(arr, N)  # This code is contributed by Samim Hossain Mondal. 
C#
// C# implementation of the approach using System;  class GFG {      // Function to update the array // in most recently used fashion static 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 array static void printArray(int []arr, int N) {     for (int i = 0; i < N; i++)         Console.Write(arr[i]+" "); }  // Driver code static 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 
JavaScript
<script> // Javascript implementation of the approach  // Function to update the array // in most recently used fashion function 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 array function 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> 

Output
4 3 5 2 1 

Time Complexity: O(N)


Next Article
Program for K Most Recently Used (MRU) Apps

D

Dhirendra121
Improve
Article Tags :
  • DSA
  • Technical Scripter 2018

Similar Reads

    Find top k (or most frequent) numbers in a stream
    Given an array of n numbers. Your task is to read numbers from the array and keep at-most K numbers at the top (According to their decreasing frequency) every time a new number is read. We basically need to print top k numbers sorted by frequency when input stream has included k distinct elements, e
    11 min read
    How to add Rate the App feature in Android
    When you publish your app on google play store it is important to get feedback from the user. Unless the user does not love or hate your app, they are not likely to go out of their way to rate your app. Since high rating indicates the success of your app, and even criticism is required to make the a
    2 min read
    Project Idea | Department Data Analysis Mobile Application
    1) Project Title: Department Data Analysis 2) Introduction: Department Data Analysis is a software developed for bringing automation in institutional departmental work (For ex. Attendance Management, E-Circular, Displaying Result, Q&A Forum, Live Chat, E-Notice board). It facilitates to access t
    2 min read
    Top K Frequent Elements in an Array
    Given an array arr[] and a positive integer k, the task is to find the k most frequently occurring elements from a given array.Note: If more than one element has same frequency then prioritise the larger element over the smaller one.Examples: Input: arr= [3, 1, 4, 4, 5, 2, 6, 1], k = 2Output: [4, 1]
    15+ min read
    k most frequent in linear time
    Given an array of integers, we need to print k most frequent elements. If there is a tie, we need to prefer the elements whose first appearance is first.Examples: Input : arr[] = {10, 5, 20, 5, 10, 10, 30}, k = 2 Output : 10 5Input : arr[] = {7, 7, 6, 6, 6, 7, 5, 4, 4, 10, 5}, k = 3 Output : 7 6 5 E
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences