2576. Find the Maximum Number of Marked Indices Medium

@problem@discussion
#Array#Two Pointers#Binary Search#Greedy#Sorting



1/**
2 * [2576] Find the Maximum Number of Marked Indices
3 *
4 * You are given a 0-indexed integer array nums.
5 * Initially, all of the indices are unmarked. You are allowed to make this operation any number of times:
6 * 
7 * 	Pick two different unmarked indices i and j such that 2 * nums[i] <= nums[j], then mark i and j.
8 * 
9 * Return the maximum possible number of marked indices in nums using the above operation any number of times.
10 *  
11 * <strong class="example">Example 1:
12 * 
13 * Input: nums = [3,5,2,4]
14 * Output: 2
15 * Explanation: In the first operation: pick i = 2 and j = 1, the operation is allowed because 2 * nums[2] <= nums[1]. Then mark index 2 and 1.
16 * It can be shown that there's no other valid operation so the answer is 2.
17 * 
18 * <strong class="example">Example 2:
19 * 
20 * Input: nums = [9,2,5,4]
21 * Output: 4
22 * Explanation: In the first operation: pick i = 3 and j = 0, the operation is allowed because 2 * nums[3] <= nums[0]. Then mark index 3 and 0.
23 * In the second operation: pick i = 1 and j = 2, the operation is allowed because 2 * nums[1] <= nums[2]. Then mark index 1 and 2.
24 * Since there is no other operation, the answer is 4.
25 * 
26 * <strong class="example">Example 3:
27 * 
28 * Input: nums = [7,6,8]
29 * Output: 0
30 * Explanation: There is no valid operation to do, so the answer is 0.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= nums.length <= 10^5
36 * 	1 <= nums[i] <= 10^9
37 * 
38 *  
39 * <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; 
40 * }
41 * .spoiler {overflow:hidden;}
42 * .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
43 * .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
44 * .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
45 * </style>
46 * 
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/find-the-maximum-number-of-marked-indices/
51// discuss: https://leetcode.com/problems/find-the-maximum-number-of-marked-indices/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56    pub fn max_num_of_marked_indices(nums: Vec<i32>) -> i32 {
57        0
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_2576() {
69    }
70}
71


Back
© 2025 bowen.ge All Rights Reserved.