Leetcode 2576: Find the Maximum Number of Marked Indices

grid47
grid47
Exploring patterns and algorithms
Feb 23, 2024 5 min read

You are given a 0-indexed integer array nums. Initially, all indices are unmarked. You are allowed to perform the following operation any number of times: Pick two different unmarked indices i and j such that 2 * nums[i] <= nums[j], then mark both indices i and j. Return the maximum possible number of marked indices after performing this operation multiple times.
Problem
Approach
Steps
Complexity
Input: The input consists of an integer array nums of size n.
Example: For example, nums = [4, 6, 2, 5].
Constraints:
• 1 <= nums.length <= 10^5
• 1 <= nums[i] <= 10^9
Output: The output is an integer, the maximum possible number of marked indices.
Example: For nums = [10, 2, 3, 6], the output is 4.
Constraints:
• The output is an integer representing the maximum number of marked indices.
Goal: To find the maximum number of indices that can be marked by performing the operation any number of times.
Steps:
• 1. Sort the array nums.
• 2. Use a greedy approach to find valid pairs: For each unmarked index i, try to find a valid index j such that 2 * nums[i] <= nums[j].
• 3. Mark both indices i and j if the condition is met and repeat the process.
Goal: The input array contains integers from 1 to 10^9, and the array size is between 1 and 10^5.
Steps:
• 1 <= nums.length <= 10^5
• 1 <= nums[i] <= 10^9
Assumptions:
• The array nums is non-empty and contains only positive integers.
Input: For nums = [10, 2, 3, 6], the output is 4.
Explanation: First, pair index 3 with 0 (6 and 10), then pair index 1 with 2 (2 and 3), and all indices are marked.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus