title: Program Page Replacement Algorithms Set 2 Fifo
signature: class Solution {
  
      public Integer programPageReplacementAlgorithmsSet2Fifo(ArrayList<Integer> pages, Integer n, Integer capacity) {
test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(ArrayList<Integer> pages, Integer n, Integer capacity, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(pages, n, capacity,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(new ArrayList<>(Arrays.asList(4, 4, 6, 7, 8, 11, 13, 18, 26, 35, 36, 37, 45, 46, 46, 47, 48, 49, 51, 52, 53, 56, 61, 74, 75, 77, 80, 83, 85, 86, 87, 90, 93, 95, 97, 98, 99, 99)), 36, 37, 34);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(78, -48, 50, -20, -6, 58, -8, 66, 72, 68, 4, 80, 58, -26, -82, -56, 92, 76, 20, 82, -46, 86, 38, 60, -62, -48, 76, 8, -66, -4, -98, -96, -52, 92)), 33, 23, 30);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 19, 13, 2);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(98, 78, 94, 42, 62, 83, 7, 62, 60, 94, 16, 28, 50, 15, 18, 71, 86, 47, 62, 89)), 15, 11, 13);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-82, -70, -68, -56, -50, -44, 4, 18, 28, 30, 30, 42, 66, 78, 80)), 9, 11, 9);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0)), 25, 25, 2);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(4, 5, 13, 15, 18, 28, 32, 40, 46, 46, 55, 57, 61, 63, 65, 68, 77, 79, 79, 96)), 17, 18, 16);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-2, 82, 2, -74, -6, -24, 54, -74, -98, 8, -94, -60, -42, -38, 36, -38, -58, -70, -28, -34, 70, -6, -2, -76, -40, -4, 0, -4, 76, 48, -34, -26, -48, -58, -88, -44, 20, -22, 78)), 31, 24, 25);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 26, 24, 2);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(4, 90, 28, 71, 69, 45, 92, 63, 72, 76, 47, 85, 36, 59, 88, 46, 28, 19, 50, 31, 63, 13)), 15, 12, 15);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> pages, Integer n, Integer capacity, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(pages, n, capacity),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
source_py: def program_page_replacement_algorithms_set_2_fifo(pages, n, capacity):
      s = set()
      indexes = Queue()
      page_faults = 0
      for i in range(n):
          if (len(s) < capacity):
              if (pages[i] not in s):
                  s.add(pages[i])
                  page_faults += 1
                  indexes.put(pages[i])
          elif (pages[i] not in s):
              val = indexes.queue[0]
              indexes.get()
              s.remove(val)
              s.add(pages[i])
              indexes.put(pages[i])
              page_faults += 1
      return page_faults
source_cpp: int program_page_replacement_algorithms_set_2_fifo(vector<int> pages, int n, int capacity) {
    unordered_set < int > s;
    queue < int > indexes;
    int page_faults = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( s . size ( ) < capacity ) {
        if ( s . find ( pages [ i ] ) == s . end ( ) ) {
          s . insert ( pages [ i ] );
          page_faults ++;
          indexes . push ( pages [ i ] );
        }
      }
      else {
        if ( s . find ( pages [ i ] ) == s . end ( ) ) {
          int val = indexes . front ( );
          indexes . pop ( );
          s . erase ( val );
          s . insert ( pages [ i ] );
          indexes . push ( pages [ i ] );
          page_faults ++;
        }
      }
    }
    return page_faults;
  }