You are viewing a single comment's thread. Return to all comments →
It keeps timing out. I understand my solution is cubic but how do i get it faster.... :(
Here is my solution:
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;
public class Solution {
// Complete the countTriplets function below. static long countTriplets(List<Long> arr, long r) { long result = 0; Map<Long, Map<Long, Long>> triplet = new HashMap<>(); Map<Long, Long> pairProgression = new HashMap<>(); Map<Long, Long> placeHolder = new HashMap<>(); long pH = -1000; long secondValue = -1000; long thirdValue = -1000; triplet.put(pH, placeHolder); for(long i = 0; i < arr.size(); i++){ secondValue = -1000; thirdValue = -1000; int firstValueIndex = (int) i; long firstArrayValue = arr.get(firstValueIndex); //System.out.print("First Values : "+firstArrayValue); for(long j = i + 1; j < arr.size() ; j++){ int secondValueIndex = (int) j; long secondArrayValue = arr.get(secondValueIndex); long secondValueExpected = firstArrayValue * r; if(secondArrayValue == secondValueExpected ) { for(long k = j + 1; k < arr.size(); k++){ int thirdValueIndex = (int) k; long thirdArrayValue = arr.get(thirdValueIndex); long thirdValueExpected = secondArrayValue * r; // System.out.println(firstArrayValue); // System.out.println(secondArrayValue+" "+secondValueExpected); // System.out.println(thirdArrayValue+" "+thirdValueExpected); // System.out.println(" "); if(secondArrayValue == secondValueExpected && thirdArrayValue == thirdValueExpected){ Map<Long, Long> pair = new HashMap<>(); pair.put( j, k); triplet.put(i, pair); result++; } } } } } // For iterating through array - IK // for (Map.Entry<Long, Map<Long, Long>>entry : triplet.entrySet()) { // Long key = entry.getKey(); // System.out.println(key); // } //System.out.println(r); return result; } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String[] nr = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); int n = Integer.parseInt(nr[0]); long r = Long.parseLong(nr[1]); List<Long> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Long::parseLong) .collect(toList()); long ans = countTriplets(arr, r); bufferedWriter.write(String.valueOf(ans)); bufferedWriter.newLine(); bufferedReader.close(); bufferedWriter.close(); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Count Triplets
You are viewing a single comment's thread. Return to all comments →
It keeps timing out. I understand my solution is cubic but how do i get it faster.... :(
Here is my solution:
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;
public class Solution {
}