3618. Split Array by Prime Indices Medium

@problem@discussion
#Array#Math#Number Theory



1/**
2 * [3618] Split Array by Prime Indices
3 *
4 * You are given an integer array nums.
5 * Split nums into two arrays A and B using the following rule:
6 * 
7 * 	Elements at <span data-keyword="prime-number">prime</span> indices in nums must go into array A.
8 * 	All other elements must go into array B.
9 * 
10 * Return the absolute difference between the sums of the two arrays: |sum(A) - sum(B)|.
11 * Note: An empty array has a sum of 0.
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">nums = [2,3,4]</span>
16 * Output: <span class="example-io">1</span>
17 * Explanation:
18 * 
19 * 	The only prime index in the array is 2, so nums[2] = 4 is placed in array A.
20 * 	The remaining elements, nums[0] = 2 and nums[1] = 3 are placed in array B.
21 * 	sum(A) = 4, sum(B) = 2 + 3 = 5.
22 * 	The absolute difference is |4 - 5| = 1.
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">nums = [-1,5,7,0]</span>
27 * Output: <span class="example-io">3</span>
28 * Explanation:
29 * 
30 * 	The prime indices in the array are 2 and 3, so nums[2] = 7 and nums[3] = 0 are placed in array A.
31 * 	The remaining elements, nums[0] = -1 and nums[1] = 5 are placed in array B.
32 * 	sum(A) = 7 + 0 = 7, sum(B) = -1 + 5 = 4.
33 * 	The absolute difference is |7 - 4| = 3.
34 * </div>
35 *  
36 * Constraints:
37 * 
38 * 	1 <= nums.length <= 10^5
39 * 	-10^9 <= nums[i] <= 10^9
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/split-array-by-prime-indices/
45// discuss: https://leetcode.com/problems/split-array-by-prime-indices/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn split_array(nums: Vec<i32>) -> i64 {
51        
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_3618() {
63    }
64}
65

Back
© 2026 bowen.ge All Rights Reserved.