Strange Counter
Hacker Rank Solution
Strange Counter
Hey guys ,welcome to another blog on hacker rank solution. this time we gonna discuss a hacker rank algorithim problem, which is named as Strange Counter
Dashboard> Algorithms> Implementation >Strange Counter
Solution:
This one is little tricky question , After a close observation one will find it easy. the first solution arises in mind is by iterating through for loop till i==t and setting intital-value to 1 and count=3 and decreasing the value of "vount" by 1 at each iteration and as the value of "count" equals to 1 , we will double the initial-value and will initialise count to initial value.
But here a problem arises , which is time complexity , iteratinf through each value will give time out because remember t can vary from t=1 to t= 10 to the power 12.
So another way of solving this is by jumping through each cycle rather than iterating through each time t and checking if the given time "t" falls under the current cycle, if yes, then we can easily find the answer by initial_value-=(t-i);
Here is my java code :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static long strangeCode(long t) {
long initial_value=3;
for(long i=1;i<=t;i=+initial_value,initial_value*=2)
{
if(t>=i&&t<i+initial_value)
{
initial_value-=(t-i);
break;
}
}
return initial_value;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long t = in.nextLong();
long result = strangeCode(t);
System.out.println(result);
in.close();
}
}
If you liked my Explaination ,dont forget to subscribe to the blog.
Follow me on social media
Facebook ,Instagram .
Comments
Post a Comment