CSES Solutions - Apple Division
Last Updated : 16 Mar, 2024
There are N apples with known weights given as arr[]. Your task is to divide the apples into two groups so that the difference between the weights of the groups is minimal.
Examples:
Input: N = 5, arr[] = {3, 2, 7, 4, 1}
Output: 1
Explanation: Group 1 has weights 2, 3 and 4 (total weight 9), and group 2 has weights 1 and 7 (total weight 8). Difference = 9 - 8 = 1
Input: N = 4, arr[] = {1, 2, 3, 4}
Output: 0
Explanation: Group 1 has weights 1 and 4 (total weight 5), and group 2 has weights 2 and 3 (total weight 5). Difference = 5 - 5 = 0
Approach: To solve the problem, follow the below idea:
The problem can be solved using Recursion by generating all the possible divisions. This can be done by making two choices at every index: either choose the element at the current index in the first group or not choose the element at the current index in the first group (choose the element in the second group). After traversing though the entire array, we can return the absolute difference of sum of elements between both groups.
Step-by-step algorithm:
- Maintain a recursive function solve(idx, arr,r sum1, sum2, N) which returns the minimum difference of weights between two groups if we are currently at index idx.
- Check if we have traversed the entire array, that is idx == N, then simply return the difference between the sum of weights of both the groups.
- Otherwise, explore both the cases when the element at index idx is chosen in the first group and when the element at index idx is chosen in the second group.
- After exploring all the possibilities, return the minimum possible weight difference between both the groups.
Below is the implementation of the algorithm:
C++ #include <bits/stdc++.h> #define ll long long int using namespace std; ll solve(int idx, ll* arr, ll sum1, ll sum2, ll N) { // If we have reached the end, return the difference // between the sums if (idx == N) { return abs(sum1 - sum2); } // Choose the current apple in group 1 ll choose = solve(idx + 1, arr, sum1 + arr[idx], sum2, N); // Choose the current apple in group 2 ll notChoose = solve(idx + 1, arr, sum1, sum2 + arr[idx], N); // Return the minimum of both the choices return min(choose, notChoose); } int main() { // Sample Input ll N = 5; ll arr[] = { 3, 2, 7, 4, 1 }; // Call the recursive function to find the minimum // difference between both the groups ll ans = solve(0, arr, 0, 0, N); cout << ans; }
Java public class MinimumDifference { static long solve(int idx, long[] arr, long sum1, long sum2, int N) { // If we have reached the end, return the difference between the sums if (idx == N) { return Math.abs(sum1 - sum2); } // Choose the current apple in group 1 long choose = solve(idx + 1, arr, sum1 + arr[idx], sum2, N); // Choose the current apple in group 2 long notChoose = solve(idx + 1, arr, sum1, sum2 + arr[idx], N); // Return the minimum of both the choices return Math.min(choose, notChoose); } public static void main(String[] args) { // Sample Input int N = 5; long[] arr = { 3, 2, 7, 4, 1 }; // Call the recursive function to find the minimum difference between both the groups long ans = solve(0, arr, 0, 0, N); System.out.println(ans); } } // This code is contributed by shivamgupta310570
C# using System; class Program { // Function to solve for the minimum difference static long Solve(int idx, long[] arr, long sum1, long sum2, int N) { // If we have reached the end, return the difference // between the sums if (idx == N) { return Math.Abs(sum1 - sum2); } // Choose the current item in group 1 long choose = Solve(idx + 1, arr, sum1 + arr[idx], sum2, N); // Choose the current item in group 2 long notChoose = Solve(idx + 1, arr, sum1, sum2 + arr[idx], N); // Return the minimum of both choices return Math.Min(choose, notChoose); } static void Main(string[] args) { // Sample Input int N = 5; long[] arr = { 3, 2, 7, 4, 1 }; // Call the recursive function to find the minimum // difference between both groups long ans = Solve(0, arr, 0, 0, N); Console.WriteLine(ans); } }
JavaScript function GFG(idx, arr, sum1, sum2, N) { // If we have reached the end and // return the absolute difference between the sums if (idx === N) { return Math.abs(sum1 - sum2); } // Choose the current apple in the group 1 and 2 const choose = GFG(idx + 1, arr, sum1 + arr[idx], sum2, N); const notChoose = GFG(idx + 1, arr, sum1, sum2 + arr[idx], N); // Return the minimum of both the choices return Math.min(choose, notChoose); } // Main function function main() { const N = 5; const arr = [3, 2, 7, 4, 1]; // Call the recursive function to find the minimum // difference between both the groups const ans = GFG(0, arr, 0, 0, N); console.log(ans); } main();
Python3 def solve(idx, arr, sum1, sum2, N): # If we have reached the end, return the difference # between the sums if idx == N: return abs(sum1 - sum2) # Choose the current apple in group 1 choose = solve(idx + 1, arr, sum1 + arr[idx], sum2, N) # Choose the current apple in group 2 not_choose = solve(idx + 1, arr, sum1, sum2 + arr[idx], N) # Return the minimum of both the choices return min(choose, not_choose) # Sample Input N = 5 arr = [3, 2, 7, 4, 1] # Call the recursive function to find the minimum # difference between both the groups ans = solve(0, arr, 0, 0, N) print(ans)
Time Complexity: O(2 ^ N), where N is the number of apples.
Auxiliary Space: O(2 ^ N)
Similar Reads
CSES Solutions â Distributing Apples There are n children and m apples that will be distributed to them. Your task is to count the number of ways this can be done. Examples: Input: n = 3, m = 2Output: 6Explanation: There are 6 ways to distribute 2 apples among 3 children: [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0] and [2, 0,
5 min read
CSES Problem Set Solutions In this article, we have compiled comprehensive, high-quality tutorials on the CSES Problem Set Solutions to assist you in understanding the problem set for learning algorithmic programming. What is CSES Problem Set?CSES Problem Set is a collection of competitive programming tasks hosted on the CSES
8 min read
Maximum and Minimum apples distribution limits Given an integer N representing a number of apples and an array weights[] consisting weight of these apples. Distribute them to K people such that they receive equal-weighing apples. Each person must receive at least one apple. Cutting an apple in half is restricted, the task is to print the maximum
11 min read
What is the Division Formula? Division is one of the top four important arithmetic operations (i.e., Addition, subtraction, multiplication, division). Division operation is used to split the number into equal parts. The symbolic representation for division is '÷' and '/'. a divided by b can be represented as a÷b or a/b. The form
4 min read
Class 9 RD Sharma Solutions - Chapter 1 Number System - Exercise 1.3 Question 1: Express each of the following decimals in the form p/q:(i) 0.39(ii) 0.750(iii) 2.15(iv) 7.010(v) 9.90(vi) 1.0001Solution:(i) Multiplying and Dividing the number by 1000.39 = 39/100(ii) Multiplying and Dividing the number by 10000.750 = 750/1000 = 3/4(iii) Multiplying and Dividing the num
2 min read
Apple Work Experience Apple Inc. needs no introduction in the realm of technology and innovation. Renowned for its cutting-edge products, sleek design, and unwavering commitment to quality, Apple has carved a niche for itself as a global leader in the tech industry. But what is it like to work at Apple? Let's delve into
4 min read