Bit to Byte Practice Problem

What could be the possible error in the code mentioned here? I am just not able to get the correct answer.

#include <bits/stdc++.h>
using namespace std;

#define NEWLINE "\n";
typedef long long ll;


int main()
{
    ios::sync_with_stdio(false);

    ll T;
    cin >> T;

    ll N;

    for (ll i = 0; i < T; i++)
    {
        cin >> N;
        ll time = N;

        ll bit = 0, nibble = 0, byte = 0, num = 0;
        static ll byteArr[3];

        if(time <= 26)
        {
            if(time <= 2)
                bit++;
            else if(time <=10 && time > 2)
                nibble++;
            else if(time <= 26 && time > 10)
                byte++;
        }
        else if(time > 26)
        {
            if(time % 26 == 0){
                num = pow(2, time/26);
                byte = num;
            }
            else
            {
                ll remainder = time % 26;
                num = pow(2, (time - remainder)/26);

                if(remainder >= 0 && remainder <= 2)
                    bit += num;
                else if(remainder > 2 && remainder <= 10)
                    nibble += num;
                else byte = num;
            }
        }
        byteArr[0] = bit;
        byteArr[1] = nibble;
        byteArr[2] = byte;

        cout << byteArr[0] << " " << byteArr[1] << " " << byteArr[2] << NEWLINE;
    }

    return 0;
}

I have covered all the test cases, even the edge ones, and still am getting a wrong answer. What could be the possible error in the code above?

When I input 26 the code will output (0 0 1), for 130 (0 0 32), for 140 (0 32 0), and for 1600
(0 0 2305843009213693952). What am I missing here?

Hi, @kartavyasharma

I may be wrong, but I suspect your issue is here…

        if(time % 26 == 0){
            num = pow(2, time/26);
            byte = num;
        }

…when time % 26 is 0, for example when time == 26 or 52, num should NOT be 2^(time/26). Currently this formula calculates that there are 2 bytes at t=26 and 4 at t=52, not the correct answers 1 and 2, respectively.

Thanks for pointing that out, I could have completely missed that. Much appreciated @sbatten1969 !

Then accept this answer, no? @kartavyasharma

1 Like
//