Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Count indices where the maximum in the prefix array is less than that in the suffix array
Next article icon

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

Last Updated : 10 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
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: 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++
// 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) 


Next Article
Count indices where the maximum in the prefix array is less than that in the suffix array
author
mdjabir786
Improve
Article Tags :
  • Greedy
  • DSA
  • Arrays
Practice Tags :
  • Arrays
  • Greedy

Similar Reads

  • Find the maximum number of indices having 0 as Prefix sum
    Given an array arr[]. Find the maximum number of indices having 0 as prefix sums possible by replacing the 0s in the array with any arbitrary value. Examples: Input: arr[] = {2, 0, 1, -1, 0}Output: 3Explanation: On changing the 1st 0 at arr[1] to -2 the array changes to {2, -2, 1, -1, 0} which 3 pre
    13 min read
  • Find the maximum subarray XOR in a given array
    Given an array of integers. The task is to find the maximum subarray XOR value in the given array. Examples: Input: arr[] = {1, 2, 3, 4}Output: 7Explanation: The subarray {3, 4} has maximum XOR value Input: arr[] = {8, 1, 2, 12, 7, 6}Output: 15Explanation: The subarray {1, 2, 12} has maximum XOR val
    15+ min read
  • Find a number that divides maximum array elements
    Given an array A[] of N non-negative integers. Find an Integer greater than 1, such that maximum array elements are divisible by it. In case of same answer print the smaller one. Examples: Input : A[] = { 2, 4, 5, 10, 8, 15, 16 }; Output : 2 Explanation: 2 divides [ 2, 4, 10, 8, 16] no other element
    13 min read
  • Count indices where the maximum in the prefix array is less than that in the suffix array
    Given an array arr[] of size N, the task is to find the number of prefix arrays whose maximum is less than the maximum element in the remaining suffix array. Examples: Input: arr[] = {2, 3, 4, 8, 1, 4}Output: 3Explanation:Prefix array = {2}, {2, 3}, {2, 3, 4}, {2, 3, 4, 8}, {2, 3, 4, 8, 1} Respectiv
    8 min read
  • Maximum number of Unique integers in Sub-Array of given size
    Given an array of N integers and a number M. The task is to find out the maximum number of unique integers among all possible contiguous subarrays of size M. Examples: Input : arr[] = {5, 3, 5, 2, 3, 2}, M = 3 Output : 3 Explanation: In the sample test case, there are 4 subarrays of size 3. s1 = (5,
    12 min read
  • Count the maximum number of elements that can be selected from the array
    Given an array arr[], the task is to count the maximum number of elements that can be selected from the given array following the below selection process: At 1st selection, select an element which is greater than or equal to 1.At 2nd selection, select an element which is greater than or equal to 2.A
    5 min read
  • Find alphabet in a Matrix which has maximum number of stars around it
    Given a matrix mat consisting of * and lowercase English alphabets, the task is to find the character which has the maximum number of * around it (including diagonal elements too). If two characters have same maximum count, print lexicographically smallest character. Source: D-E Shaw Interview Exper
    15+ min read
  • Count of indices in an array that satisfy the given condition
    Given an array arr[] of N positive integers, the task is to find the count of indices i such that all the elements from arr[0] to arr[i - 1] are smaller than arr[i]. Examples: Input: arr[] = {1, 2, 3, 4} Output: 4 All indices satisfy the given condition.Input: arr[] = {4, 3, 2, 1} Output: 1 Only i =
    4 min read
  • Find the peak index of a given array
    Given an array arr[] consisting of N(> 2) integers, the task is to find the peak index of the array. If the array doesn't contain any peak index, then print -1. The peak index, say idx, of the given array arr[] consisting of N integers is defined as: 0 < idx < N - 1arr[0] < arr[1] < a
    8 min read
  • Find all numbers that divide maximum array elements
    Given an array of N numbers, the task is to print all the numbers greater than 1 which divides the maximum of array elements. Examples: Input: a[] = {6, 6, 12, 18, 13} Output: 2 3 6 All the numbers divide the maximum of array elements i.e., 4 Input: a[] = {12, 15, 27, 20, 40} Output: 2 3 4 5 Approac
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences