1664. Ways to Make a Fair Array Medium
1/**
2 * [1664] Ways to Make a Fair Array
3 *
4 * You are given an integer array nums. You can choose exactly one index (0-indexed) and remove the element. Notice that the index of the elements may change after the removal.
5 * For example, if nums = [6,1,7,4,1]:
6 *
7 * Choosing to remove index 1 results in nums = [6,7,4,1].
8 * Choosing to remove index 2 results in nums = [6,1,4,1].
9 * Choosing to remove index 4 results in nums = [6,1,7,4].
10 *
11 * An array is fair if the sum of the odd-indexed values equals the sum of the even-indexed values.
12 * Return the number of indices that you could choose such that after the removal, nums is fair.
13 *
14 * Example 1:
15 *
16 * Input: nums = [2,1,6,4]
17 * Output: 1
18 * Explanation:
19 * Remove index 0: [1,6,4] -> Even sum: 1 + 4 = 5. Odd sum: 6. Not fair.
20 * Remove index 1: [2,6,4] -> Even sum: 2 + 4 = 6. Odd sum: 6. Fair.
21 * Remove index 2: [2,1,4] -> Even sum: 2 + 4 = 6. Odd sum: 1. Not fair.
22 * Remove index 3: [2,1,6] -> Even sum: 2 + 6 = 8. Odd sum: 1. Not fair.
23 * There is 1 index that you can remove to make nums fair.
24 *
25 * Example 2:
26 *
27 * Input: nums = [1,1,1]
28 * Output: 3
29 * Explanation: You can remove any index and the remaining array is fair.
30 *
31 * Example 3:
32 *
33 * Input: nums = [1,2,3]
34 * Output: 0
35 * Explanation: You cannot make a fair array after removing any index.
36 *
37 *
38 * Constraints:
39 *
40 * 1 <= nums.length <= 10^5
41 * 1 <= nums[i] <= 10^4
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/ways-to-make-a-fair-array/
47// discuss: https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn ways_to_make_fair(nums: Vec<i32>) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_1664() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.