Everything seems right but the answer is wrong. Please help

I am trying to solve THIS problem.
I have checked everything from my side, all the variables have correct values when I tested them in my IDE.
The code is HERE.

You get an overflow with the sample testcase:

[simon@simon-laptop][20:05:48]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling vedang_danej-MULTHREE.cpp
+ g++ -std=c++14 vedang_danej-MULTHREE.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
vedang_danej-MULTHREE.cpp: In function ‘int main()’:
vedang_danej-MULTHREE.cpp:34:34: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
             int cycles = (k - 3) / 4;
                          ~~~~~~~~^~~
vedang_danej-MULTHREE.cpp:35:70: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
             int one_cycle_sum = 2 * s % 10 + 4 * s % 10 + 6 * s % 10 + 8 * s % 10;
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
vedang_danej-MULTHREE.cpp:36:37: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
             int left_ones = (k - 3) % 4;
                             ~~~~~~~~^~~
+ set +x
Successful
[simon@simon-laptop][20:05:54]
[~/devel/hackerrank/otherpeoples]>echo "3
5 3 4
13 8 1
760399384224 5 1" | ./a.out
NO
YES
vedang_danej-MULTHREE.cpp:43:33: runtime error: signed integer overflow: 1121285031 * 20 cannot be represented in type 'int'
YES

Fix that and try again.

1 Like

That’s interesting. I did not get that error in my IDE or code chef’s IDE. I got the right answer in both IDEs . However, the variable ‘cycles’ was probably overflowing. I made it long long. I also made the data type of the ‘sum’ variable long long and it still gives wrong answer. HERE’S the modified code.

Consider the test input:

1
13 6 8

Edit:

Even better:

1
3 6 6

I was making a few mistakes. Now, the sum is actually the sum of all the k digits. Now, the two test cases you provided give the right answers (first test case YES and second NO). But, I’m still getting the wrong answer. The code is HERE.

1
6 1 3

For CodeChef: Practical coding for everyone :

2
2 1 1
2 3 4

I corrected the code even further. I ran all the test cases that you have provided thus far. It looks like this:

5
13 6 8
3 6 6
6 1 3
2 1 1
2 3 4

The output of this input is correct and looks like this:

YES
NO
YES
NO
NO

I can’t understand what wrong with my code. I tried many test cases and all give the correct answer. There are also no overflows.

For CodeChef: Practical coding for everyone :

1
32 7 3

It works now, thanks. I was losing my mind over this.

1 Like