Element Appearing More Than 25% In Sorted Array

🏠 ⬅️ ➑️

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

Example 1:

Input: arr = [1,2,2,6,6,6,6,7,10] Output: 6

Example 2:

Input: arr = [1,1] Output: 1

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 105

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer, parameter :: n = 9
    integer, dimension(n) :: sorted = (/ 1, 2, 2, 6, 6, 6, 6, 7, 10 /)
    integer :: i, j, k, l, m, n, o
    integer :: result

    ! Example 1
    write (*, '(A)') 'Example 1:'
    write (*, '(A)') 'arr = [1,2,2,6,6,6,6,7,10]'
    result = find_most_frequent(sorted)
    write (*, '(A, I0)') 'result = ', result
    write (*, *)

    ! Example 2
    write (*, '(A)') 'Example 2:'
    write (*, '(A)') 'arr = [1,1]'
    result = find_most_frequent(sorted)
    write (*, '(A, I0)') 'result = ', result
    write (*, *)

contains

    function find_most_frequent(sorted) result(res)
        implicit none
        integer, intent(in) :: sorted(:)
        integer :: res
        integer :: i, j, k, l, m, n, o

        do i = 1, size(sorted) - 1
            if (sorted(i+1) == sorted(i)) then
                cycle
            else
                exit
            end if
        end do

        res = sorted(i)
    end function find_most_frequent

end program main
❌ Compiled
❌ Executed
❌ Correct
module FindMode

implicit none

contains

function find_mode(sorted) result(mode)

integer, intent(in) :: sorted(:)
integer :: mode

integer :: i, count, max_count

max_count = 0
mode = 0

do i = 1, size(sorted)

    count = 1

    if (i < size(sorted)) then

        if (sorted(i) == sorted(i+1)) then

            count = count + 1

        end if

    end if

    if (count > max_count) then

        max_count = count
        mode = sorted(i)

    end if

end do

end function find_mode

end module FindMode

program test_find_mode

use FindMode

implicit none

integer, parameter :: arr1(8) = [1, 2, 2, 6, 6, 6, 6, 7, 10]
integer, parameter :: arr2(2) = [1, 1]

integer :: mode

mode = find_mode(arr1)
write (*,*) "Mode of arr1: ", mode

mode = find_mode(arr2)
write (*,*) "Mode of arr2: ", mode

end program test_find_mode
🌐 Data from online sources
def shortest_distance(distance, start, destination):
    total_distance = sum(distance)
    clockwise_distance = sum(distance[start:destination])

    return min(clockwise_distance, total_distance - clockwise_distance)
  1. Initialize two variables total_distance and clockwise_distance.
  2. Iterate through the input distance array and add the elements into the total_distance.
  3. For elements between the start and the destination, add elements into the clockwise_distance.
  4. After the iteration, we have the total distance of the circle and the distance from start to destination in the clockwise direction.
  5. The counterclockwise distance is then total_distance - clockwise_distance.
  6. Return the minimum of the clockwise and counterclockwise distances.
🌐 Data from online sources
int shortestDistance(vector<int>& distance, int start, int destination) {
    int total_distance = 0, clockwise_distance = 0;

    for (int i = 0; i < distance.size(); ++i) {
        total_distance += distance[i];
        if (i >= start && i < destination) {
            clockwise_distance += distance[i];
        }
    }

    return min(clockwise_distance, total_distance - clockwise_distance);
}
  1. Initialize two variables total_distance and clockwise_distance.
  2. Iterate through the input distance array and add the elements into the total_distance.
  3. For elements between the start and the destination, add elements into the clockwise_distance.
  4. After the iteration, we have the total distance of the circle and the distance from start to destination in the clockwise direction.
  5. The counterclockwise distance is then total_distance - clockwise_distance.
  6. Return the minimum of the clockwise and counterclockwise distances.