Saturday, November 16, 2024
HomeSoftware DevelopmentDecrease Subset sum distinction after deleting K parts of Array

Decrease Subset sum distinction after deleting K parts of Array

[ad_1]

  

import java.io.*;

import java.util.*;

  

class GFG {

  

    static int min = Integer.MAX_VALUE;

  

    

    static void subset(Listing<Integer> arr,

                       int index,

                       int sum, int whole)

    {

        if (index >= arr.measurement()) {

            if (sum != 0)

                min = Math.min(min,

                               sum - (whole - sum));

            return;

        }

        subset(arr, index + 1,

               sum + arr.get(index), whole);

        subset(arr, index + 1, sum, whole);

    }

  

    

    static void print(int index, int arr[],

                      int whole,

                      Listing<Integer> ans)

    {

        if (whole == 0) {

            int sum = 0;

            for (int parts : ans) {

                sum += parts;

            }

  

            

            subset(ans, 0, 0, sum);

            return;

        }

        if (index >= arr.size) {

            return;

        }

        ans.add(arr[index]);

        print(index + 1, arr, whole - 1, ans);

        ans.take away(ans.measurement() - 1);

        print(index + 1, arr, whole, ans);

    }

  

    

    public static void clear up(int arr[], int N)

    {

        int chosen = arr.size - N;

  

        if (chosen <= 1) {

            System.out.println(-1);

            return;

        }

        Listing<Integer> ans = new ArrayList<>();

        print(0, arr, chosen, ans);

    }

  

    

    public static void most important(String[] args)

    {

        int arr[] = { 7, 9, 5, 8, 1, 3 };

        int N = 2;

  

        clear up(arr, N);

        System.out.println(min);

    }

}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments