C++ code to find different winner and non-winner counts on a contest


Suppose we have two numbers n and k, There are n students in a contest. Some of them will receive memento, some will get certificates, and others won't receive anything. Who receives something are called winners. But there are some rules of counting the number of memento and certificates. The number of certificates must be exactly k times greater than the number of memento. The number of winners must not be greater than n/2. It's also possible that there are no winners. We have to identify the maximum possible number of winners, according to these rules. Then find the number of students with memento, the number of students with certificates and the number of students who are not winners.

So, if the input is like n = 18; k = 2, then the output will be [3, 6, 9].

Steps

To solve this, we will follow these steps −

x := (n / 2) / (1 + k)
return x, (k * x) and (n - (x + (k * x)))

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
void solve(int n, int k){
   int x = (n / 2) / (1 + k);
   cout << x << ", " << (k * x) << ", " << (n - (x + (k * x)));
}
int main(){
   int n = 18;
   int k = 2;
   solve(n, k);
}

Input

8, 2

Output

3, 6, 9

Updated on: 30-Mar-2022

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements