Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int minStartValue(int[] arr) {
- int left = 1;
- int right = Integer.MAX_VALUE;
- while (left < right) {
- int mid = left + (right - left) / 2;
- if (isValid(arr, mid)) {
- right = mid;
- } else {
- left = mid + 1;
- }
- }
- return left;
- }
- private static boolean isValid(int[] arr, int x) {
- int sum = x;
- for (int num : arr) {
- sum += num;
- if (sum < 1)
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement