Skip to content

Commit fd95155

Browse files
committed
2149. Rearrange Array Elements by Sign: RETRY
1 parent 92640fb commit fd95155

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,3 +1624,4 @@ mod s2145_count_the_hidden_sequences;
16241624
mod s2146_k_highest_ranked_items_within_a_price_range;
16251625
mod s2147_number_of_ways_to_divide_a_long_corridor;
16261626
mod s2148_count_elements_with_strictly_smaller_and_greater_elements;
1627+
mod s2149_rearrange_array_elements_by_sign;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* [2149] Rearrange Array Elements by Sign
3+
*
4+
* You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers.
5+
* You should return the array of nums such that the the array follows the given conditions:
6+
* <ol>
7+
* Every consecutive pair of integers have opposite signs.
8+
* For all integers with the same sign, the order in which they were present in nums is preserved.
9+
* The rearranged array begins with a positive integer.
10+
* </ol>
11+
* Return the modified array after rearranging the elements to satisfy the aforementioned conditions.
12+
*
13+
* Example 1:
14+
*
15+
* Input: nums = [3,1,-2,-5,2,-4]
16+
* Output: [3,-2,1,-5,2,-4]
17+
* Explanation:
18+
* The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
19+
* The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
20+
* Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.
21+
*
22+
* Example 2:
23+
*
24+
* Input: nums = [-1,1]
25+
* Output: [1,-1]
26+
* Explanation:
27+
* 1 is the only positive integer and -1 the only negative integer in nums.
28+
* So nums is rearranged to [1,-1].
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 2 <= nums.length <= 2 * 10^5
34+
* nums.length is even
35+
* 1 <= |nums[i]| <= 10^5
36+
* nums consists of equal number of positive and negative integers.
37+
*
38+
*
39+
* It is not required to do the modifications in-place.
40+
*/
41+
pub struct Solution {}
42+
43+
// problem: https://leetcode.com/problems/rearrange-array-elements-by-sign/
44+
// discuss: https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/?currentPage=1&orderBy=most_votes&query=
45+
46+
// submission codes start here
47+
48+
impl Solution {
49+
pub fn rearrange_array(nums: Vec<i32>) -> Vec<i32> {
50+
vec![]
51+
}
52+
}
53+
54+
// submission codes end
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
60+
#[test]
61+
#[ignore]
62+
fn test_2149_example_1() {
63+
let nums = vec![3, 1, -2, -5, 2, -4];
64+
65+
let result = vec![3, -2, 1, -5, 2, -4];
66+
67+
assert_eq!(Solution::rearrange_array(nums), result);
68+
}
69+
70+
#[test]
71+
#[ignore]
72+
fn test_2149_example_2() {
73+
let nums = vec![-1, 1];
74+
75+
let result = vec![1, -1];
76+
77+
assert_eq!(Solution::rearrange_array(nums), result);
78+
}
79+
}

0 commit comments

Comments
 (0)