Created
March 16, 2022 20:21
-
-
Save obasekiosa/a212547a674dcfcd107496e091b77a5a to your computer and use it in GitHub Desktop.
Solution to the Equal stack challenge on Hacker Rank (https://www.hackerrank.com/challenges/equal-stacks/problem)
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; | |
class Result { | |
/* | |
* Complete the 'equalStacks' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts following parameters: | |
* 1. INTEGER_ARRAY h1 | |
* 2. INTEGER_ARRAY h2 | |
* 3. INTEGER_ARRAY h3 | |
*/ | |
public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) { | |
// Write your code here | |
int sum1 = h1.stream().mapToInt(Integer::intValue).sum(); | |
int sum2 = h2.stream().mapToInt(Integer::intValue).sum(); | |
int sum3 = h3.stream().mapToInt(Integer::intValue).sum(); | |
int smallest = Math.min(sum1, Math.min(sum2, sum3)); | |
int index1 = 0; | |
int index2 = 0; | |
int index3 = 0; | |
// (sum1 != sum2 || sum2 != sum3) implies !(sum1 == sum2 && sum2 == sum3) i.e sum1 == sum2 == sum3 is false | |
while ((sum1 != sum2 || sum2 != sum3) && smallest != 0) { | |
if (sum1 != smallest) { | |
sum1 -= h1.get(index1++); | |
} | |
if (sum2 != smallest) { | |
sum2 -= h2.get(index2++); | |
} | |
if (sum3 != smallest) { | |
sum3 -= h3.get(index3++); | |
} | |
smallest = Math.min(sum1, Math.min(sum2, sum3)); | |
} | |
return smallest; | |
} | |
} | |
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"))); | |
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); | |
int n1 = Integer.parseInt(firstMultipleInput[0]); | |
int n2 = Integer.parseInt(firstMultipleInput[1]); | |
int n3 = Integer.parseInt(firstMultipleInput[2]); | |
List<Integer> h1 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) | |
.map(Integer::parseInt) | |
.collect(toList()); | |
List<Integer> h2 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) | |
.map(Integer::parseInt) | |
.collect(toList()); | |
List<Integer> h3 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) | |
.map(Integer::parseInt) | |
.collect(toList()); | |
int result = Result.equalStacks(h1, h2, h3); | |
bufferedWriter.write(String.valueOf(result)); | |
bufferedWriter.newLine(); | |
bufferedReader.close(); | |
bufferedWriter.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment