SHOW:
|
|
- or go back to the newest paste.
1 | // Author : Saurav Kalsoor | |
2 | - | // Beautiful Array - JAVA |
2 | + | // Disjoint Sum - 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 | - | int k = 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); | |
19 | } | |
20 | - | System.out.println(beautifulArray(n, k, arr)); |
20 | + | |
21 | public static void disjointSum(int n, ArrayList<Integer> arr){ | |
22 | HashSet<Integer> st = new HashSet<>(); | |
23 | - | public static int beautifulArray(int n, int k, ArrayList<Integer> arr){ |
23 | + | |
24 | - | HashMap<Integer, Integer> freq = new HashMap<>(); |
24 | + | st.add(x); |
25 | } | |
26 | - | freq.put(x, freq.getOrDefault(x, 0) + 1); |
26 | + | |
27 | ArrayList<Integer> result = new ArrayList<>(); | |
28 | for(int i=1; i <= n; i++){ | |
29 | - | Collections.sort(arr); |
29 | + | result.add(Math.max(i, st.size())); |
30 | - | int res = 0; |
30 | + | |
31 | ||
32 | - | for(int i = 0; i < n; i++){ |
32 | + | for(int x :result){ |
33 | - | if(freq.containsKey(arr.get(i))){ |
33 | + | System.out.print(x + " "); |
34 | - | int y = k * arr.get(i); |
34 | + | |
35 | - | freq.put(arr.get(i), freq.get(arr.get(i)) - 1); |
35 | + | System.out.println(); |
36 | } | |
37 | - | if(freq.get(arr.get(i)) == 0){ |
37 | + | |
38 | - | freq.remove(arr.get(i)); |
38 | + | |
39 | - | } |
39 | + |