-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7b97b6
commit 25ee928
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |