
- 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
Check if at least half array is reducible to zero by performing some operation in Python
Suppose, we are provided with a list of size n that contains positive integers and another positive integer m. Let's say, we are currently inside a loop and in each iteration, we decrease the value of some elements in the array by 1 and increase the value of the remaining elements by m. We have to find out if half or more of the elements of the list turn into zero after some iterations. We return True if possible, and False if not.
So, if the input is like input_list = [10, 18, 35, 5, 12], m = 4, then the output will be True.
To solve this, we will follow these steps −
- frequency_list := a new list of size m+1 initialized with 0
- i := 0
- while i < size of input_list, do
frequency_list[input_list[i] mod(m + 1) ] :=
frequency_list[input_list[i] mod (m + 1) ] + 1
- i := i + 1
- i := 0
- while i <= m, do
- if frequency_list[i] >=(size of input_list / 2) , then
- come out from the loop
- i := i + 1
- if frequency_list[i] >=(size of input_list / 2) , then
- if i <= m, then
- return True
- otherwise,
- return False
Example
Let us see the following implementation to get better understanding −
def solve(input_list, m): frequency_list = [0] * (m + 1) i = 0 while(i < len(input_list)): frequency_list[(input_list[i] % (m + 1))] += 1 i += 1 i = 0 while(i <= m): if(frequency_list[i] >= (len(input_list)/ 2)): break i += 1 if (i <= m): return True else: return False input_list = [10, 18, 35, 5, 12] print(solve(input_list, 4))
Input
[10, 18, 35, 5, 12], 4
Output
True
- Related Articles
- Find the index which is the last to be reduced to zero after performing a given operation in Python
- Program to make all elements equal by performing given operation in Python
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Python program to check if two lists have at least one common element
- Program to check final answer by performing given stack operations in Python
- How to check if a string has at least one letter and one number in Python?
- What is meant by Reducible fraction ?
- Check if both halves of the string have at least one different character in Python
- Check if the array is beautiful in Python
- How to check if a number is positive, negative or zero using Python?
- Check if the given array contains all the divisors of some integer in Python
- Program to find maximum sum by performing at most k negate operations in Python
- C# program to check if two lists have at-least one element common
- How to check if all the values in a numpy array are non-zero?
- Check if given array is almost sorted (elements are at-most one position away) in Python

Advertisements