SHOW:
|
|
- or go back to the newest paste.
1 | // Author : Saurav Kalsoor | |
2 | - | // Disjoint Sum - JAVA |
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 | - | disjointSum(n, arr); |
18 | + | System.out.println(duplicateBogies(n, arr)); |
19 | } | |
20 | ||
21 | - | public static void disjointSum(int n, ArrayList<Integer> arr){ |
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 | - | ArrayList<Integer> result = new ArrayList<>(); |
27 | + | break; |
28 | - | for(int i=1; i <= n; i++){ |
28 | + | } |
29 | - | result.add(Math.max(i, st.size())); |
29 | + | |
30 | } | |
31 | ||
32 | - | for(int x :result){ |
32 | + | Collections.reverse(arr); |
33 | - | System.out.print(x + " "); |
33 | + | st.clear(); |
34 | ||
35 | - | System.out.println(); |
35 | + | |
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 |