-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrangeCounter.java
More file actions
56 lines (46 loc) · 1.52 KB
/
strangeCounter.java
File metadata and controls
56 lines (46 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Bob has a strange counter. At the first second, t=0, it displays the number 3. At each subsequent second,
the number displayed by the counter decrements by 1. The counter counts down in cycles. In the second after the counter
counts down to 1, the number becomes 2x the initial number for that countdown cycle and then continues counting down
from the new initial number in a new cycle. The diagram below shows the counter values for each time t in the first three cycles:
time value time value time value
1 3 4 6 10 12
2 2 5 5 11 11
3 1 6 4 12 10
7 3 13 9
8 2 14 8
9 1 15 7
16 6
17 5
18 4
19 3
20 2
21 1
Given a time, t, find and print the value displayed by the counter at time t.
Input Format
A single integer denoting the value of t.
Output Format
Print the value displayed by the strange counter at the given time t. */
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 i,j,prev=3,next=6,count=0;
while(t>count)
{
count = count + prev;
prev = next;
next = next*2;
}
return 1+count-t;
}
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();
}
}