Saturday, 1 April 2017

Hacker Rank Solution For Day 10: Binary Numbers

Problem:- Write a Hackerrank Solution For Day 10: Binary Numbers or Hacker Rank Solution Program In C++ For " Day 10: Binary Numbers " or Hackerrank 30 days of code Java Solution: Day 10: Binary Numbers solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 10: Binary Numbers solution, or C/C++ Logic & Problem Solving: Day 10: Binary Numbers.


Hacker Rank Solution For Day 10: Binary Numbers

Logic:- For Day 10: Binary Numbers solution is divided into two part one is a binary number and second is consecutive 1's. In the problem, we can see that we have to find that maximum number consecutive 1's. in a Number. Means of consecutive number is continuing same number repeat maximum times. Below is the explanation of consecutive 1's with an example.

Explanation:- For this problem, we are taking some number and also find the binary of all number after that we can find the maximum consecutive 1's, take an example with a number between 1 to 15 and also write their binary number.

Number    binary    Maximum consecutive 1's
0                0000         0
1                0001         1
2                0010         1
3                0011         2
4                0100         1
5                0101         1
6                0110         2
7                0111         3
8                1000         1
9                1001         1
10              1010         1
11              1011         2
12              1100         2
13              1101         2
14              1110         3
15              1111         4

As we can see that maximum consecutive 1's in binary number are {15(4 consecutive 1's), (14, 7(consecutive 1's)), (3, 6, 11, 12, 13(consecutive 1's)), (1, 2, 4, 5, 8, 9, 10(1 consecutive 1's)), 0 (0 consecutive 1's). And here n & 1 produces a value that is either 1 or 0, depending on the least significant bit of. This is a bitwise AND operation. or 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.

Note:- We can see that maximum consecutive 1's and maximum consecutive 0's may be less and equal to binary number digit(maximum digit in binary number).

All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or sends a mail with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation.

Important:- If any readers want to write a post for me or for our Programming community please click here. You have a better solution and you want to publish your solution send the solution I will publish your post / Article with your name and one more thing you can add 2 backlinks + you can earn credits, this will help you if you have any website and blog. 

Recently Updated Post:-

1. Hacker Rank Solution For Day 5: Loops

2. Hackerrank Solution For Day 6: Let's Review

3. Hackerrank Solution For Day 7: Arrays

4. Hacker Rank Solution For Day 8: Dictionaries and Maps

5. Hacker Rank Solution For Day 9: Recursion


Tip:- Always try to implement the own logic this will help you to solve and building a logic. Before copy the program I recommended please read this full article, this will help you to build your own logic.

Submit Your Solution Here:- Click Here

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 For Day 10: Binary Numbers

You May Also Like:-


2. Hackerrank Solution For Day 12: Inheritance

3. Hackerrank Solution For Day 13: Abstract Classes

4. Hackerrank Solution For Day 14: Scope

5. Hackerrank Solution For Day 15: Linked List

6. Hackerrank Solution For Day 16: Exceptions - String to Integer

7. Hackerrank Solution For Day 17: More Exceptions

8. Hackerrank Solution For Day 18: Queues and Stacks

9. Hackerrank Solution For Day 19: Interfaces

10. Hackerrank Solution For Day 20: Sorting

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

5 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
  2. Replies
    1. Hello,

      Here is your code in C language, Next time if you want any code please tell us, I feel happy to help. Don't forgot to share

      Code:-


      #include
      #include
      #include
      #include
      #include
      #include
      #include

      int main(){
      int n,count=0,max=0;
      scanf("%d",&n);

      while(n)
      {
      if (n&1)
      count++;
      else
      count = 0;
      if (max < count)
      max = count;
      n>>=1;
      }
      printf("%d",max);
      return 0;
      }


      Stay cool and healthy

      Delete
  3. With Binary META trading, an investor is able to not only bet on the future value of a particular investment but also double the bet mid-way through the day.Get More Info

    ReplyDelete