
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find most occurring number after k increments in python
Suppose we have a list of numbers called nums and another value k. Let us consider an operation where we increase some element by one. We can perform at most k times, we have to find the value of the most frequently occurring number we can obtain. If there are more than one solution, select the smallest possible number.
So, if the input is like nums = [1, 0, 0, 0, 8, 8, 8, 8] k = 8, then the output will be 8, as we can increase 1, 7 times to get 8, and increase any 0 to 1, so, we get [8, 1, 0, 0, 8, 8, 8, 8]. So the result is 8.
To solve this, we will follow these steps:
- sort the list nums
- low := 0, high := 0
- dist := 0, best := 0
- ret := -1
- while high < size of nums, do
- if high > 0 and nums[high] is not same as nums[high - 1], then
- dist := dist +(high - low) *(nums[high] - nums[high - 1])
- high := high + 1
- while dist > k, do
- dist := dist - nums[high - 1] - nums[low]
- low := low + 1
- if high - low > best, then
- best := high - low
- ret := nums[high - 1]
- return ret
- if high > 0 and nums[high] is not same as nums[high - 1], then
Let us see the following implementation to get better understanding:
Example Code
class Solution: def solve(self, nums, k): nums.sort() low, high = 0, 0 dist = 0 best = 0 ret = -1 while high < len(nums): if high > 0 and nums[high] != nums[high - 1]: dist += (high - low) * (nums[high] - nums[high - 1]) high += 1 while dist > k: dist -= nums[high - 1] - nums[low] low += 1 if high - low > best: best = high - low ret = nums[high - 1] return ret ob = Solution() nums = [1, 0, 0, 0, 8, 8, 8, 8] k = 8 print(ob.solve(nums, k))
Input
[1, 0, 0, 0, 8, 8, 8, 8], 8
Output
8
- Related Articles
- Program to find longest equivalent sublist after K increments in Python
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Python program to find the most occurring character and its count
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- Program to find min length of run-length encoding after removing at most k characters in Python
- Program to find least number of unique integers after K removals using Python
- The most occurring number in a string using Regex in python
- Python Program to Find Element Occurring Odd Number of Times in a List
- Program to find minimum amplitude after deleting K elements in Python
- Program to find total duration of K most watched shows in Python
- Program to find k partitions after truncating sentence using Python
- Program to find minimum number of increments on subarrays to form a target array in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find state of prison cells after k days in python
- Program to find string after deleting k consecutive duplicate characters in python

Advertisements