Advertisement

Three Equal Parts - LeetCode 927 Solution

Three Equal Parts - Complete Solution Guide

Three Equal Parts is LeetCode problem 927, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array arr which consists of only zeros and ones, divide the array into three non-empty parts such that all of these parts represent the same binary value. If it is possible, return any [i, j] with i + 1 < j , such that: arr[0], arr[1], ..., arr[i] is the first part, arr[i + 1], arr[i + 2], ..., arr[j - 1] is the second part, and arr[j], arr[j + 1], ..., arr[arr.length - 1] is the third part. All three parts have equal binary values. If it is not possible, return [-1, -1] . Note

Detailed Explanation

The problem asks us to divide a binary array `arr` into three non-empty parts such that each part represents the same binary value (decimal equivalent). Leading zeros are allowed, meaning `[0,1,1]` and `[1,1]` are considered equal. We need to find indices `i` and `j` (where `i + 1 < j`) that define the boundaries of the three parts: `arr[0...i]`, `arr[i+1...j-1]`, and `arr[j...arr.length-1]`. If a valid split is possible, return `[i, j]`; otherwise, return `[-1, -1]`.

Solution Approach

The solution first counts the number of ones in the array. If this count is not divisible by 3, the problem is unsolvable. If the count is 0, any split will work (in this case, the solution provides a default split). Otherwise, we find the indices of the first one in each of the three parts (assuming the array can be divided into three equal parts). The third part starting from its first '1' is used as a template or 'pattern', and its length is determined. Then, the code verifies whether the first and second parts match this pattern by comparing their corresponding segments. If all parts match, the indices `i` and `j` are calculated based on the pattern's length and are returned.

Step-by-Step Algorithm

  1. Step 1: Count the number of ones in the array.
  2. Step 2: If the number of ones is not divisible by 3, return `[-1, -1]`. If the number of ones is zero, return `[0, 2]`.
  3. Step 3: Calculate the number of ones each part should have (total_ones / 3).
  4. Step 4: Find the start indices (s1, s2, s3) of the three parts based on the indices of '1's.
  5. Step 5: Extract the 'pattern' or canonical representation using the last part (starting from s3).
  6. Step 6: Compare the pattern with the segments of array starting from s1 and s2, return `[-1, -1]` if any of them does not match.
  7. Step 7: Calculate the end index `i` of the first part and the start index `j` of the third part using the length of the pattern.
  8. Step 8: Return the indices `[i, j]`.

Key Insights

  • Insight 1: The most important observation is that all three parts must have the same number of ones. If the total number of ones is not divisible by 3, a split is impossible.
  • Insight 2: After identifying the number of ones each part should have, the core logic involves finding a 'pattern' or canonical representation of the equal part. The third part is used as this pattern.
  • Insight 3: After finding the indices for the end of the first part and the start of the third part based on the pattern, the leading zeros or the 'tailing zeros' after each of these three parts is crucial to determine if the segments are really same.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Math.

Companies

Asked at: Hotstar.