We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*
* Complete the 'organizingContainers' function below.
*
* The function is expected to return a STRING.
* The function accepts 2D_INTEGER_ARRAY container as parameter.
*/
public static String organizingContainers(List<List<Integer>> container) {
int n = container.size();
int[] containerCapacity = new int[n];
int[] typeCount = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
containerCapacity[i] += container.get(i).get(j); // total balls in container i
typeCount[j] += container.get(i).get(j); // total balls of type j
}
}
Arrays.sort(containerCapacity);
Arrays.sort(typeCount);
return Arrays.equals(containerCapacity, typeCount) ? "Possible" : "Impossible";
}
}
public class Solution {
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")));
int q = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, q).forEach(qItr -> {
try {
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<List<Integer>> container = new ArrayList<>();
IntStream.range(0, n).forEach(i -> {
try {
container.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList())
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
String result = Result.organizingContainers(container);
try {
bufferedWriter.write(result);
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Organizing Containers of Balls
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.util.stream.*; import static java.util.stream.Collectors.toList;
class Result {
}
public class Solution { 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")));
}