0% found this document useful (0 votes)
11 views22 pages

DP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

DP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Climbing chair

**********************************1******************** //
word break is done but has confusion not aadded in this list solved in gfg
// "static void main" must be defined in a public class.

public class Main {

public static int path(int value,int dp[]){


if(value==0){
return 1;
}
else if(value<0){
return 0;
}
if(dp[value]>0){
return dp[value];
}
int v1=path(value-1,dp);
int v2=path(value-2,dp);
int v3=path(value-3,dp);
int v4=v1+v2+v3;
dp[value]=v4;
return v4;
}

public static void main(String[] args) {


int n=5;
int dp[]=new int[n+1];
// int data= path(n,dp);
// System.out.println(data);
dp[0]=1;
for(int i=1;i<=n;i++){
if(i==1){
dp[i]=dp[i-1];
}
else if(i==2){
dp[i]=dp[i-1]+dp[i-2];
}
else{
dp[i]=dp[i-3]+dp[i-2]+dp[i-1];
}

}
System.out.print(dp[n]);
}
}

*******************************2********************
coin change possible ways to to distribute 3 coins 1,2,3 to pay 4 amount
Input:
n = 4 , m = 3
S[] = {1,2,3}
Output: 4
Explanation: Four Possible ways are:
{1,1,1,1},{1,1,2},{2,2},{1,3}.

