Javatpoint Logo
Javatpoint Logo

Find the maximum number of marked indices problem.

Problem Statement:

We are given a 0-indexed integer array nums. Initially, all of the indices are unmarked. You are allowed to make this operation any number of times:

Pick two different unmarked indices i and j such that 2 * nums[i] <= nums[j], then mark i and j.

Return the maximum possible number of marked indices in nums using the above operation any number of times.

Java Code Using Two pointers

Output:

Find the maximum number of marked indices problem

Code Explanation:

  • The Java code aims to find the maximum number of marked indices by sorting the input array. It initializes two pointers, i and j, to traverse the first and second halves of the sorted array, respectively. The algorithm compares elements at these pointers, marking indices when the condition nums[i] * 2 <= nums[j] holds true.
  • If the condition is satisfied, the counter is incremented by 2, representing the marked indices. The pointers progress accordingly, and the process continues until one of the halves is fully traversed. The algorithm efficiently determines marked indices by utilizing array sorting and linear traversal.

Time Complexity:

  • O(n log n), where n is the input array's length, is the time complexity. With an O(n log n) time complexity, the sorting process is the main factor.

Space Complexity:

  • O(1) is the space complexity. The procedure, irrespective of the size of the input array, utilizes a fixed quantity of excess space. The memory needs don't increase as the size of the input does because sorting is done in-place.

Java Code Using Priority Queue

Output:

Find the maximum number of marked indices problem

Code explanation:

  • The Java code aims to find the maximum number of marked indices by sorting the input array and using a priority queue. It first sorts the array in ascending order. Then, it initializes a max heap (priority queue) and populates it with elements greater than or equal to the median.
  • The algorithm iterates through the first half of the sorted array, marking indices by comparing elements with the largest remaining element in the max heap. The priority queue efficiently maintains the maximum elements.
  • The approach ensures correctness by utilizing a combination of sorting and max heap operations to identify and mark indices satisfying the specified condition.

Time Complexity:

  • The sorting process dominates the O(n log n) time complexity, where n is the length of the input array. The operations in the priority queue barely make a difference.

Space Complexity:

  • Because the algorithm utilizes additional space for the priority queue, the space complexity is O(n). In the worst situation, the size of the priority queue could increase to n/2, where n is the input array's length. Sorting is done right there.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news