You are given a 0-indexed integer array nums
and a target element target
.
A target index is an index i
such that nums[i] == target
.
Return a list of the target indices of nums
after sorting nums
in non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order.
Example 1:
Input: nums = [1,2,5,2,3], target = 2 Output: [1,2] Explanation: After sorting, nums is [1,2,2,3,5]. The indices where nums[i] == 2 are 1 and 2.
Example 2:
Input: nums = [1,2,5,2,3], target = 3 Output: [3] Explanation: After sorting, nums is [1,2,2,3,5]. The index where nums[i] == 3 is 3.
Example 3:
Input: nums = [1,2,5,2,3], target = 5 Output: [4] Explanation: After sorting, nums is [1,2,2,3,5]. The index where nums[i] == 5 is 4.
Constraints:
1 <= nums.length <= 100
1 <= nums[i], target <= 100
program main
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: nums
integer :: i, j, target
integer, allocatable, dimension(:) :: targetIndices
! Examples
nums = [1, 2, 5, 2, 3]
target = 2
call solve(nums, target, targetIndices)
write (*, '(A, *(I0, A))') 'Example 1: ', targetIndices
nums = [1, 2, 5, 2, 3]
target = 3
call solve(nums, target, targetIndices)
write (*, '(A, *(I0, A))') 'Example 2: ', targetIndices
nums = [1, 2, 5, 2, 3]
target = 5
call solve(nums, target, targetIndices)
write (*, '(A, *(I0, A))') 'Example 3: ', targetIndices
contains
subroutine solve(nums, target, targetIndices)
implicit none
integer, intent(in) :: nums(:), target
integer, allocatable, intent(out) :: targetIndices(:)
integer :: i, j
allocate(targetIndices(size(nums)))
targetIndices = -1
do i = 1, size(nums)
if (nums(i) == target) then
targetIndices(i) = i
end if
end do
call sort(targetIndices)
end subroutine solve
end program main
/usr/bin/ld: /tmp/cc7Uxzji.o: in function `solve.0': temp.f95:(.text+0x3ef): undefined reference to `sort_' collect2: error: ld returned 1 exit status
module TargetIndices
implicit none
private
public :: target_indices
contains
function target_indices(nums, target) result(indices)
integer, intent(in) :: nums(:)
integer, intent(in) :: target
integer :: indices(size(nums))
integer :: i, j
! Sort the array in non-decreasing order
call sort(nums)
! Find the indices of the target elements
i = 1
j = 1
do while (i <= size(nums))
if (nums(i) == target) then
indices(j) = i
j = j + 1
end if
i = i + 1
end do
! Return the sorted indices
indices = indices(1:j-1)
end function target_indices
end module TargetIndices
! Test the function with some examples
program test_target_indices
use TargetIndices
implicit none
integer, parameter :: nums1(5) = [1, 2, 5, 2, 3]
integer, parameter :: nums2(5) = [1, 2, 5, 2, 3]
integer, parameter :: nums3(5) = [1, 2, 5, 2, 3]
integer, parameter :: target = 2
integer :: indices1(size(nums1))
integer :: indices2(size(nums2))
integer :: indices3(size(nums3))
indices1 = target_indices(nums1, target)
indices2 = target_indices(nums2, target)
indices3 = target_indices(nums3, target)
write (*,*) "Indices of target = 2 in nums1:", indices1
write (*,*) "Indices of target = 2 in nums2:", indices2
write (*,*) "Indices of target = 2 in nums3:", indices3
end program test_target_indices
/usr/bin/ld: /tmp/cctbKega.o: in function `__targetindices_MOD_target_indices': temp.f95:(.text+0x1c9): undefined reference to `sort_' collect2: error: ld returned 1 exit status
def maxMatrixSum(matrix):
n = len(matrix)
minValue, negativeCount, total = float('inf'), 0, 0
for i in range(n):
for j in range(n):
total += abs(matrix[i][j])
minValue = min(minValue, abs(matrix[i][j]))
if matrix[i][j] < 0:
negativeCount += 1
if negativeCount % 2 == 0:
return total
else:
return total - 2 * minValue
To maximize the summation of the matrix's elements, we can follow these steps: 1. Calculate the sum of the absolute values of all elements. 2. Count the number of negative elements in the matrix. 3. Determine the minimum absolute value of all elements in the matrix. 4. If the number of negative values is even, the sum remains the same. 5. If the number of negative values is odd, subtract twice the minimum absolute value from the sum, which accounts for turning an adjacent element from positive to negative, thereby increasing the overall sum.
In each language implementation, we create a function called maxMatrixSum
that takes an n x n integer matrix as input and loops through the matrix to calculate the values described in the steps above. The function then returns either the sum of elements or the sum minus twice the minimum absolute value depending on the number of negative values. This way, we maximize the summation of the matrix's elements.
int maxMatrixSum(vector<vector<int>>& matrix) {
int n = matrix.size();
int minValue = INT_MAX, negativeCount = 0, sum = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
sum += abs(matrix[i][j]);
minValue = min(minValue, abs(matrix[i][j]));
if (matrix[i][j] < 0) {
negativeCount++;
}
}
}
if (negativeCount % 2 == 0) {
return sum;
} else {
return sum - 2 * minValue;
}
}
To maximize the summation of the matrix's elements, we can follow these steps: 1. Calculate the sum of the absolute values of all elements. 2. Count the number of negative elements in the matrix. 3. Determine the minimum absolute value of all elements in the matrix. 4. If the number of negative values is even, the sum remains the same. 5. If the number of negative values is odd, subtract twice the minimum absolute value from the sum, which accounts for turning an adjacent element from positive to negative, thereby increasing the overall sum.
In each language implementation, we create a function called maxMatrixSum
that takes an n x n integer matrix as input and loops through the matrix to calculate the values described in the steps above. The function then returns either the sum of elements or the sum minus twice the minimum absolute value depending on the number of negative values. This way, we maximize the summation of the matrix's elements.