class Solution {
public long count(int S[], int coins, int amount) {
// code here.
long dp[]=new long[amount+1];
dp[0]=1;

for(int i=0;i<S.length;i++){

for(int j=S[i];j<dp.length;j++){
dp[j]+=dp[j-S[i]];
}

}
return dp[amount];
}
}
*****************************3*********************
leetcode buy stock and sell easy level
class Solution {
public int maxProfit(int[] A) {
int min_value=Integer.MAX_VALUE;
int least_value=0;
int max_profit=0;
for(int i=0;i<A.length;i++){

if(A[i]<min_value){
min_value=A[i];
}
least_value=A[i]-min_value;
if(least_value>max_profit){
max_profit=least_value;

}
}
return max_profit;

****************************4*****************************

Stickler thief geeks for geeks solution is bit simple... problem is bit
confusion you will understand by seeing the solution

class Solution
{
//Function to find the maximum money the thief can get.
public int FindMaxSum(int arr[], int n)
{
// Your code here
int b[]=new int[n];
if(n==0){
return 0;
}
if(n==1){
return arr[0];

}
if(n==2){
return Math.max(arr[0],arr[1]);

}
b[0]=arr[0];
b[1]=Math.max(arr[0],arr[1]);
for(int i=2;i<arr.length;i++){

b[i]=Math.max(arr[i]+b[i-2],b[i-1]);
}
return b[n-1];
}
}
***************************************************5***************** 0/1
knapsack with recursion doesn't pass the all test cases in gfg 1100/1200 but for
keep in mind

class Solution
{
//Function to return max value that can be put in knapsack of capacity W.
static int max(int a, int b)
{
return (a > b) ? a : b;
}
static int knapSack(int W, int wt[], int val[], int n) //Start
{
// your code here

if(n==0|| W==0){
return 0;
}
if(wt[n-1]<=W){
return max(val[n-1]+knapSack(W-wt[n-1],wt,val,n-1),knapSack(W,wt,val,n-
1));
}
else
return knapSack(W,wt,val,n-1);

}
}

top down it works with full test cases

class Solution
{
//Function to return max value that can be put in knapsack of capacity W.
static int max(int a, int b)
{
return (a > b) ? a : b;
}
static int knapSack(int W, int wt[], int val[], int n)
{
// your code here
int K[][]=new int[n+1][W+1];

for(int i =0;i<=n;i++){
// for(int w=0;w<=W;w++){
K[i][0]=1;
// }
}

for(int i=0;i<=n;i++)
{
for(int j=0;j<=W;j++)
{
if(i==0|| j==0){
K[i][j]=0;

}
else if(wt[i-1]>j){
K[i][j]=K[i-1][j];
}
else{
K[i][j]=max(val[i-1]+K[i-1][j-wt[i-1]],K[i-1][j]); for
unbounded knapsack everything is same except i-1 K[i][j]=max(val[i-1]+K[i][j-
wt[i-1]],K[i-1][j]);
}
}
}

****************************6***********************
fibonnci geeks for geeks why %100000007 do research but for
brief bcz value exceeds its limits that's why we are doing mod

static long nthFibonacci(long n){


// code here
int N=(int)n;
long f[] = new long[N+1]; // 1 extra to handle case, n = 0
int i;

/* 0th and 1st number of the series are 0 and 1*/


f[0] = 0;
f[1] = 1;

for (i = 2; i <= N; i++)


{
/* Add the previous 2 numbers in the series
and store it */
f[i] = (f[i-1] + f[i-2])%1000000007;
}
return f[N];

**************************7***************
subset sum problem

static boolean isSubsetSum(int set[],


int n, int sum)
{
// Base Cases
if (sum == 0)
return true;
if (n == 0)
return false;

// If last element is greater than


// sum, then ignore it
if (set[n - 1] > sum)
return isSubsetSum(set, n - 1, sum);

/* else, check if sum can be obtained


by any of the following
(a) including the last element
(b) excluding the last element */
return isSubsetSum(set, n - 1, sum)
|| isSubsetSum(set, n - 1, sum - set[n - 1]);
}

class Solution{
boolean dp[][];

static Boolean isSubsetSum(int N, int arr[], int sum){


// code here
// memorize(N,arr,sum);

boolean dp[][]=new boolean [N+1][sum+1];


for(int i=0;i<sum+1;i++){
dp[0][i]=false;
}
for(int j=0;j<N+1;j++){
dp[j][0]=true;
}
for(int i=1;i<=N;i++){
for(int j=1;j<=sum;j++){

if(arr[i-1]<=j){
dp[i][j]=dp[i-1][j-arr[i-1]] || dp[i-1][j];
}
else{
dp[i][j]=dp[i-1][j];
}
}
}
return dp[N][sum];
}

************************8******************
Target sum leetcode still we can optimized O(2^n)
class Solution {
int count=0;
public int findTargetSumWays(int[] nums, int target) {

helper(nums,target,0,0);
return count;
}
void helper(int[] nums,int target,int start,int sum){
if(start==nums.length){
if(sum==target){
count++;

}
return;
}

helper(nums,target,start+1,sum+nums[start]);
helper(nums,target,start+1,sum-nums[start]);
//Remeber we are subtracting

}
}

road cutting
**********************9***********************

variation of unbounded knapsack

class Solution{

static int max(int a, int b)


{
return (a > b) ? a : b;
}

public int cutRod(int price[], int n) {


//code here
int length[]=new int[n];
for(int i=0;i<n;i++){
length[i]=i+1;
}

int K[][]=new int[n+1][n+1];

for(int i =0;i<=n;i++){
// for(int w=0;w<=W;w++){
K[i][0]=1;
// }
}

for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i==0|| j==0){
K[i][j]=0;

}
else if(length[i-1]>j){
K[i][j]=K[i-1][j];
}
else{
K[i][j]=max(price[i-1]+K[i][j-length[i-1]],K[i-1][j]); Everything
is same we change value with price array and added a new variable called length in
place of wt
}
}
}

return K[n][n];

}
}

**********************10*******************
Target sum dp soultuion

leetcode

class Solution {
int count=0;
public int findTargetSumWays(int[] arr, int sum) {
int n=arr.length;
int target=0;

for(int i=0;i<n;i++)
target+=arr[i];

if((target-sum)%2==1 || (sum>target))
return 0;

sum=(sum+target)/2;
if(sum<0)
return 0;
int t[][]=new int [n + 1][sum + 1]; // DP - matrix
// initialization
// here we are setting 1st row and 1st column
// i denotes the size of the array
// j denotes the target sum (subset sum)
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= sum; j++) {
if (i == 0) // when array(i) is empty than there is no meaning of
sum of elements so return count of subset as 0;
t[i][j] = 0;
if (j == 0) // when sum(j) is zero and there is always a chance
of empty subset so return count as 1;
t[i][j] = 1;
}
}

for (int i = 1; i <= n; i++) {


for (int j = 0; j <= sum; j++) {
if (arr[i - 1] <= j) // when element in the list is less then
target sum
t[i][j] = t[i - 1][j - arr[i - 1]] + t[i - 1][j]; // either
exclude or inxlude and add both of them to get final count
else
t[i][j] = t[i - 1][j]; // exclude when element in the list
is greater then the sum
}
}

return t[n][sum];

}
}
****************************11******************************
Gold mine geeks for geeks very easy pepcoding solution

class Solution{
static int maxGold(int n, int m, int arr[][])
{
// code here

if(n==1)
{
int sum=0;
for(int i=m-1;i>=0;i--)
sum+=arr[0][i];
return sum;
}

int dp[][]=new int[n][m];

for(int j=arr[0].length-1;j>=0;j--){
for(int i=arr.length-1;i>=0;i--){
if(j==arr[0].length-1){
dp[i][j]=arr[i][j];
}

else if(i==arr.length-1){
dp[i][j]=arr[i][j]+Math.max(dp[i][j+1],dp[i-1][j+1]);
} else if(i==0){
dp[i][j]=arr[i][j]+Math.max(dp[i][j+1],dp[i+1][j+1]);
}
else{
dp[i][j]=arr[i][j]+Math.max(dp[i][j+1],Math.max(dp[i+1]
[j+1],dp[i-1][j+1]));
}
}
}
int max=dp[0][0];
for(int i=1;i<dp.length;i++){
if( dp[i][0]>max){
max=dp[i][0];
}
}
return max;
}
}

***********************************12***************************
Longest Increasing Subsequence
Input:
N = 16
A[]={0,8,4,12,2,10,6,14,1,9,5
13,3,11,7,15}
Output: 6
Explanation:Longest increasing subsequence doubt
0 2 6 9 13 15, which has length 6

class Solution
{
//Function to find length of longest increasing subsequence.
static int longestSubsequence(int n, int a[])
{
// code here
int omax=0;
int dp[]=new int[n];
dp[0]=1;
for(int i=1;i<n;i++){
int max=0;
for(int j=0;j<i;j++){
if(a[i]>a[j]){
if(dp[j]>max){
max=dp[j];
}
}
}
dp[i]=max+1;
if(dp[i]>omax){
omax=dp[i];
}
}
return omax;
}
}

*******************13**************
Longest Common Substring
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.

class Solution {
int max(int a, int b)
{
return (a > b)? a : b;
}

public int longestCommonSubsequence(String S1, String S2) {


int n=S1.length();
int m=S2.length();
int dp[][]=new int[n+1][m+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i==0||j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){

if(S1.charAt(i-1)==S2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[n][m];

}
}

recursion approach
int lcs( char[] X, char[] Y, int m, int n )
{
if (m == 0 || n == 0)
return 0;
if (X[m-1] == Y[n-1])
return 1 + lcs(X, Y, m-1, n-1);
else
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}

****************************14**************
Shortest Common Subsequene Note :- Similar to Longest common
subsequence just calculate substracting total length of string form lcs (M+N)-LSC

class Solution
{
//Function to find length of shortest common supersequence of two strings.

static int LCS(String X,String Y,int m,int n){

int dp[][]=new int[m+1][n+1];


for(int i=0;i<m+1;i++){
for(int j=0;j<n+1;j++){
if(i==0 || j==0){
dp[i][j]=0;
}
}
}

for(int i=1;i<m+1;i++){
for(int j=1;j<n+1;j++){

if(X.charAt(i-1)==Y.charAt(j-1)){

dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
return dp[m][n];

}
public static int shortestCommonSupersequence(String X,String Y,int m,int n)
{
//Your code here

return m+n-LCS(X,Y,m,n);
}
}

**********************************15*******************
Minimum insertion and deletion

Input: str1 = "heap", str2 = "pea"


Output: 3
Explanation: 2 deletions and 1 insertio
class Solution
{
public int minOperations(String str1, String str2)
{
// Your code goes here
int n=str1.length();
int m=str2.length();

int dp[][]=new int[n+1][m+1];

for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0|| j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
if(str1.charAt(i-1)==str2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
int sum=n-dp[n][m];
int sum2=m-dp[n][m];

return sum+sum2;

}
}

***************16***************
palindrom subsequnce leetcode solution easy will give you one string.......create
another string by reverse the string and then calculate LCS will give to
palindromic subsequence

class Solution {
public int longestPalindromeSubseq(String s) {
String s2="";
for(int i=s.length()-1;i>=0;i--){
s2=s2+s.charAt(i);
}

int n=s.length();
int m=s.length();

int dp[][]=new int[n+1][m+1];

for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0|| j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){

if(s.charAt(i-1)==s2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}

return dp[n][m];
}
}
*********************17****************************
Minimum Insertion Steps to Make a String Palindrome leetcode similar to
above one no difference except last line

Example 1:

Input: s = "zzazz"
Output: 0
Explanation: The string "zzazz" is already palindrome we don't need any insertions.
Example 2:

Input: s = "mbadm"
Output: 2
Explanation: String can be "mbdadbm" or "mdbabdm".

class Solution {
public int minInsertions(String s) {
String s2="";
for(int i=s.length()-1;i>=0;i--){
s2=s2+s.charAt(i);
}

int n=s.length();
int m=s.length();

int dp[][]=new int[n+1][m+1];

for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0|| j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){

if(s.charAt(i-1)==s2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}

return s.length()-dp[n][m];

}
}

************************************18*****************************
Maximize the cut segment it is easy take a look at it

class Solution
{
//Function to find the maximum number of cuts.

public int maximizeCuts(int l, int p, int q, int r)


{
int dp[] = new int[l + 1];

// All values with -1


for (int i = 0; i < l + 1; i++)
dp[i] = -1;

// if length of rod is 0
// then total cuts will
// be 0 so, initialize
// the dp[0] with 0
dp[0] = 0;

for (int i = 0; i <= l; i++) {

// if certain length
// is not possible
if (dp[i] == -1)
continue;

// if a segment of
// p is possible
if (i + p <= l)
dp[i + p] = Math.max(dp[i + p], dp[i] + 1); it will check
current and its previous value

// if a segment of
// q is possible
if (i + q <= l)
dp[i + q] = Math.max(dp[i + q], dp[i] + 1);

// if a segment of
// r is possible
if (i + r <= l)
dp[i + r] = Math.max(dp[i + r], dp[i] + 1);
}

// if no segment can be cut then return 0


if (dp[l] == -1) {
dp[l] = 0;
}
// return value corresponding
// to length l
return dp[l];
}
}
******************19*****************
Stock buy and sell leetcode easy
class Solution {
public int maxProfit(int[] A) {
int min_value=Integer.MAX_VALUE;
int least_value=0;
int max_profit=0;
for(int i=0;i<A.length;i++){
if(A[i]<min_value){
min_value=A[i];
}
least_value=A[i]-min_value;
if(least_value>max_profit){
max_profit=least_value;

}
}
return max_profit;

}
}

stock buy and sell II Medium level

class Solution {
public int maxProfit(int[] prices) {

int profit =0;


for(int i=1;i<prices.length;i++){

if(prices[i]>prices[i-1]){
profit+=prices[i]-prices[i-1];
}
}
return profit;
}
}

***********************20***************** Edit distance geeks for geeks almost


same as LCS but bit variation for value of empty
s="";
t=abcd"
we need 4 operation similarlyt vice vercas that's why boundry conditons are bit
different

if(both values are same will check the previous value i-1,j-1)
for differnt values will compare three values.. and pick the small value with
addition 1

Input:
s = "geek", t = "gesek"
Output: 1
Explanation: One operation is required
inserting 's' between two 'e's of str1.

class Solution {
public int editDistance(String s, String t) {
// Code here

int n=s.length();
int m=t.length();

int dp[][]=new int[n+1][m+1];

for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0){
dp[i][j]=j;
}
if(j==0){
dp[i][j]=i;
}
}
}
for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){

if(i==0){
dp[i][j]=j;
}
else if(j==0){
dp[i][j]=i;
}

else if(s.charAt(i-1)==t.charAt(j-1)){
dp[i][j]=dp[i-1][j-1];
}
else{
dp[i][j]=1+Math.min(dp[i][j-1],Math.min(dp[i-1][j-1],dp[i-1]
[j]));
}
}
}

return dp[n][m];

}
}

**************************************21***************************
Longest Repeating Subsequence variation of LCS only 1 difference index should not
be same gfg

class Solution
{
public int LongestRepeatingSubsequence(String str)
{
// code here
int n=str.length();
int m=str.length();

int dp[][]=new int[n+1][m+1];

for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0|| j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){

if(str.charAt(i-1)==str.charAt(j-1) && i!=j){


dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}

return dp[n][m];
}
}

******************************22**************
Longest Common Substring

class Solution{
int max(int a, int b)
{
return (a > b)? a : b;
}

int longestCommonSubstr(String S1, String S2, int n, int m){


// code here
int ans=0;
int dp[][]=new int[n+1][m+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i==0||j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){

if(S1.charAt(i-1)==S2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
ans=max(ans,dp[i][j]);
}
else{
dp[i][j]=0; Variation of LCS here is one line
difference
}
}
}
return ans;
}
}

******************23***************

Get Minimum Squares


class Solution
{
public int MinSquares(int n)
{
// Code here
int dp[]=new int[n+1];
dp[0]=0;
dp[1]=1;
for(int i=2;i<=n;i++){
int min=Integer.MAX_VALUE;
for(int j=1;j*j<=i;j++){
int sub=i-j*j;
if(dp[sub]<min){
min=dp[sub];
}
}
dp[i]=min+1;
}
return dp[n];
}
}

************************24********************
Max rectangle
Input:
n = 4, m = 4
M[][] = {{0 1 1 0},
{1 1 1 1},
{1 1 1 1},
{1 1 0 0}}
Output: 8
Explanation: For the above test case the
matrix will look like
0 1 1 0
1 1 1 1
1 1 1 1
1 1 0 0
the max size rectangle is
1 1 1 1
1 1 1 1
and area is 4 *2 = 8.

class Solution {
int[] prevSmaller(int[] a){
int[] ps=new int[a.length];
Stack<Integer> stk = new Stack<>();
for(int i=0;i<a.length;i++){
while(!stk.isEmpty() && a[stk.peek()]>=a[i]){
stk.pop();
}
if(stk.isEmpty()){
ps[i]=-1;
}else{
ps[i]=stk.peek();
}
stk.push(i);
}
return ps;
}

int[] nextSmaller(int[] a){


int[] ps=new int[a.length];
Stack<Integer> stk = new Stack<>();
for(int i=a.length-1;i>=0;i--){
while(!stk.isEmpty() && a[stk.peek()]>=a[i]){
stk.pop();
}
if(stk.isEmpty()){
ps[i]=a.length;
}else{
ps[i]=stk.peek();
}
stk.push(i);
}
return ps;
}
int maxAreaHelper(int[] a){
int maxAr=0;
int[] ps = prevSmaller(a);
int[] ns = nextSmaller(a);
for(int i=0;i<a.length;i++){
int curr = (ns[i]-ps[i]-1)*a[i];
maxAr= Math.max(maxAr, curr);
}
return maxAr;
}

public int maxArea(int M[][], int n, int m) {


// add code here.
int result=0;
int[] row = new int[m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(M[i][j]==1){
row[j] += 1;
}else{
row[j] = 0;
}
}
result = Math.max(result, maxAreaHelper(row));
}
return result;

}
}
**********************25*********************

