Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int ans=INT_MAX;
- void helper1(int ar[],int n1,int n,int i,int sum,int tmpsum,int ct)
- {
- if(ct==n1)
- {
- int diff1=sum-tmpsum;
- int diff=abs(diff1-tmpsum);
- if(diff<ans)
- ans=diff;
- return;
- }
- if(i==n)
- return;
- helper1(ar,n1,n,i+1,sum,tmpsum+ar[i],ct+1);
- helper1(ar,n1,n,i+1,sum,tmpsum,ct);
- }
- int helper(int ar[],int n)
- {
- int sum=0;
- for(int i=0;i<n;i++)
- sum+=ar[i];
- ans=INT_MAX;
- int n1=n/2;
- int tmpsum=0;
- int i=0;
- int ct=0;
- helper1(ar,n1,n,i,sum,tmpsum,ct);
- return ans;
- }
- int main()
- {
- int n=5;
- int ar[]={1,4,9,12,17};
- cout<<helper(ar,n)<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement