Sort Array by Increasing Frequency

🏠 ⬅️ ➡️

Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.

Return the sorted array.

Example 1:

Input: nums = [1,1,2,2,2,3] Output: [3,1,1,2,2,2] Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.

Example 2:

Input: nums = [2,3,1,3,2] Output: [1,3,3,2,2] Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.

Example 3:

Input: nums = [-1,1,-6,4,5,-6,1,4,1] Output: [5,-1,4,4,-6,-6,1,1,1]

Constraints:

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    ! Solve the problem described in the task
    implicit none

    integer, parameter :: n = 100
    integer :: nums(n)
    integer :: freq(n)
    integer :: i, j, k

    ! Read input
    read (*, *) nums

    ! Initialize frequencies
    do i = 1, n
        freq(i) = 0
    end do

    ! Count frequencies
    do i = 1, size(nums)
        freq(nums(i)) = freq(nums(i)) + 1
    end do

    ! Sort by frequency (increasing)
    do i = 1, n - 1
        do j = i + 1, n
            if (freq(i) < freq(j)) then
                k = nums(i)
                nums(i) = nums(j)
                nums(j) = k
            end if
        end do
    end do

    ! Sort by value (decreasing)
    do i = 1, n - 1
        do j = i + 1, n
            if (nums(i) == nums(j)) then
                if (freq(i) > freq(j)) then
                    k = nums(i)
                    nums(i) = nums(j)
                    nums(j) = k
                end if
            end if
        end do
    end do

    ! Print output
    write (*, *) nums

end program main
Compiled
Executed
Correct
module FrequencySort
  implicit none
contains
  subroutine sort(nums)
    integer, intent(inout) :: nums(:)
    ! Local variables
    integer :: i, j, temp
    integer :: count(size(nums))
    logical :: sorted

    ! Initialize count array
    count = 0
    do i = 1, size(nums)
      count(nums(i)) = count(nums(i)) + 1
    end do

    ! Sort the array based on the frequency of the values
    do i = 1, size(nums)
      do j = i + 1, size(nums)
        if (count(nums(i)) < count(nums(j))) then
          temp = nums(i)
          nums(i) = nums(j)
          nums(j) = temp
        end if
      end do
    end do

    ! Sort the array within each frequency group in decreasing order
    do i = 1, size(nums)
      do j = i + 1, size(nums)
        if (count(nums(i)) == count(nums(j)) .and. nums(i) > nums(j)) then
          temp = nums(i)
          nums(i) = nums(j)
          nums(j) = temp
        end if
      end do
    end do

    ! Check if the array is sorted
    sorted = .true.
    do i = 2, size(nums)
      if (nums(i) < nums(i-1)) then
        sorted = .false.
        exit
      end if
    end do

    ! Print the sorted array
    if (sorted) then
      write (*,*) "The sorted array is:", nums
    else
      write (*,*) "The array is not sorted."
    end if
  end subroutine sort
end module

! Test the subroutine with the given examples
program test_sort
  use FrequencySort
  implicit none
  integer, parameter :: nums1(7) = [1, 1, 2, 2, 2, 3]
  integer, parameter :: nums2(8) = [2, 3, 1, 3, 2, 4, 4, 5]
  integer, parameter :: nums3(9) = [-1, 1, -6, 4, 5, -6, 1, 4, 1]

  call sort(nums1)
  call sort(nums2)
  call sort(nums3)
end program
🌐 Data from online sources
def num_sub(s: str) -> int:
    mod = 1000000007
    result = 0
    count = 0
    for c in s:
        count = count + 1 if c == '1' else 0
        result = (result + count) % mod
    return result

The algorithm is a single-pass algorithm that iterates over the binary string s and calculates the number of contiguous "1" substrings seen so far. We use a variable count to track the length of the current run of "1"s. For each character c in s: - If c is "1", we increment count as we have a longer run of 1's, and add the count to the result. The notion here is that until this run of 1s breaks, any new 1 being added increases the total number of substrings by the entire size of the current window. - If c is not a "1", we reset the count to 0 as the sequence is broken.

Finally, we take the result modulo 10^9 + 7 and return it.

🌐 Data from online sources
int numSub(string s) {
    int mod = 1000000007;
    long long result = 0;
    int count = 0;
    for (char c : s) {
        count = (c == '1') ? count + 1 : 0;
        result = (result + count) % mod;
    }
    return result;
}

The algorithm is a single-pass algorithm that iterates over the binary string s and calculates the number of contiguous "1" substrings seen so far. We use a variable count to track the length of the current run of "1"s. For each character c in s: - If c is "1", we increment count as we have a longer run of 1's, and add the count to the result. The notion here is that until this run of 1s breaks, any new 1 being added increases the total number of substrings by the entire size of the current window. - If c is not a "1", we reset the count to 0 as the sequence is broken.

Finally, we take the result modulo 10^9 + 7 and return it.