FastPrepPivot Index After Exactly One Removal
Problem · Array

Pivot Index After Exactly One Removal

Learn this problem
MediumGoogle logoGoogleNEW GRADONSITE INTERVIEW
See Google hiring insights

Problem statement

Given an integer array nums, remove exactly one element by index. The remaining elements keep their relative order.

A pivot index in the remaining array is an index whose strictly-left sum equals its strictly-right sum; the pivot value belongs to neither sum.

Return true if at least one removal leaves a nonempty array with a pivot index. Otherwise return false.

Function

canCreatePivot(nums: int[]) → boolean

Examples

Example 1

nums = [1,2,3]return = false

Removing any one value leaves a two-element array with unequal nonzero values. Neither index can have equal left and right sums.

Example 2

nums = [2,1,1,2,2]return = true

Remove the first 2. The remaining array is [1,1,2,2]; at pivot index 2, the left sum is 1 + 1 = 2 and the right sum is 2.

Example 3

nums = [4,1]return = true

Removing either element leaves a one-element array. Its only index has empty left and right sides, both summing to zero.

Constraints

  • 1 <= nums.length <= 200000
  • -1000000000 <= nums[i] <= 1000000000
  • Use signed 64-bit arithmetic for sums.

More Google problems

drafts saved locally
public boolean canCreatePivot(int[] nums) {
    // Write your code here.
}
nums[1,2,3]
expectedfalse
checking account