(HackerRank) Write a Modular C Programming code to solve Riping of Mangoes. There are some mangoes in a mango tree. Every mango is about to ripe in few days. Hari notes the riping level of the mango.
Given a non-negative number find the position of the right – most set bit (1) in the binary representation of the number.
Input Format
First line contains a non-negative number.
Constraints
N>0 and N<= (2^31-1)
Output Format
Display the bit number, where the right most set bit is found in a single line.
Sample Input 0
8
Sample Output 0
3
Explanation 0
8 in binary representation is 1000. We clearly see that the set bit from RHS is at position 3. (right most bit is at position 0.)
3 2 1 0 -----> Bit position
1 0 0 0 -----> The bits
Sample Input 1
-9
Sample Output 1
Invalid
CODE:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> struct node{ int data; struct node *link; }; typedef struct node *NODE; NODE create_node() { NODE newnode; newnode=malloc(sizeof(struct node)); scanf("%d",&newnode->data); newnode->link=newnode; return newnode; } NODE insert_end(NODE tail) { NODE newnode; newnode=create_node(); if(tail==NULL) { tail=newnode; } else{ newnode->link=tail->link; tail->link=newnode; tail=newnode; } return tail; } NODE remaining(NODE tail,int *count) { NODE prev=tail; NODE cur=tail->link; NODE next=cur->link; *count=0; do{ if(next->datadata) { prev->link=next; free(cur); cur=next; next=next->link; (*count)++; } else{ prev=cur; cur=next; next=next->link; } }while(cur!=tail); if(cur->data>next->data) { tail=prev; prev->link=next; free(cur); (*count)++; } return tail; } void display(NODE tail) { NODE cur=tail->link; do{ printf("%d ",cur->data); cur=cur->link; }while(cur!=tail->link); } int main() { NODE tail=NULL; int n; scanf("%d",&n); for(int i=0;i OUTPUT
Congratulations, you passed the sample test case. Click the Submit Code button to run your code against all the test cases. Input (stdin) 6 29 54 66 12 67 10 Your Output (stdout) 29 54 12 10 2 Expected Output 29 54 12 10 2
Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page: