Page Faults in LRU
Problem :
Given a sequence of pages and memory capacity, your task is to find the number of page faults using Least Recently Used (LRU) Algorithm .
Given a sequence of pages and memory capacity, your task is to find the number of page faults using Least Recently Used (LRU) Algorithm .
Input:
N - test case
k- no. of elements in array
k elements
t - size of memory
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
int arr[]=new int[n];
int count,i;
for(i=0;i<n;i++)
arr[i]=ab.nextInt();
int k=ab.nextInt();
count=0;
Queue<Integer> q=new LinkedList<Integer>();
for(int j=0;j<n;j++)
{
if(!q.contains(arr[j]))
{
if(count>=k) q.remove();
q.add(arr[j]);
count++;
}
else
{
q.remove(arr[j]);
q.add(arr[j]);
}
}
System.out.println(count);
}
}
}
0 Comments:
Post a Comment