Advertisement
saurav_kalsoor

Duplicate Bogies - JAVA

Aug 13th, 2022 (edited)
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author : Saurav Kalsoor
  2. // Duplicate Bogies - JAVA
  3.  
  4.  
  5. import java.util.*;
  6.  
  7. public class Test {
  8.    
  9.     static Scanner sc = new Scanner(System.in);
  10.  
  11.     public static void main(String[] args) {
  12.         int n = sc.nextInt();
  13.  
  14.         ArrayList<Integer> arr = new ArrayList<>();
  15.         for(int i=0; i < n ;i++){
  16.             arr.add(sc.nextInt());
  17.         }
  18.         System.out.println(duplicateBogies(n, arr));
  19.     }
  20.  
  21.     public static int duplicateBogies(int n, ArrayList<Integer> arr){
  22.         HashSet<Integer> st = new HashSet<>();
  23.         int res = n;
  24.         for(int x : arr){
  25.             if(st.contains(x)){
  26.                 res = (n - st.size());
  27.                 break;
  28.             }
  29.             st.add(x);
  30.         }
  31.  
  32.         Collections.reverse(arr);
  33.         st.clear();
  34.        
  35.         for(int x : arr){
  36.             if(st.contains(x)){
  37.                 res = Math.min(res, (n - st.size()));
  38.                 break;
  39.             }
  40.             st.add(x);
  41.         }
  42.  
  43.         if(res == n) res = 0;
  44.  
  45.         return res;
  46.     }
  47.    
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement