- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array nums and another value k. In one operation, we can select an index of nums and increase the element at that index by 1. We have to find the maximum possible frequency of an element after performing at most k number of operations.

So, if the input is like nums = [8,3,6], k = 9, then the output will be 3 because we can update 3 by 5, 6 by 2, to make it [8,8,8] so after 7 operations we have maximum frequency 3.

To solve this, we will follow these steps −

sort the list nums

left := 0, right := 1

while right < size of nums, do

k := k -(nums[right] - nums[right-1]) *(right - left)

if k < 0, then

k := k + nums[right] - nums[left]

left := left + 1

right := right + 1

return right - left

Let us see the following implementation to get better understanding −

def solve(nums, k): nums.sort() left = 0 right = 1 while right < len(nums): k -= (nums[right] - nums[right-1]) * (right - left) if k < 0: k += nums[right] - nums[left] left += 1 right += 1 return right - left nums = [8,3,6] k = 9 print(solve(nums, k))

[8,3,6], 9

3

- Related Questions & Answers
- C# program to find the most frequent element
- Find most frequent element in a list in Python
- Program to find out the index of the most frequent element in a concealed array in Python
- Find the second most frequent element in array JavaScript
- Python program to find Most Frequent Character in a String
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python
- Program to find most frequent subtree sum of a binary tree in Python
- Program to find second most frequent character in C++
- Write a program in C++ to find the most frequent element in a given array of integers
- Most frequent element in an array in C++
- Find the k most frequent words from data set in Python
- Python program for most frequent word in Strings List
- Count Frequency of Highest Frequent Elements in Python
- Find Second most frequent character in array - JavaScript
- Python Program to crawl a web page and get most frequent words

Advertisements