C++ code to find minimum difference between concerts durations

C++Server Side ProgrammingProgramming

Suppose we have three numbers a, b and c. A singer has 'a' one-minute songs, 'b' tow-minutes song and 'c' three-minutes song. He wants to distribute all songs into two concerts, such that every song should be included to exactly one concert. He wants to make the absolute difference of durations of the concerts as small as possible. The duration of the concert is the sum of durations of all songs in that concert. We have to find the minimal possible difference between the concerts durations.

So, if the input is like a = 2; b = 1; c = 3, then the output will be 1, because he can include two one-minute songs and one two-minute song and one three-minute song into the first concert, and two three-minute songs into the second concert. First concert duration will be 1 + 1 + 2 + 3 = 7, the duration of the second concert will be 6. The difference of them is |7 − 6| = 1.

Steps

To solve this, we will follow these steps −

return (a + (1 if c is odd, otherwise 0))

Example

Let us see the following implementation to get better understanding −

#include<bits/stdc++.h>
using namespace std;
int solve(int a, int b, int c){
   return (a+c&1);
}
int main(){
   int a = 2;
   int b = 1;
   int c = 3;
   cout << solve(a, b, c) << endl;
}

Input

2, 1, 3

Output

1
raja
Updated on 15-Mar-2022 06:19:00

Advertisements