Codechef Problems: Equal Buying (EQBUY)

 Problem Statement:

https://www.codechef.com/problems/EQBUY

Solution:

Given, N : weight of sacks of flour and sugar chef has

                2 kg: weight of a flour sack

                1 kg: weight of a sugar sack

To find: check whether chef has even number of sacks of flour and sugar

 

To solve this problem, let us assume chef has equal number of sacks (X) for sugar and flour then,

Total weight(N) = weight of flour sacks + weight of sugar sacks

                               = X * 2kg + X * 1kg

                                = X * 3 kg

 

For above we can say that for number of sacks to be equal the total weight has to divisible by 3

So,

Condition 1: total weight (N) is divisible by 3

Output: YES

Condition 2 : Otherwise

Output: NO

 

Code Solution:

#include <bits/stdc++.h>

using namespace std;

int main() {

    int n;

    cin >> n;

    if (n % 3 == 0) {

        cout << "Yes" << endl;

    } else {

        cout << "No" << endl;

    }

    return 0;

}

Comments