Maximum equal elements after K operations

You are given an arbitrary array/list of integers 'ARR' of size ‘N’ and an integer ‘K’. You need to find the maximum number of elements which can be made equal to each other after performing at most K operations.

An operation is defined as "Choosing an element from 'ARR' and increasing it by 1".

Note:
You can perform multiple operations on a single element also.
Input Format:
The first line of the input contains a single integer T, representing the number of test cases.

The first line of each test case consists of two space-separated integers, representing the values of N (size of 'ARR') and K(maximum number of updates possible). 

The second line of each test case contains N space-separated integers, denoting the elements of the 'ARR'.
Output Format:
For each test case, print a single integer, representing the maximum number of elements that can be made equal after performing K or fewer increments.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10^2
1 <= N <= 10^3
1 <= ARR[i], K <= 10^9

Time Limit: 1sec
CodingNinjas
author
2y

This was a very easy question as I just had to traverse both the array and maintain a count variable and a variable to store last maximum equal element encountered which I initialized to a very small ...read more

CodingNinjas
author
2y
Naive Approach

Approach: To find the maximum count of equal elements after performing K increments, a naive approach will be to consider every array element and try making all other elements equal to i...read more

CodingNinjas
author
2y
Binary Search

Approach: An efficient way to solve this problem is to first sort the array and then use binary search on the sorted array. In binary search, we will try to find a range of ‘X’ elements i...read more

Add answer anonymously...
Visa Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter