Given an array of integers arr
, a lucky integer is an integer that has a frequency in the array equal to its value.
Return the largest lucky integer in the array. If there is no lucky integer return -1
.
Example 1:
Input: arr = [2,2,3,4] Output: 2 Explanation: The only lucky number in the array is 2 because frequency[2] == 2.
Example 2:
Input: arr = [1,2,2,3,3,3] Output: 3 Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.
Example 3:
Input: arr = [2,2,2,3,3] Output: -1 Explanation: There are no lucky numbers in the array.
Constraints:
1 <= arr.length <= 500
1 <= arr[i] <= 500
program main
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: arr
integer :: i, max_freq, max_val
! Example 1
arr = [2, 2, 3, 4]
write(*, '(A, I0)') 'Example 1: ', max_lucky_int(arr)
! Example 2
arr = [1, 2, 2, 3, 3, 3]
write(*, '(A, I0)') 'Example 2: ', max_lucky_int(arr)
! Example 3
arr = [2, 2, 2, 3, 3]
write(*, '(A, I0)') 'Example 3: ', max_lucky_int(arr)
contains
function max_lucky_int(arr) result(max_val)
implicit none
integer, intent(in) :: arr(:)
integer :: freq(size(arr)), max_freq, max_val
freq = count(arr == arr)
max_freq = maxval(freq)
max_val = -1
do i = 1, size(arr)
if (freq(i) == max_freq .and. arr(i) > max_val) then
max_val = arr(i)
end if
end do
end function max_lucky_int
end program main
temp.f95:9:4: 9 | arr = [2, 2, 3, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 4) temp.f95:13:4: 13 | arr = [1, 2, 2, 3, 3, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 6)
module LuckyInteger
implicit none
private
public :: luckyInteger
contains
integer function luckyInteger(arr) result(largestLuckyInteger)
integer, intent(in) :: arr(:)
integer :: i, frequency(500)
! Initialize frequency array
frequency = 0
! Count frequency of each integer in the array
do i = 1, size(arr)
frequency(arr(i)) = frequency(arr(i)) + 1
end do
! Find the largest lucky integer
largestLuckyInteger = -1
do i = 1, size(arr)
if (frequency(arr(i)) == arr(i)) then
if (largestLuckyInteger < arr(i)) then
largestLuckyInteger = arr(i)
end if
end if
end do
end function
end module
program test_luckyInteger
use LuckyInteger
implicit none
integer :: arr(5)
! Test case 1
arr = [2, 2, 3, 4]
write (*,*) 'Largest lucky integer in ', arr, ' is ', luckyInteger(arr)
! Test case 2
arr = [1, 2, 2, 3, 3, 3]
write (*,*) 'Largest lucky integer in ', arr, ' is ', luckyInteger(arr)
! Test case 3
arr = [2, 2, 2, 3, 3]
write (*,*) 'Largest lucky integer in ', arr, ' is ', luckyInteger(arr)
end program
temp.f95:4:26: 4 | public :: luckyInteger | 1 Error: PUBLIC attribute applied to MODULE luckyinteger at (1) temp.f95:6:33: 6 | integer function luckyInteger(arr) result(largestLuckyInteger) | 1 Error: MODULE attribute of ‘luckyinteger’ conflicts with PROCEDURE attribute at (1) temp.f95:7:37: 7 | integer, intent(in) :: arr(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:36: 8 | integer :: i, frequency(500) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:21: 11 | frequency = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:27: 14 | do i = 1, size(arr) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:53: 15 | frequency(arr(i)) = frequency(arr(i)) + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:11: 16 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:19:32: 19 | largestLuckyInteger = -1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:27: 20 | do i = 1, size(arr) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:21:49: 21 | if (frequency(arr(i)) == arr(i)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:22:54: 22 | if (largestLuckyInteger < arr(i)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:23:48: 23 | largestLuckyInteger = arr(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:24:19: 24 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:15: 25 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:11: 26 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:7: 27 | end function | 1 Error: Expecting END MODULE statement at (1) temp.f95:31:9: 31 | use LuckyInteger | 1 Fatal Error: Cannot open module file ‘luckyinteger.mod’ for reading at (1): No such file or directory compilation terminated.
def find_lucky(arr):
freq = {}
for num in arr:
freq[num] = freq.get(num, 0) + 1
max_lucky = -1
for num, count in freq.items():
if num == count:
max_lucky = max(max_lucky, num)
return max_lucky
The algorithm iterates over the input array, computing the frequency of each number. It then iterates through the frequencies and checks if the number and its frequency match. If they do, it keeps track of the maximum such number. Finally, the algorithm returns the maximum lucky integer. If no lucky integer is found, the function returns -1.
int findLucky(vector<int>& arr) {
unordered_map<int, int> freq;
for (int num : arr) {
freq[num]++;
}
int maxLucky = -1;
for (auto& pair : freq) {
if (pair.first == pair.second) {
maxLucky = max(maxLucky, pair.first);
}
}
return maxLucky;
}
The algorithm iterates over the input array, computing the frequency of each number. It then iterates through the frequencies and checks if the number and its frequency match. If they do, it keeps track of the maximum such number. Finally, the algorithm returns the maximum lucky integer. If no lucky integer is found, the function returns -1.