-2
#include<stdio.h>
#include <stdlib.h>
int main()
{
    long long int a;
    int flag = 0;
    scanf("%lld", &a);
    if (a > 0)
    {
        while (a > 0)
        {
            if (a % 2 == 0 || a == 1)
            {
                a = a / 2;
                flag = 1;
            }
            else
            {
                flag = 0;
                break;
            }
        }
    }
    if (a < 0)
    {
        while (a <= -1)
        {
            if (a % 2 == 0 || a == -1)
            {
                a = a / 2;
                flag = 1;
            }
            else
            {
                flag = 0;
                break;
            }
        }
    }
    if (flag == 1)
    {
        printf("yes");
    }
    else
    {
        printf("no");
    }
}

click this for output image

Given an integer N, the program must determine if it is a power of 2 or -2. If N is a power of 2 or -2, the program must print yes. Else the program must print no.

Boundary Condition(s):

-10^17 <= N <= 10^17

Input Format:
The first line contains the value of N.

Output Format:
The first line contains either yes or no

For the input -4503599627370496 it should print no but it prints yes. Solution, please

  • 1
    Work it out by hand to see why you're getting the wrong answer (Try smaller numbers like -16 to keep it simple). Once you know that, coming up with a correct algorithm will be a lot easier. (So will knowing about logarithms, if you want a less complicated loop-free approach). – Shawn Jul 14 '18 at 08:58
  • it works for smaller values but for larger values like 16 digits ,it doesnt work, any data type problem? – Peniel Jacob Paul G Jul 14 '18 at 09:08
  • 1
    Your program prints yes for -16, when it should print no. The problem doesn't lie in the number of digits. Hint: Do you see a pattern in the results of the following expressions? `-2 * -2`, `-2 * -2 * -2`, `-2 * -2 * -2 * -2` and `-2 * -2 * -2 * -2 * -2` – Shawn Jul 14 '18 at 09:26
  • yeah its working now!! thanks for the logic – Peniel Jacob Paul G Jul 14 '18 at 09:41
  • A simple change that might fix things `while (a <= -1) { ... flag = 1; }` --> `while (a < -1) { ... flag = !flag; }` – chux - Reinstate Monica Jul 14 '18 at 13:38

2 Answers2

0

In case of negative numbers, you should also count which power of 2 is it. If it is even it won't work. -2 to the power 52 gives 4503599627370496 instead of -4503599627370496.

The code below solves this puzzle using bitwise shift operator:

#include <stdio.h>  
#include <stdlib.h>

int main()
{
    long long int a, aPositive, l = 1;
    scanf("%lld", &a);

    aPositive = a > 0 ? a : -a;

    int count = 0;
    while (l < aPositive)
    {
        l = l << 1;
        count++;
        //printf("%d\t%lld\n", count, l);
    }

    if (l == aPositive && (a > 0 || count % 2 == 1))
    {
        printf("yes");
    }
    else
    {
        printf("no");
    }

    return 0;
}
L_J
  • 2,351
  • 10
  • 23
  • 28
  • thanks you but how do you caught up with this logic....using shift operator ,i am a beginner in c ,difficult to think this way...any tips or websites to learn more about c? – Peniel Jacob Paul G Jul 14 '18 at 09:44
  • This is not really important here. bitwise shift `l = l << 1;` is just equivalent of this `l *= 2;` You can refer to [this](https://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work) to get more about bitwise shift operator. – L_J Jul 14 '18 at 09:51
  • 1
    `long long int l ... l = l << 1` is just equivalent of this `l *= 2` when `0 <= l <= LLONG_MAX/2` as it is here. Otherwise it is not specified the same. – chux - Reinstate Monica Jul 14 '18 at 13:29
0

Very similar problem has been solved here: how-to-check-if-a-number-is-a-power-of-2

bool IsPowerOfTwo(long long x)
{
    return (x != 0) && ((x & (x - 1)) == 0);
}

int main()
{
    bool result = IsPowerOfTwo(256);

    if (result )
    {
        printf("yes");
    }
    else
    {
        printf("no");
    }

    return 0;
}
ES2018
  • 438
  • 3
  • 15