|
| 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 | + */ |
| 44 | +pub 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 | + |
| 51 | +impl Solution { |
| 52 | + pub fn ways_to_make_fair(nums: Vec<i32>) -> i32 { |
| 53 | + let n = nums.len(); |
| 54 | + let mut odds = vec![0; n + 1]; |
| 55 | + let mut evens = vec![0; n + 1]; |
| 56 | + |
| 57 | + for i in 0..n { |
| 58 | + if i % 2 == 0 { |
| 59 | + odds[i + 1] = odds[i]; |
| 60 | + evens[i + 1] = nums[i] + evens[i]; |
| 61 | + } else { |
| 62 | + odds[i + 1] = nums[i] + odds[i]; |
| 63 | + evens[i + 1] = evens[i]; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + let mut result = 0; |
| 68 | + |
| 69 | + for i in 0..n { |
| 70 | + let ov = odds[i] - odds[0] + evens[n] - evens[i]; |
| 71 | + let ev = evens[i] - evens[0] + odds[n] - odds[i]; |
| 72 | + if i % 2 == 0 { |
| 73 | + if ov - nums[i] == ev { |
| 74 | + result += 1; |
| 75 | + } |
| 76 | + } else { |
| 77 | + if ov == ev - nums[i] { |
| 78 | + result += 1; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + result |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// submission codes end |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use super::*; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_1664_example_1() { |
| 95 | + let nums = vec![2, 1, 6, 4]; |
| 96 | + |
| 97 | + let result = 1; |
| 98 | + |
| 99 | + assert_eq!(Solution::ways_to_make_fair(nums), result); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_1664_example_2() { |
| 104 | + let nums = vec![1, 1, 1]; |
| 105 | + |
| 106 | + let result = 3; |
| 107 | + |
| 108 | + assert_eq!(Solution::ways_to_make_fair(nums), result); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_1664_example_3() { |
| 113 | + let nums = vec![1, 2, 3]; |
| 114 | + |
| 115 | + let result = 0; |
| 116 | + |
| 117 | + assert_eq!(Solution::ways_to_make_fair(nums), result); |
| 118 | + } |
| 119 | +} |
0 commit comments