Scanner scanner = new Scanner(System.in); long x = scanner.nextLong(); if(x<=1) return; if(x>=Math.pow(10, 12)) return; Map<Integer, Long> y = new HashMap<Integer, Long>(); long k = 5; long yes = 0; for(int f = 0;f<x; f++){ y.put(f, k); k++; if(k==7){ k=1; } yes+= y.get(f); if(f == x-1){ System.out.println((long)yes); } } // for (int g = 0; g<y.size(); g++) { // yes+= y.get(g); // } System.out.println((long)yes); }}
The first time I was using ArrayList
instead of HashMap
. I changed it yet it keeps giving me the error. I even tried combining the loop as you can see from the commented out code.
The code works for the first few test cases but after it got into the 7+ digits (~2500000
) it starts timing me out.
Please go easy on me as I just started learning Java a few days ago but I have decent JavaScript knowledge.
Do provide me tips on how to increase code efficiency in the future if I ever run into this again.