Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import static java.lang.Math.*;
- class myCode
- {
- /*
- * @author for this solution : Rishabh Gupta
- */
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc = new Scanner(System.in);
- String s[] = sc.nextLine().split(",");
- int n=s.length,a[]=new int[n],sum=0;
- for(int i=0;i<n;i++) {
- sum+=a[i]=Integer.valueOf(s[i]);
- }
- Set<Integer> set = new HashSet<>(),set2=new HashSet<>();
- set.add(0); set.add(a[0]);
- for(int i=1;i<n;i++) {
- Iterator<Integer> itr = set.iterator();
- while(itr.hasNext()) {
- set2.add(a[i]+itr.next());
- }
- set.addAll(set2);
- set2.clear();
- }
- int sum1=0,sum2=0,mn=Integer.MAX_VALUE;
- Iterator<Integer> itr = set.iterator();
- while(itr.hasNext()) {
- int e=itr.next(),x=sum-e;
- if(mn>abs(x-e)) { mn=abs(x-e); sum1=x; sum2=e; }
- }
- System.out.println(sum1<sum2?sum1+" "+sum2:sum2+" "+sum1);
- }
- }
Add Comment
Please, Sign In to add comment