We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My newbie approach to the solution for C code , took 3 hours of research.
intmain(){intn=parse_int(ltrim(rtrim(readline())));intbinArr[32],remainder,i=0,consecutive=1,maxconsecutive=1;// save binary values in binArr arrayfor(i=0;n>0;i++){binArr[i]=n%2;n=n/2;}// reverse the elements of the arrayfor(i=i-1;i>=0;i--){/* * check consecutive elements for "1" or "0" * if it's "1", increment consecutive and * if maxconsecutive is lesser than consecutive, * equal it so we have max number of consecutive. */if(binArr[i]==1&&binArr[i-1]==1){consecutive++;if(maxconsecutive<consecutive){maxconsecutive=consecutive;}/* * here, we are checking there are zeroes * for example 439 = 110110111 * 2 times there are 0 and 1's consecutive * so we reset consecutive counter */if((binArr[i-1]==1&&binArr[i-2]==0)||(binArr[i-1]==0&&binArr[i-2]==1)){consecutive=1;}}}printf("%d",maxconsecutive);return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →
My newbie approach to the solution for C code , took 3 hours of research.