Friday, April 22, 2016

SPOJ Problem DOL

LARGEST ODD DIVISOR

Explanation:

Simple problem. Requires just pen and paper to come up with a solution, or not even that. Coming to the point though. For an odd number 'N', answer will be 'N'. Now, for an even integer, we simply need to keep on dividing by 2 until the quotient is even, and thus, removing the even factors from the number.

Solution:

#include <iostream>
#include <cstdio>
using namespace std;

typedef long long int LLD;

LLD findLargestOddDivisor(LLD n){
    while(n % 2 == 0){
        n >>= 1;
    }
    return n;
}

int main() {
    LLD t, n, cas=0;
    scanf("%lld", &t);
    while(t--){
        scanf("%lld", &n);
        printf("Case %lld: %lld\n", ++cas, findLargestOddDivisor(n));
    }
    return 0;
}

Any suggestions are welcome.

No comments:

Post a Comment

SPOJ Problem ONEZERO

Ones and zeros Explanation: A very good problem to practice BFS skills. I think that much hint is good to give the problem a shot. An...