HackerRank Solutions – 30 Days of Code – Day 10

HackerRank Solutions – 30 Days of Code – Day 10

 

Objective
Today, we’re working with binary numbers.

Task
Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1‘s in n‘s binary representation.

Input Format

A single integer,n.

Constraints

  • 1<=n<=10^6 .

Output Format

Print a single base-10 integer denoting the maximum number of consecutive 1’s in the binary representation of n .

Sample Input 1

5

Sample Output 1

1

Sample Input 2

13

Sample Output 2

2

Explanation

Sample Case 1:
The binary representation of 5 is 101, so the maximum number of consecutive 1’s is 1.

Sample Case 2:
The binary representation of 13 is 1101, so the maximum number of consecutive 1’s is 2.

PROGRAM :

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int n,c=0,max=0;
scanf(“%d”,&n);
while(n)
{
if(n&1)
c++;
else
c=0;
if(max<c)
max=c;
n>>=1;
}
printf(“%d”,max);
return 0;
}

Output:

Congratulations
You solved this challenge.
Compiler Message
Success
Input (stdin)
5
Expected Output
1
Follow us on Social Media:
People find me to be an upbeat, self-motivated team player with excellent communication skills”.
I am NITHIESH KUMAR, pursuing my BE CSE from the reputed college ,SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY.
 I have an idea in the technology industry. My experience includes successfully known positions of technology departments and developing viable leads. I have a track record of maintaining a consistent and activity volume and consistently achieving the goal.

Be the first to comment

Leave a Reply

Your email address will not be published.