mediumArrayGreedy 0 views

Transform Array to All Equal Elements

You are given an integer array nums of size n containing only 1 and -1, and an integer k.

You are given an integer array nums of size n containing only 1 and -1, and an integer k.

You can perform the following operation at most k times:

Note that you can choose the same index i more than once in different operations.

Return true if it is possible to make all elements of the array equal after at most k operations, and false otherwise.

Transform Array to All Equal Elements diagram

Example 1

Input: nums = [1,-1,1,-1,1], k = 3

Output: true

Explanation: We can make all elements in the array equal in 2 operations as follows:

Example 2

Input: nums = [-1,-1,-1,1,1,1], k = 5

Output: false

Explanation: It is not possible to make all array elements equal in at most 5 operations.

Constraints

  • 1 <= n == nums.length <= 10^5
  • nums[i] is either -1 or 1.
  • 1 <= k <= n

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.