Last active
December 7, 2020 06:43
-
-
Save benwaffle/8fdee019d9b268321b00f818ba002de1 to your computer and use it in GitHub Desktop.
hackerrank triplets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 total = 0; | |
Map<Long, Long> count = new HashMap<>(); // count of ints | |
Map<Long, Long> tuplets = new HashMap<>(); // map 2nd -> count of links | |
for (int i = 0; i < arr.size(); ++i) { | |
//System.out.printf("i=%d\n", i); | |
long val = arr.get(i); | |
// inc total | |
if (val % r == 0 && tuplets.containsKey(val / r)) { | |
total += tuplets.get(val / r); | |
//System.out.printf("%dx %d -> %d -> %d\n", t.count, t.first, val / r, val); | |
} | |
// create links/tuplets | |
if (tuplets.containsKey(val)) { | |
tuplets.put(val, tuplets.get(val) + count.get(val/r)); | |
//System.out.printf("%dx %d -> %d\n", tuplets.get(val).count, tuplets.get(val).first, val); | |
} else if (val % r == 0 && count.containsKey(val / r)) { | |
//System.out.printf("%dx %d -> %d\n", count.get(val/r), val/r, val); | |
tuplets.put(val, count.get(val / r)); | |
} | |
// inc count | |
if (count.containsKey(val)) | |
count.put(val, count.get(val)+1); | |
else | |
count.put(val, 1L); | |
} | |
return total; | |
} | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment