Skip to content

Instantly share code, notes, and snippets.

@yoosinpaddy
Created July 1, 2022 21:15
Show Gist options
  • Save yoosinpaddy/8fb1e4f80ce00671caa6c3ffb93643ec to your computer and use it in GitHub Desktop.
Save yoosinpaddy/8fb1e4f80ce00671caa6c3ffb93643ec to your computer and use it in GitHub Desktop.
Ice Cream Parlor
package com.company;
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;
class Result {
/*
* Complete the 'icecreamParlor' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER m
* 2. INTEGER_ARRAY arr
*/
public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
List<Integer> ans = new ArrayList<>();
HashMap<Integer,Integer> hmap = new HashMap<>();
for(int i=0; i<arr.size(); i++){
if(hmap.containsKey(arr.get(i))){ //Contains key, return answer
ans.add( hmap.get(arr.get(i)) );
ans.add( i+1 );
break;
}
hmap.put(m-arr.get(i), i+1 ); //doesnt contain the key, add it
}
return ans;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = null;
try {
File f = new File("s.txt");
if (!f.exists()) {
System.out.println(f.createNewFile());
}
bufferedWriter = new BufferedWriter(new FileWriter(f.getName()));
} catch (IOException e) {
e.printStackTrace();
}
int t = Integer.parseInt(bufferedReader.readLine().trim());
BufferedWriter finalBufferedWriter = bufferedWriter;
IntStream.range(0, t).forEach(tItr -> {
try {
int m = Integer.parseInt(bufferedReader.readLine().trim());
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.icecreamParlor(m, arr);
finalBufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment