Open In App

Find the maximum number of indices that are marked in the given Array

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a boolean array a[] of size N and integer K. Initially, all are marked false. We have to choose 2 different indexes having the value false such that a[i] * K ≤ a[j] and mark it as true, the task is to find the maximum number of indexes having a value true in array a[].

Examples:

Input: a[] = [3, 5, 2, 4], N = 4, K =2
Output:
Explanation: In the first operation take i = 2 and j = 1, the operation is taken because  2 * nums[2] ≤ nums[1]. then mark indexes 2 and 1. It can be shown that there’s no other valid operation so the answer is 2. 

Input: a[] = [7, 6, 8], N = 3,  K = 2 
Output: 0
Explanation: There is no valid operation to do, so the answer is 0.

Approach: This can be solved with the following idea:

The problem is observations and Greedy logic based. This problem can be solved using implementing those observations into code. 

Below are the steps to solve this problem:

  • Sort the array using the sort function. 
  • Now iterate a loop from i = 0 to N/2 and j = N/2 to N – 1. 
  • Whenever we found K * a[i] ≤ a[j] check add either 1 or 0 to i and i will keep rotating till N/2 
  • Finally, we will return i*2 as our answer the no indexes are marked.

Below is the code to implement the above approach:

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find count of marked index
int Mark_Indexes(vector<int>& a, int k)
{
 
    // Sorting vector
    sort(begin(a), end(a));
 
    // Start Iterating
    int i = 0, n = a.size();
    for (int j = n / 2; i < n / 2 && j < n; ++j) {
        i += k * a[i] <= a[j];
    }
 
    // Return the number of
    // marked indexes
    return i * 2;
}
 
// Driver code
int main()
{
    vector<int> a = { 3, 5, 2, 4 };
    int N = 4;
    int K = 2;
 
    // Function call
    int ans = Mark_Indexes(a, K);
    cout << ans << endl;
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
  // Function to find count of marked index
  static int markIndexes(int[] a, int k)
  {
    // Sorting array
    Arrays.sort(a);
 
    // Start iterating
    int i = 0, n = a.length;
    for (int j = n / 2; i < n / 2 && j < n; ++j) {
      i += k * a[i] <= a[j] ? 1 : 0;
    }
 
    // Return the number of marked indexes
    return i * 2;
  }
 
  public static void main(String[] args)
  {
    int[] a = { 3, 5, 2, 4 };
    int n = 4, k = 2;
 
    // Function call
    int ans = markIndexes(a, k);
    System.out.println(ans);
  }
}
 
// This code is contributed by sankar.


Python




# Function to find count of marked index
def Mark_Indexes(a, k):
    # Sorting list
    a.sort()
 
    # Start Iterating
    i, n = 0, len(a)
    for j in range(n // 2, n):
        i += k * a[i] <= a[j]
 
    # Return the number of marked indexes
    return i * 2
 
# Driver code
if __name__ == '__main__':
    a = [3, 5, 2, 4]
    N = 4
    K = 2
 
    # Function call
    ans = Mark_Indexes(a, K)
    print(ans)


C#




// C# code for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
public class Program {
    // Function to find count of marked index
    public static int Mark_Indexes(List<int> a, int k)
    {
        // Sorting list
        a.Sort();
 
        // Start Iterating
        int i = 0, n = a.Count;
        for (int j = n / 2; i < n / 2 && j < n; ++j) {
            i += k * a[i] <= a[j] ? 1 : 0;
        }
 
        // Return the number of marked indexes
        return i * 2;
    }
 
    // Driver code
    public static void Main()
    {
        List<int> a = new List<int>() { 3, 5, 2, 4 };
        int N = 4;
        int K = 2;
 
        // Function call
        int ans = Mark_Indexes(a, K);
        Console.WriteLine(ans);
    }
}


Javascript




// Function to find count of marked index
function Mark_Indexes(a, k)
{
 
    // Sorting list
    a.sort();
 
    // Start Iterating
    let i = 0;
    let n = a.length;
    for (let j = Math.floor(n / 2); j < n; j++) {
        i += (k * a[i] <= a[j]) ? 1 : 0;
    }
 
    // Return the number of marked indexes
    return i * 2;
}
 
// Driver code
let a = [3, 5, 2, 4];
let N = 4;
let K = 2;
 
// Function call
let ans = Mark_Indexes(a, K);
console.log(ans);
 
// This code is contributed by akashish__


Output

2

Time Complexity: O(N * log2N), where N is the size of the array. This is because sort function has been called which takes O(N * log2N) time. Also there is a for loop running which will take O(N) time. So overall complexity will be O(N * log2N).
Auxiliary Space: O(N) 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads