Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Solution {
- private static long[] arr;
- private static int max;
- public static void main(String[] args) {
- /* Enter your code here. Read input from STDIN. Prlong output to STDOUT. Your class should be named Solution. */
- Scanner scan = new Scanner(System.in);
- int q = scan.nextInt();
- int[] qer = new int[q];
- max = -1;
- while (q > 0) {
- int x = scan.nextInt();
- if(x > max){
- max = x;
- }
- qer[qer.length - q] = x;
- q--;
- }
- arr = new long[max + 1];
- for(int i = 2; i < arr.length ; i++){
- if(arr[i] == 0){
- arr[i] = solve(i);
- }
- }
- for(int i = 0; i < qer.length; i++) {
- max = 0;
- for(int j = 2; j <= qer[i]; j++){
- if(arr[j] >= arr[max]){
- max = j;
- }
- }
- System.out.println(max);
- }
- }
- public static long solve(long n){
- if(n == 1){
- return 0;
- }
- if(n % 2 == 0) {
- if(n > max){
- return solve(n/2) + 1;
- }
- else if(arr[(int)(n/2)] != 0){
- arr[(int)n] = arr[(int)(n/2)] + 1;
- }
- else
- arr[(int) n] = solve((n/2)) + 1;
- }
- else{
- if( (3*n) + 1 > max ){
- return solve((3*n) + 1) + 1;
- }
- else if(arr[(int)((3*n) + 1)] != 0 ){
- arr[(int)n] = arr[(int)((3*n) + 1)] + 1;
- }
- else
- arr[(int)n] = solve((3*n) + 1) + 1;
- }
- //System.out.println("number: " + n + " len: " + arr[n]);
- return arr[(int) n];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement