|
| 1 | +/** |
| 2 | + * [1968] Array With Elements Not Equal to Average of Neighbors |
| 3 | + * |
| 4 | + * You are given a 0-indexed array nums of distinct integers. You want to rearrange the elements in the array such that every element in the rearranged array is not equal to the average of its neighbors. |
| 5 | + * More formally, the rearranged array should have the property such that for every i in the range 1 <= i < nums.length - 1, (nums[i-1] + nums[i+1]) / 2 is not equal to nums[i]. |
| 6 | + * Return any rearrangement of nums that meets the requirements. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: nums = [1,2,3,4,5] |
| 11 | + * Output: [1,2,4,5,3] |
| 12 | + * Explanation: |
| 13 | + * When i=1, nums[i] = 2, and the average of its neighbors is (1+4) / 2 = 2.5. |
| 14 | + * When i=2, nums[i] = 4, and the average of its neighbors is (2+5) / 2 = 3.5. |
| 15 | + * When i=3, nums[i] = 5, and the average of its neighbors is (4+3) / 2 = 3.5. |
| 16 | + * |
| 17 | + * Example 2: |
| 18 | + * |
| 19 | + * Input: nums = [6,2,0,9,7] |
| 20 | + * Output: [9,7,6,2,0] |
| 21 | + * Explanation: |
| 22 | + * When i=1, nums[i] = 7, and the average of its neighbors is (9+6) / 2 = 7.5. |
| 23 | + * When i=2, nums[i] = 6, and the average of its neighbors is (7+2) / 2 = 4.5. |
| 24 | + * When i=3, nums[i] = 2, and the average of its neighbors is (6+0) / 2 = 3. |
| 25 | + * Note that the original array [6,2,0,9,7] also satisfies the conditions. |
| 26 | + * |
| 27 | + * Constraints: |
| 28 | + * |
| 29 | + * 3 <= nums.length <= 10^5 |
| 30 | + * 0 <= nums[i] <= 10^5 |
| 31 | + * |
| 32 | + */ |
| 33 | +pub struct Solution {} |
| 34 | + |
| 35 | +// problem: https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/ |
| 36 | +// discuss: https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/discuss/?currentPage=1&orderBy=most_votes&query= |
| 37 | + |
| 38 | +// submission codes start here |
| 39 | + |
| 40 | +impl Solution { |
| 41 | + pub fn rearrange_array(nums: Vec<i32>) -> Vec<i32> { |
| 42 | + vec![] |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// submission codes end |
| 47 | + |
| 48 | +#[cfg(test)] |
| 49 | +mod tests { |
| 50 | + use super::*; |
| 51 | + |
| 52 | + #[test] |
| 53 | + #[ignore] |
| 54 | + fn test_1968_example_1() { |
| 55 | + let nums = vec![1, 2, 3, 4, 5]; |
| 56 | + |
| 57 | + let result = vec![1, 2, 4, 5, 3]; |
| 58 | + |
| 59 | + assert_eq!(Solution::rearrange_array(nums), result); |
| 60 | + } |
| 61 | + |
| 62 | + #[test] |
| 63 | + #[ignore] |
| 64 | + fn test_1968_example_2() { |
| 65 | + let nums = vec![6, 2, 0, 9, 7]; |
| 66 | + |
| 67 | + let result = vec![9, 7, 6, 2, 0]; |
| 68 | + |
| 69 | + assert_eq!(Solution::rearrange_array(nums), result); |
| 70 | + } |
| 71 | +} |
0 commit comments