Leetcode-2576

Find the Maximum Number of Marked Indices

Time complexity: $O(n)$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <algorithm>

class Solution {
public:
    int maxNumOfMarkedIndices(std::vector<int>& nums) {
        int n = nums.size();
        std::sort(nums.begin(), nums.end());
        
        int a = 0;
        for (int b = (n + 1) / 2; b < n; ++b) {
            if (nums[b] >= 2 * nums[a]) {
                ++a;
            }
        }
        
        return a * 2;
    }
};

Leetcode-2576