Find the maximum number of indices that are marked in the given Array
Last Updated :
10 Apr, 2023
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: 2
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++
#include <bits/stdc++.h>
using namespace std;
int Mark_Indexes(vector< int >& a, int k)
{
sort(begin(a), end(a));
int i = 0, n = a.size();
for ( int j = n / 2; i < n / 2 && j < n; ++j) {
i += k * a[i] <= a[j];
}
return i * 2;
}
int main()
{
vector< int > a = { 3, 5, 2, 4 };
int N = 4;
int K = 2;
int ans = Mark_Indexes(a, K);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int markIndexes( int [] a, int k)
{
Arrays.sort(a);
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 i * 2 ;
}
public static void main(String[] args)
{
int [] a = { 3 , 5 , 2 , 4 };
int n = 4 , k = 2 ;
int ans = markIndexes(a, k);
System.out.println(ans);
}
}
|
Python
def Mark_Indexes(a, k):
a.sort()
i, n = 0 , len (a)
for j in range (n / / 2 , n):
i + = k * a[i] < = a[j]
return i * 2
if __name__ = = '__main__' :
a = [ 3 , 5 , 2 , 4 ]
N = 4
K = 2
ans = Mark_Indexes(a, K)
print (ans)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Program {
public static int Mark_Indexes(List< int > a, int k)
{
a.Sort();
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 i * 2;
}
public static void Main()
{
List< int > a = new List< int >() { 3, 5, 2, 4 };
int N = 4;
int K = 2;
int ans = Mark_Indexes(a, K);
Console.WriteLine(ans);
}
}
|
Javascript
function Mark_Indexes(a, k)
{
a.sort();
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 i * 2;
}
let a = [3, 5, 2, 4];
let N = 4;
let K = 2;
let ans = Mark_Indexes(a, K);
console.log(ans);
|
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)
Share your thoughts in the comments
Please Login to comment...