Showing posts with label Hackerrank. Show all posts
Showing posts with label Hackerrank. Show all posts

Monday, August 18, 2014

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game solution

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game: https://www.hackerrank.com/contests/infinitum-aug14/challenges/stepping-stones-game

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game editorial: https://www.hackerrank.com/contests/infinitum-aug14/challenges/stepping-stones-game/editorial

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game solution: 


#include <iostream>
#include <cstdio>
#include <cmath>

int main() {
    int t;
    long long int n, a;
    scanf("%d", &t);
    while(t--) {
        scanf("%lld", &n);
        if(floor(sqrt(1+8*n))==sqrt(1+8*n)) a=(sqrt(1+8*n)-1)/2, printf("Go On Bob %lld\n", a);
        else printf("Better Luck Next Time\n");
    }
    return 0;
}

note: The formular for the sum of the first 
n natural numbers is
n(n+1)2
A number x appears in this sequence, iff
n(n+1)2=xn2+n2x=0
has an non-negative integer solution. Since, this is a quadratic equation, you can simply compute the positive solution:
n=1+8x12
This is an integer number, iff 1+8x is a square and 21+8x1. You can see, that the latter is always true, if the former is true. So the answer is:
x occurs in this sequence, if 1+8x is a perfect square.

ref: http://math.stackexchange.com/questions/466649/how-do-i-test-if-a-number-x-is-a-sum-of-consecutive-natural-numbers

Hackerrank Ad Infinitum August 2014 - Reverse Game solution

Hackerrank Ad Infinitum August 2014 - Reverse Game: https://www.hackerrank.com/contests/infinitum-aug14/challenges/reverse-game

Hackerrank Ad Infinitum August 2014 - Reverse Game editorial: https://www.hackerrank.com/contests/infinitum-aug14/challenges/reverse-game/editorial

Hackerrank Ad Infinitum August 2014 - Reverse Game solution: 


#include <iostream>
#include <cstdio>

int main() {
    int n, k, t, cnt, a;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &k);
        if(k<(n-1)/2) {
            cnt=0, a=2;
            while(cnt<k) a+=2, cnt++;
        }
        else {
            cnt=n-1, a=1;
            while(cnt>k) a+=2, cnt--;
        }
        printf("%d\n", a-1);
    }
    return 0;
}

Friday, August 15, 2014

Hackerrank Weekly - Week 8 - Counter Game solution

Hackerrank Weekly - Week 8 - Counter Game: https://www.hackerrank.com/contests/w8/challenges/counter-game

Hackerrank Weekly - Week 8 - Counter Game editorial: https://www.hackerrank.com/contests/w8/challenges/counter-game/editorial

Hackerrank Weekly - Week 8 - Counter Game solution: 


#include <iostream>
#include <cstdio>

int main() {
    long long unsigned int n, tmp;
    int cnt, t;
    scanf("%d", &t);
    while(t--) {
        scanf("%llu", &n), tmp=n;
        cnt=0;
        while(tmp) tmp&=tmp-1, cnt++;
        cnt--;
        while((n&1)==0) n>>=1, cnt++;
        if(cnt%2==0) printf("Richard\n");
        else printf("Louise\n");
    }
    return 0;
}

and my stackoverflow question to the difference between ampersand and equal to signs in c++  & and == operators: http://stackoverflow.com/questions/25256749/difference-between-n1-and-n1

Monday, August 11, 2014

Hackerrank Weekly challenges Week 8 - John and GCD list solution

Hackerrank Weekly challenges Week 8 - John and GCD list: https://www.hackerrank.com/contests/w8/challenges/john-and-gcd-list

Hackerrank Weekly challenges Week 8 - John and GCD list solution: http://ideone.com/QZZxNC


#include <iostream>
#include <cstdio>

int gcd(int u, int v) {
    if(u==v) return u;
    if(u==0) return v;
    if(v==0) return u;
    if(~u&1) {
        if(v&1) return gcd(u>>1, v);
        return gcd(u>>1, v>>1)<<1;
    }
    if(~v&1) return gcd(u, v>>1);
    if(u>v) return gcd((u-v)>>1, v);
    return gcd((v-u)>>1, u);
}

int lcm(int u, int v) {
    return u*v/gcd(u, v);
}

int main() {
    int t, n, a[1005], b[1005];
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(int i=0; i<n; i++) scanf("%d", &a[i]);
        b[0]=a[0];
        for(int i=1; i<n; i++) b[i]=lcm(a[i-1], a[i]);
        b[n]=a[n-1];
        for(int i=0; i<=n; i++) printf("%d ", b[i]);
        printf("\n");
    }
    return 0;
}


Thursday, July 24, 2014

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 solution

Hackerrank Weekly Challenges - Week 7 - Die Hard 3: https://www.hackerrank.com/contests/w7/challenges/die-hard-3

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 editorial: http://chasethered.com/2014/07/hackerrank-weekly-challenge-7-problem-1-die-hard-3/ &
https://www.hackerrank.com/contests/w7/challenges/die-hard-3/editorial

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 solution: http://ideone.com/FET4JY

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

int gcd(int u, int v) {
    if(u==v) return u;
    if(v==0) return u;
    if(u==0) return v;
    if(~u&1) {
        if(v&1) return gcd(u>>1, v);
        return gcd(u>>1, v>>1)<<1;
    }
    if(~v&1) return gcd(u, v>>1);
    if(u>v) return gcd((u-v)>>1, v);
    return gcd((v-u)>>1, u);
}

int main() {
    int t, a, b, c;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &a, &b, &c);
        if(c%gcd(a, b)==0 && c<=max(a, b)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

Tuesday, July 15, 2014

Hackerrank Ad Infinitum July 14 - Sherlock and Divisors solution

Hackerrank Ad Infinitum July 14 - Sherlock and Divisors:  https://www.hackerrank.com/contests/infinitum-jul14/challenges/sherlock-and-divisors

Hackerrank Ad Infinitum July 14 - Sherlock and Divisors editorial: https://www.hackerrank.com/contests/infinitum-jul14/challenges/sherlock-and-divisors/editorial

Hackerrank Ad Infinitum July 14 - Sherlock and Divisors solution: http://ideone.com/0irtP6


#include <iostream> #include <cstdio> using namespace std; int main() { int t, n, cnt; scanf("%d", &t); while(t--) { cnt=0; scanf("%d", &n); for(int i=1; i*i<=n; i++) if(n%i==0) { if(i%2==0) cnt++; if(i*i!=n && n/i%2==0) cnt++; } printf("%d\n", cnt); } return 0; }

Solution version 2: http://ideone.com/mNrA8K

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

int main() {
    int t, n, cnt;
    scanf("%d", &t);
    while(t--) {
        cnt=0;
        scanf("%d", &n);
        for(int i=1; i*i<=n; i++) if(n%i==0) {
            if(~i & 1) cnt++;
            if(i*i!=n && ~(n/i)&1) cnt++;
        }
        printf("%d\n", cnt);
    }
    return 0;
}