Max sum without Adjacents


Both solution
Input:
N = 6
Arr[] = {5, 5, 10, 100, 10, 5}
Output: 110
Explanation: If you take indices 0, 3
and 5, then Arr[0]+Arr[3]+Arr[5] =
5+100+5 = 110.

class Solution {

int helper(int arr[],int n,int dp[]){


if(n==0){
return arr[n];
}
if(n<0){
return 0;
}
if(dp[n]!=0){
return dp[n];
}

int first=helper(arr,n-2,dp)+arr[n];
int second=helper(arr,n-1,dp);
dp[n]=Math.max(first,second);
return dp[n];

}
int findMaxSum(int arr[], int n) {
// code here
int dp[]=new int[n];
// return helper(arr,n-1,dp);
dp[0]=arr[0];

for(int i=1;i<n;i++){
int take=arr[i];
if(i>1) take+=dp[i-2];

int not=dp[i-1];
dp[i]=Math.max(take,not);

}
return dp[n-1];

}
}

**********************26**************** leetcode
Minimum Falling Path Sum Striver

class Solution {
public int minFallingPathSum(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
int dp[][] = new int[n][m];

// Initializing first row - base condition


for(int j=0; j<m; j++){
dp[0][j] = matrix[0][j];
}

for(int i=1; i<n; i++){


for(int j=0;j<m;j++){

int up = matrix[i][j] + dp[i-1][j];

int leftDiagonal= matrix[i][j];


if(j-1>=0) leftDiagonal += dp[i-1][j-1];
else leftDiagonal += (int)Math.pow(10,9);

int rightDiagonal = matrix[i][j];


if(j+1<m) rightDiagonal += dp[i-1][j+1];
else rightDiagonal += (int)Math.pow(10,9);

dp[i][j] = Math.min(up, Math.min(leftDiagonal,rightDiagonal));

}
}

int maxi = Integer.MAX_VALUE;

for(int j=0; j<m;j++){


maxi = Math.min(maxi,dp[n-1][j]);
}

return maxi;
}
}
*****************************27******************* very easy Minimum sum partition
same as subset sum only in the last 1 step is more ...... Striver
class Solution
{

public int minDifference(int arr[], int n)


{
// Your code goes here
int sum=0;
for(int i=0;i<arr.length;i++){
sum+=arr[i];
}
boolean dp[][]=new boolean[n+1][sum+1];
for(int i=0;i<sum+1;i++){
dp[0][i]=false;
}
for(int i=0;i<n+1;i++){
dp[i][0]=true;
}

for(int i=1;i<n+1;i++){
for(int j=1;j<sum+1;j++){

if(arr[i-1]>j){
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=dp[i-1][j]||dp[i-1][j-arr[i-1]];
}
}
}
int min=Integer.MAX_VALUE;

for(int i=0;i<=sum/2;i++){
if(dp[n][i]==true){
min=Math.min(min,Math.abs(sum-2*i));
}

}
return min;
}
}

*******************************28****************
Min number of coins gfg

class Solution{

public int minCoins(int coins[], int M, int V)


{
// Your code goes here

int dp[][]=new int [M+1][V+1];

for(int i=0;i<M+1;i++){
for(int j=0;j<V+1;j++){
if(i == 0) dp[i][j] = 10000000;
if(j == 0) dp[i][j] = 0;
}
}

for(int i=1;i<M+1;i++){
for(int j=1;j<V+1;j++){

if(coins[i-1]>j){
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=Math.min(dp[i-1][j],1+dp[i][j-coins[i-1]]);
}
}
}
if(dp[M][V] == 10000000) return -1;
return dp[M][V];
}
}

You might also like