Problem solution · Java

Count the Number of Incremovable Subarrays I

Count the Number of Incremovable Subarrays I: a Java solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Binary search
Source
walkccc LeetCode Solutions
Length
41 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Count the Number of Incremovable Subarrays I, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 41 lines of Java from the credited upstream file 2970.java.
  • The implementation visibly relies on sequence storage.
  • 2 loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeCount the Number of Incremovable Subarrays I · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int incremovableSubarrayCount(int[] nums) {    final int n = nums.length;    final int startIndex = getStartIndexOfSuffix(nums);    // If the complete array is strictly increasing, the total number of ways we    // can remove elements equals the total number of possible subarrays.    if (startIndex == 0)      return n * (n + 1) / 2;     // The valid removals starting from nums[0] include nums[0..startIndex - 1],    // nums[0..startIndex], ..., nums[0..n).    int ans = n - startIndex + 1;     // Enumerate each prefix subarray that is strictly increasing.    for (int i = 0; i < startIndex; ++i) {      if (i > 0 && nums[i] <= nums[i - 1])        break;      // Since nums[0..i] is strictly increasing, find the first index j in      // nums[startIndex..n) such that nums[j] > nums[i]. The valid removals      // will then be nums[i + 1..j - 1], nums[i + 1..j], ..., nums[i + 1..n).      ans += n - firstGreater(nums, startIndex, nums[i]) + 1;    }     return ans;  }   // Returns the start index i of the suffix subarray such that nums[i..n) is  // strictly increasing.  private int getStartIndexOfSuffix(int[] nums) {    for (int i = nums.length - 2; i >= 0; --i)      if (nums[i] >= nums[i + 1])        return i + 1;    return 0;  }   private int firstGreater(int[] arr, int startIndex, int target) {    final int i = Arrays.binarySearch(arr, startIndex, arr.length, target + 1);    return i < 0 ? -i - 1 : i;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