coder0687

Untitled

Apr 13th, 2021 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import static java.lang.Math.*;
  4. class myCode
  5. {
  6.     /*
  7.     * @author for this solution : Rishabh Gupta
  8.     */
  9.     public static void main (String[] args) throws java.lang.Exception
  10.     {
  11.         Scanner sc = new Scanner(System.in);
  12.         String s[] = sc.nextLine().split(",");
  13.         int n=s.length,a[]=new int[n],sum=0;
  14.         for(int i=0;i<n;i++) {
  15.             sum+=a[i]=Integer.valueOf(s[i]);
  16.         }
  17.         Set<Integer> set = new HashSet<>(),set2=new HashSet<>();
  18.         set.add(0); set.add(a[0]);
  19.         for(int i=1;i<n;i++) {
  20.             Iterator<Integer> itr = set.iterator();
  21.             while(itr.hasNext()) {
  22.                 set2.add(a[i]+itr.next());
  23.             }
  24.             set.addAll(set2);
  25.             set2.clear();
  26.         }
  27.         int sum1=0,sum2=0,mn=Integer.MAX_VALUE;
  28.         Iterator<Integer> itr = set.iterator();
  29.         while(itr.hasNext()) {
  30.             int e=itr.next(),x=sum-e;
  31.             if(mn>abs(x-e)) { mn=abs(x-e); sum1=x; sum2=e; }
  32.         }
  33.         System.out.println(sum1<sum2?sum1+" "+sum2:sum2+" "+sum1);
  34.     }
  35. }
Add Comment
Please, Sign In to add comment