- Related Questions & Answers
- Check if elements of an array can be arranged satisfying the given condition in Python
- Check if the array can be sorted using swaps between given indices only in Python
- Find the index which is the last to be reduced to zero after performing a given operation in Python
- Python program to check if the given number is Happy Number
- Python Program to check if the given array is Monotonic
- Python program to check if the given number is a Disarium Number
- Check if a given number can be represented in given a no. of digits in any base in C++
- Check if the given number K is enough to reach the end of an array in Python
- Check if the given number is Ore number or not in Python
- Check whether given string can be generated after concatenating given strings in Python
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Check if the given array contains all the divisors of some integer in Python
- Check if given number is perfect square in Python
- Maximizing array sum with given operation in C++
- Check if given string can be split into four distinct strings in Python

- 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 a value k, we have to check whether the elements in nums can be made 0 by performing the following operation exactly k number of times or not.

- Operation: The smallest element from the nums will be subtracted from all of the non-zero values of nums.

So, if the input is like nums [2, 2, 3, 5] k = 3, then the output will be True because at first delete 2 from array, so the array will be [0, 0, 1, 3], then remove 1 to get [0, 0, 0, 2], then again delete 2 to get [0, 0, 0, 0].

To solve this, we will follow these steps −

- if there are k number of distinct elements, then
- return True
- return False

Let us see the following implementation to get better understanding −

def solve(nums, k): distinct = set(nums) if len(distinct) == k: return True return False nums = [2, 2, 3, 4] k = 3 print(solve(nums, k))

[2, 2, 3, 4], 3

True

Advertisements