Solve Two Array Optimization Problems
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
You are given two coding problems from the same interview round.
**Problem 1: Split a gold chain fairly**
An array `weights` represents the weights of consecutive links in a gold chain.
You must perform the following steps:
1. Remove exactly one link.
2. Reconnect the remaining links, preserving their relative order.
3. Cut the reconnected chain once so that it becomes two non-empty contiguous parts.
Return `true` if there exists a choice of removed index `r` and cut position `k` in the new array such that the two resulting parts have the same total weight. Otherwise, return `false`.
Formally, after removing `weights[r]`, the new array is:
- `weights[0], weights[1], ..., weights[r-1], weights[r+1], ..., weights[n-1]`
You need to determine whether there exists a cut position `k` in this new array such that:
- sum of elements from index `0` to `k` equals the sum of elements from `k+1` to the end.
**Follow-up:** Return all valid `(r, k)` pairs, where `r` is the removed index in the original array and `k` is the cut index in the array after removal.
---
**Problem 2: Maximize a subarray whose endpoints are equal**
Given an integer array `a`, find indices `i` and `j` such that:
- `0 <= i <= j < n`
- `a[i] == a[j]`
- the value of `a[i] + a[i+1] + ... + a[j]` is maximized
Return one pair `(i, j)` that achieves the maximum sum.
**Follow-up:** Solve the problem using strictly `O(1)` extra space, even if a higher time complexity is allowed.
Quick Answer: This pair of problems evaluates array manipulation, partitioning, and sum-optimization competencies, focusing on handling element removals, contiguous partitions, and maximizing subarray sums.
Part 1: Split a Gold Chain Fairly
You are given an integer array `weights` representing the weights of consecutive links in a gold chain. Remove exactly one link, reconnect the remaining links without changing their relative order, and then cut the new chain once so that it becomes two non-empty contiguous parts. Return `True` if there exists some removed index `r` and cut index `k` in the new array such that the two parts have the same total weight. Otherwise, return `False`.
Constraints
- 0 <= len(weights) <= 2000
- -10^9 <= weights[i] <= 10^9
Examples
Input: ([1, 2, 1],)
Expected Output: True
Explanation: Remove index 1 to get [1, 1]. Cutting after k = 0 gives two parts with sums 1 and 1.
Input: ([1, 3, 2, 2],)
Expected Output: False