Created
April 23, 2022 14:43
-
-
Save SuvidhaMalaviya/1c10d75407ace925e79df87b86eb063f to your computer and use it in GitHub Desktop.
Alexa has two stacks of non-negative integers, stack and stack where index denotes the top of the stack.
This file contains 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 GameOfTwoStackResult { | |
/* | |
* Complete the 'twoStacks' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts following parameters: | |
* 1. INTEGER maxSum | |
* 2. INTEGER_ARRAY a | |
* 3. INTEGER_ARRAY b | |
*/ | |
public static int twoStacks(int maxSum, List<Integer> a, List<Integer> b) { | |
int sum = 0; | |
int count = 0; | |
int i = 0; | |
int j = 0; | |
while (i < a.size() && (sum + a.get(i)) <= maxSum) { | |
sum += a.get(i); | |
i++; | |
} | |
count = i; | |
while (j < b.size() && i >= 0) { | |
sum += b.get(j); | |
j++; | |
while (sum > maxSum && i > 0) { | |
i--; | |
sum -= a.get(i); | |
} | |
if (sum <= maxSum && (i + j) > count) | |
count = i + j; | |
} | |
//System.out.println("count "+count); | |
return count; | |
} | |
} | |
public class GameOfTwoStack { | |
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 g = Integer.parseInt(bufferedReader.readLine().trim()); | |
IntStream.range(0, g).forEach(gItr -> { | |
try { | |
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); | |
int n = Integer.parseInt(firstMultipleInput[0]); | |
int m = Integer.parseInt(firstMultipleInput[1]); | |
int maxSum = Integer.parseInt(firstMultipleInput[2]); | |
List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) | |
.map(Integer::parseInt) | |
.collect(toList()); | |
List<Integer> b = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) | |
.map(Integer::parseInt) | |
.collect(toList()); | |
int result = GameOfTwoStackResult.twoStacks(maxSum, a, b); | |
bufferedWriter.write(String.valueOf(result)); | |
bufferedWriter.newLine(); | |
} 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