Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author : Saurav Kalsoor
- // Duplicate Bogies - JAVA
- import java.util.*;
- public class Test {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- int n = sc.nextInt();
- ArrayList<Integer> arr = new ArrayList<>();
- for(int i=0; i < n ;i++){
- arr.add(sc.nextInt());
- }
- System.out.println(duplicateBogies(n, arr));
- }
- public static int duplicateBogies(int n, ArrayList<Integer> arr){
- HashSet<Integer> st = new HashSet<>();
- int res = n;
- for(int x : arr){
- if(st.contains(x)){
- res = (n - st.size());
- break;
- }
- st.add(x);
- }
- Collections.reverse(arr);
- st.clear();
- for(int x : arr){
- if(st.contains(x)){
- res = Math.min(res, (n - st.size()));
- break;
- }
- st.add(x);
- }
- if(res == n) res = 0;
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement