forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
63 lines (50 loc) · 1.28 KB
/
Solution.java
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
57
58
59
60
61
62
63
//Problem: https://www.hackerrank.com/challenges/strange-code
//Java 8
/*
Brute forces is to keep a counter and wait until it equals t and return our current value
int cycle = 3;
int step = 1;
int value = 3;
while(step != t)
{
if(value = 1)
{
cycle *= 2;
value = cycle;
}
value--;
step++;
}
System.out.println(value);
We can optimize by bounding our t then calculating
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long t = in.nextLong();
long upperBound = 4;
long lowerBound = 1;
long upperValue = 6;
//Find the bounds of t
while(t > upperBound)
{
lowerBound = upperBound;
upperBound = (upperBound+upperValue);
upperValue = upperBound + 2;
}
//When t is on a boundry
if(t == upperBound)
{
System.out.println(upperValue);
}
else
{
System.out.println(lowerBound+2 - (t-lowerBound));
}
}
}