-1

Q) In problem we have to distribute candy equally among 3 friends either there will be conflict ..

I tried to submit this code on codechef but it's showing some case failure what might be they and how to resolve that.

#include <iostream>

using namespace std;

int main() {
    int t;

    cin >> t;
    for (int i = t; i >=0; i--) {
        int n = 0, nl = 0, n5 = 0;

        cin >> n;

        nl = n / 3;
        n5 = n1 * 3;

        if (n5 == n)
            cout << "YES";
        else
            cout << "NO";
    }
    return 0;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Don't you have to print new line? Actual output might be *"YESNOYES"* with t == 3. – Jarod42 Aug 18 '23 at 17:52
  • `nl = n / 3; n5 = n1 * 3; if (n5 == n)` -- This is too much work just to see if a number is evenly divisible by 3. You don't need extraneous variables, multiplication, etc. Just simply `if (n%3 == 0) { evenly divisible by 3 }` – PaulMcKenzie Aug 18 '23 at 17:55
  • I don't like answering competitive coding puzzles (usually they boil down to not knowing C++ in the first place). But look for the ["modulo" (or remainder) operator]( https://en.cppreference.com/w/cpp/language/operator_arithmetic#Multiplicative_operators) – Pepijn Kramer Aug 18 '23 at 18:40
  • Good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Aug 18 '23 at 18:43

0 Answers0