Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].
Return the answer in an array.
Example 1:
Input: nums = [8,1,2,2,3] Output: [4,0,1,1,3] Explanation: For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). For nums[1]=1 does not exist any smaller number than it. For nums[2]=2 there exist one smaller number than it (1). For nums[3]=2 there exist one smaller number than it (1). For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
Example 2:
Input: nums = [6,5,4,8] Output: [2,1,0,3]
Example 3:
Input: nums = [7,7,7,7] Output: [0,0,0,0]
Constraints:
2 <= nums.length <= 5000 <= nums[i] <= 100program main
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: nums
integer, dimension(n) :: ans
! Example 1
nums = [8, 1, 2, 2, 3]
ans = solve(nums)
write (*, '(A)') 'Example 1:'
write (*, '(A)') 'Input: ', nums
write (*, '(A)') 'Output: ', ans
! Example 2
nums = [6, 5, 4, 8]
ans = solve(nums)
write (*, '(A)') 'Example 2:'
write (*, '(A)') 'Input: ', nums
write (*, '(A)') 'Output: ', ans
! Example 3
nums = [7, 7, 7, 7]
ans = solve(nums)
write (*, '(A)') 'Example 3:'
write (*, '(A)') 'Input: ', nums
write (*, '(A)') 'Output: ', ans
contains
function solve(nums) result(ans)
implicit none
integer, dimension(:), intent(in) :: nums
integer, dimension(size(nums)) :: ans
integer :: i, j
do i = 1, size(nums)
ans(i) = 0
do j = 1, size(nums)
if (j /= i .and. nums(j) < nums(i)) then
ans(i) = ans(i) + 1
end if
end do
end do
end function solve
end program main
temp.f95:16:4:
16 | nums = [6, 5, 4, 8]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (5 and 4)
temp.f95:23:4:
23 | nums = [7, 7, 7, 7]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (5 and 4)
module smaller_numbers
implicit none
contains
subroutine smaller_numbers(nums, result)
integer, intent(in) :: nums(:)
integer, intent(out) :: result(size(nums))
integer :: i, j
do i = 1, size(nums)
result(i) = 0
do j = 1, i - 1
if (nums(j) < nums(i)) then
result(i) = result(i) + 1
end if
end do
do j = i + 1, size(nums)
if (nums(j) < nums(i)) then
result(i) = result(i) + 1
end if
end do
end do
end subroutine smaller_numbers
end module smaller_numbers
program test_smaller_numbers
use smaller_numbers
implicit none
integer, parameter :: nums = [8, 1, 2, 2, 3]
integer, parameter :: expected = [4, 0, 1, 1, 3]
integer :: result(size(nums))
call smaller_numbers(nums, result)
if (any(result .ne. expected)) then
write (*,*) "Test failed"
else
write (*,*) "Test passed"
end if
contains
subroutine check(nums, expected, result)
integer, intent(in) :: nums(:)
integer, intent(in) :: expected(:)
integer, intent(in) :: result(:)
integer :: i
do i = 1, size(nums)
if (result(i) .ne. expected(i)) then
write (*,*) "Test failed for nums=", nums
write (*,*) "Expected result=", expected
write (*,*) "Actual result=", result
stop
end if
end do
end subroutine check
end program test_smaller_numbers
temp.f95:4:28:
4 | subroutine smaller_numbers(nums, result)
| 1
Error: MODULE attribute of ‘smaller_numbers’ conflicts with PROCEDURE attribute at (1)
temp.f95:5:34:
5 | integer, intent(in) :: nums(:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:6:40:
6 | integer, intent(out) :: result(size(nums))
| 1
Error: Symbol ‘nums’ is used before it is typed at (1)
temp.f95:7:19:
7 | integer :: i, j
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:9:24:
9 | do i = 1, size(nums)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:10:19:
10 | result(i) = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:11:21:
11 | do j = 1, i - 1
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:12:35:
12 | if (nums(j) < nums(i)) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:13:35:
13 | result(i) = result(i) + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:14:11:
14 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:15:9:
15 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:16:30:
16 | do j = i + 1, size(nums)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:17:35:
17 | if (nums(j) < nums(i)) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:18:35:
18 | result(i) = result(i) + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:19:11:
19 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:20:9:
20 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:21:7:
21 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:22:5:
22 | end subroutine smaller_numbers
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:26:7:
26 | use smaller_numbers
| 1
Fatal Error: Cannot open module file ‘smaller_numbers.mod’ for reading at (1): No such file or directory
compilation terminated.
def smallerNumbersThanCurrent(nums):
result = []
for i in range(len(nums)):
count = 0
for j in range(len(nums)):
if i != j and nums[j] < nums[i]:
count += 1
result.append(count)
return result
The algorithm implements a simple nested loop. For each element nums[i] in the input array nums, compare it to all other elements in the array. If any element nums[j] is smaller than nums[i] and j is not equal to i, increment a counter. After iterating through all elements, store the value of the counter in the result array at the same index as the current element nums[i]. Finally, return the result array.
#include <vector>
using namespace std;
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
vector<int> result(nums.size());
for (int i = 0; i < nums.size(); i++) {
int count = 0;
for (int j = 0; j < nums.size(); j++) {
if (i != j && nums[j] < nums[i]) {
count++;
}
}
result[i] = count;
}
return result;
}
The algorithm implements a simple nested loop. For each element nums[i] in the input array nums, compare it to all other elements in the array. If any element nums[j] is smaller than nums[i] and j is not equal to i, increment a counter. After iterating through all elements, store the value of the counter in the result array at the same index as the current element nums[i]. Finally, return the result array.