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
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
temp.f95:6:31: 6 | integer :: i, j, k, l, m, n, o | 1 Error: Symbol βnβ at (1) already has basic type of INTEGER
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
temp.f95:50:29: 50 | integer, parameter :: arr1(8) = [1, 2, 2, 6, 6, 6, 6, 7, 10] | 1 Error: Different shape for array assignment at (1) on dimension 1 (8 and 9) temp.f95:55:21: 55 | mode = find_mode(arr1) | 1 Error: Symbol βarr1β at (1) has no IMPLICIT type; did you mean βarr2β?
def shortest_distance(distance, start, destination):
total_distance = sum(distance)
clockwise_distance = sum(distance[start:destination])
return min(clockwise_distance, total_distance - clockwise_distance)
total_distance
and clockwise_distance
.distance
array and add the elements into the total_distance
.start
and the destination
, add elements into the clockwise_distance
.start
to destination
in the clockwise direction.total_distance - clockwise_distance
.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);
}
total_distance
and clockwise_distance
.distance
array and add the elements into the total_distance
.start
and the destination
, add elements into the clockwise_distance
.start
to destination
in the clockwise direction.total_distance - clockwise_distance
.