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.
publicstaticlongstrangeCounter(longt){// Write your code here/* * Idea: - The t must be inside a segment of 3 elements. - So that, t is in the (floor(t-1)/3 + 1) th segment. - The segment is in a specific cycle. - Identify that cycle and calculate the result. * Steps: - Calculate the position of the segment. - The pos (position) satisfies: 2^(k-1) <= pos <= 2^k - 1 with k is the kth cycle. - Identify the position of t in the cycle. *Time complexity: O(log t) */longsegment=(t-1)/3+1;//Find cycleintcycle=0;while(segment>0){segment/=2;cycle++;}//Find the index in the cycle, index is t minus to total time of previous cycleslongindex=t-3*(long)Math.pow(2,cycle-1)+3;return3*(long)Math.pow(2,cycle-1)-index+1;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Strange Counter
You are viewing a single comment's thread. Return to all comments →
My solution in Java: