Advertisement
rajeshinternshala

Untitled

Feb 25th, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.60 KB | None | 0 0
  1.    public static int minStartValue(int[] arr) {
  2.         int left = 1;
  3.         int right = Integer.MAX_VALUE;
  4.  
  5.         while (left < right) {
  6.             int mid = left + (right - left) / 2;
  7.  
  8.             if (isValid(arr, mid)) {
  9.                 right = mid;
  10.             } else {
  11.                 left = mid + 1;
  12.             }
  13.         }
  14.  
  15.         return left;
  16.     }
  17.  
  18.     private static boolean isValid(int[] arr, int x) {
  19.         int sum = x;
  20.  
  21.         for (int num : arr) {
  22.             sum += num;
  23.             if (sum < 1)
  24.                 return false;
  25.         }
  26.  
  27.         return true;
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement