C++ code to check review vote status and uncertainty

C++Server Side ProgrammingProgramming

Suppose we have three numbers x, y and z. On a review site there were x persons who would upvote, y persons who would downvote, and another group of z persons who would vote, but we do not know whether they would upvote or downvote. Each person can vote at most once. If there are more people upvote than downvote, the result will be "+"; if downvote count is greater, the result will be "-"; otherwise the result will be "0". Because of the z unknown persons, the result may be uncertain (i.e. there are more than one possible results). The result is uncertain (represented as '?') if and only if there exist two different situations of how the z persons vote. We have to find the result or report whether it is uncertain.

So, if the input is like x = 2; y = 0; z = 2, then the output will be '?' because 2 upvotes are there, if other two votes are down then it will be 0, but if they are high it will be '+' so the answer is uncertain.


To solve this, we will follow these steps −

if x > y + z, then:
   return "+"
otherwise when x + z < y, then:
   return "-"
otherwise when not z is non-zero, then:
   return "0"
   return "?"


Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
string solve(int x, int y, int z){
   if (x > y + z)
      return "+";
   else if (x + z < y)
      return "-";
   else if (!z)
      return "0";
      return "?";
int main(){
   int x = 2;
   int y = 0;
   int z = 2;
   cout << solve(x, y, z) << endl;


2, 0, 2


Updated on 15-Mar-2022 06:29:49