Saturday, 1 April 2017

Hacker Rank Solution Program In C++ For " Day 10: Binary Numbers "

Problem :- Day 10: Binary Numbers

Submit Your Solution Here :- Click Here

Copy The Color Code and paste it into hackerrank Editor .

Solution :-

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main()
{
    int n,count=0,max=0;
    cin >> n;
    while(n)
    {
        if (n&1)
            count++;
        else 
            count = 0;
        if (max < count)
            max = count;
        n>>=1;
    }
    cout << max;
    return 0;
}

Output :-

Hacker Rank Solution Program In C++ For " Day 10: Binary Numbers "

2 comments:
Write comments
  1. i didnt understand (n&1),n>>=1.can you explain these statements???

    ReplyDelete
    Replies
    1. n & 1 produces a value that is either 1 or 0, depending on the least significant bit of .This is a bitwise AND operation.

      n >>= 1 means set n to itself shifted by one bit to the right. The expression evaluates to the new value of x after the shift.

      Delete

Recommended Posts × +