Class Solution
-
- All Implemented Interfaces:
public final class Solution3576 - Transform Array to All Equal Elements.
Medium
You are given an integer array
numsof sizencontaining only1and-1, and an integerk.You can perform the following operation at most
ktimes:Choose an index
i(0 <= i < n - 1), and multiply bothnums[i]andnums[i + 1]by-1.
Note that you can choose the same index
imore than once in different operations.Return
trueif it is possible to make all elements of the array equal after at mostkoperations, andfalseotherwise.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:
Choose index
i = 1, and multiply bothnums[1]andnums[2]by -1. Nownums = [1,1,-1,-1,1].Choose index
i = 2, and multiply bothnums[2]andnums[3]by -1. Nownums = [1,1,1,1,1].
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:
<code>1 <= n == nums.length <= 10<sup>5</sup></code>
nums[i]is either -1 or 1.1 <= k <= n
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final BooleancanMakeEqual(IntArray nums, Integer k)-
-
Method Detail
-
canMakeEqual
final Boolean canMakeEqual(IntArray nums, Integer k)
-
-
-
-