You are given a non-negative integer array nums
. In one operation, you must:
x
such that x
is less than or equal to the smallest non-zero element in nums
.x
from every positive element in nums
.Return the minimum number of operations to make every element in nums
equal to 0
.
Example 1:
Input: nums = [1,5,0,3,5] Output: 3 Explanation: In the first operation, choose x = 1. Now, nums = [0,4,0,2,4]. In the second operation, choose x = 2. Now, nums = [0,2,0,0,2]. In the third operation, choose x = 2. Now, nums = [0,0,0,0,0].
Example 2:
Input: nums = [0] Output: 0 Explanation: Each element in nums is already 0 so no operations are needed.
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 100
program main
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: nums
integer :: i, x, min_operations
! Example 1
nums = [1, 5, 0, 3, 5]
write(*,*) 'Example 1:'
write(*,*) 'Input:', nums
call solve(nums, min_operations)
write(*,*) 'Output:', min_operations
write(*,*) ''
! Example 2
nums = [0]
write(*,*) 'Example 2:'
write(*,*) 'Input:', nums
call solve(nums, min_operations)
write(*,*) 'Output:', min_operations
write(*,*) ''
contains
subroutine solve(nums, min_operations)
implicit none
integer, intent(in) :: nums(:)
integer, intent(out) :: min_operations
integer :: i, x
min_operations = 0
do while (any(nums > 0))
x = minval(nums(nums > 0), mask=nums > 0)
min_operations = min_operations + 1
do i = 1, size(nums)
if (nums(i) > 0) then
nums(i) = nums(i) - x
end if
end do
end do
end subroutine solve
end program main
temp.f95:34:28: 34 | x = minval(nums(nums > 0), mask=nums > 0) | 1 Error: Array index at (1) must be of INTEGER type, found LOGICAL temp.f95:38:20: 38 | nums(i) = nums(i) - x | 1 Error: Dummy argument ‘nums’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:17:4: 17 | nums = [0] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 1)
module MinimumOperations
implicit none
private
public :: minimumOperations
contains
integer function minimumOperations(nums) result(minOps)
implicit none
integer, intent(in) :: nums(:)
integer :: x, i, minOps
! Find the smallest non-zero element in nums
x = minval(nums, mask = nums /= 0)
! Initialize the minimum number of operations to 0
minOps = 0
! Loop through the array and subtract x from each positive element
do i = 1, size(nums)
if (nums(i) > 0) then
nums(i) = nums(i) - x
minOps = minOps + 1
end if
end do
end function minimumOperations
end module MinimumOperations
program test_minimumOperations
use MinimumOperations
implicit none
integer, parameter :: nums1(5) = [1, 5, 0, 3, 5]
integer, parameter :: nums2(1) = [0]
integer :: minOps1, minOps2
minOps1 = minimumOperations(nums1)
write (*,*) "Minimum operations for nums1:", minOps1
minOps2 = minimumOperations(nums2)
write (*,*) "Minimum operations for nums2:", minOps2
end program test_minimumOperations
temp.f95:4:31: 4 | public :: minimumOperations | 1 Error: PUBLIC attribute applied to MODULE minimumoperations at (1) temp.f95:6:38: 6 | integer function minimumOperations(nums) result(minOps) | 1 Error: MODULE attribute of ‘minimumoperations’ conflicts with PROCEDURE attribute at (1) temp.f95:7:21: 7 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:8:38: 8 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:31: 9 | integer :: x, i, minOps | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:42: 12 | x = minval(nums, mask = nums /= 0) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:18: 15 | minOps = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:28: 18 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:19:33: 19 | if (nums(i) > 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:20:37: 20 | nums(i) = nums(i) - x | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:21:35: 21 | minOps = minOps + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:15: 22 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:11: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:7: 24 | end function minimumOperations | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:9: 28 | use MinimumOperations | 1 Fatal Error: Cannot open module file ‘minimumoperations.mod’ for reading at (1): No such file or directory compilation terminated.
def minOperations(nums):
count = 0
nums.sort()
i = 0
while i < len(nums):
if nums[i] > 0:
target = nums[i]
while i < len(nums) and nums[i] == target:
i += 1
count += 1
else:
i += 1
return count
This algorithm works because after sorting the array, we traverse it and count distinct positive numbers. Each distinct positive number corresponds to a unique operation that reduces it to zero while also reducing any larger numbers in the array. Since the operation only affects positive numbers, we can skip the zero elements. By the end of the algorithm, we have counted the minimum number of operations required to make all elements equal to zero.
int minOperations(vector<int>& nums) {
int count = 0;
std::sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] > 0) {
count += nums.end() - (std::upper_bound(nums.begin() + i, nums.end(), nums[i]));
i = (std::upper_bound(nums.begin(), nums.end(), nums[i])) - nums.begin() - 1;
}
}
return count;
}
This algorithm works because after sorting the array, we traverse it and count distinct positive numbers. Each distinct positive number corresponds to a unique operation that reduces it to zero while also reducing any larger numbers in the array. Since the operation only affects positive numbers, we can skip the zero elements. By the end of the algorithm, we have counted the minimum number of operations required to make all elements equal to zero.