How Many Numbers Are Smaller Than the Current Number

🏠 ⬅️ ➡️

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 <= 500
  • 0 <= nums[i] <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program 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
Compiled
Executed
Correct
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
🌐 Data from online sources
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.

🌐 Data from online sources
#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.