Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
abdurrezzak authored Nov 22, 2017
1 parent d7b97b6 commit 25ee928
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 26.MinimumPartition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This program constructs an array from user inputs and
* calculates the minimun difference achievable by partitioning that array into two parts
* using Dynamic Programming
*
*
* Coded by: Abdurrezak Efe
*
* */
#include <iostream>
#include <cmath>
using namespace std;

int d[100][100000]; //global aray to memoize assuming sum is not over 100000

int min_part(int arr[], int n, int set_sum, int sum)
{

if(d[n][set_sum])
return d[n][set_sum];

if(n == 0)
return abs(sum-2*set_sum);

d[n][set_sum] = min(min_part(arr, n-1, set_sum + arr[n-1], sum), min_part(arr, n-1, set_sum, sum)); //memoizing and calculating
return d[n][set_sum];

}

int main()
{
int n;
cin >> n;

int arr[n];
int sum=0;

for(int i=0;i<n;i++)
cin >> arr[i], sum+=arr[i];

cout << min_part(arr, n, 0, sum) << endl;
}

0 comments on commit 25ee928

Please sign in to comment.