Advertisement
saurav_kalsoor

Disjoint Sum - JAVA

Aug 13th, 2022
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author : Saurav Kalsoor
  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.  
  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.  
  21.     public static void disjointSum(int n, ArrayList<Integer> arr){
  22.         HashSet<Integer> st = new HashSet<>();
  23.         for(int x : arr){
  24.             st.add(x);
  25.         }
  26.        
  27.         ArrayList<Integer> result = new ArrayList<>();
  28.         for(int i=1; i <= n; i++){
  29.             result.add(Math.max(i, st.size()));
  30.         }
  31.  
  32.         for(int x  :result){
  33.             System.out.print(x + " ");
  34.         }
  35.         System.out.println();
  36.     }
  37.    
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement