You are given a 0-indexed integer array nums
. You are also given an integer key
, which is present in nums
.
For every unique integer target
in nums
, count the number of times target
immediately follows an occurrence of key
in nums
. In other words, count the number of indices i
such that:
0 <= i <= nums.length - 2
,nums[i] == key
and,nums[i + 1] == target
.Return the target
with the maximum count. The test cases will be generated such that the target
with maximum count is unique.
Example 1:
Input: nums = [1,100,200,1,100], key = 1 Output: 100 Explanation: For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key. No other integers follow an occurrence of key, so we return 100.
Example 2:
Input: nums = [2,2,2,2,3], key = 2 Output: 2 Explanation: For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key. For target = 3, there is only one occurrence at index 4 which follows an occurrence of key. target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.
Constraints:
2 <= nums.length <= 1000
1 <= nums[i] <= 1000
program main
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: nums = (/ 1, 100, 200, 1, 100 /)
integer :: key = 1
integer :: target
integer :: i
integer :: count
do i = 1, n - 1
if (nums(i) == key .and. nums(i+1) == target) then
count = count + 1
end if
end do
print '(I0)', target
end program main
0
program maxCount
implicit none
integer, parameter :: int32 = selected_int_kind(9)
integer :: i, j, target, key, count, max_count, nums(1000), len
! Read the input
read (*, *) len
do i = 1, len
read (*, *) nums(i)
end do
read (*, *) key
! Initialize the variables
max_count = 0
target = 0
! Iterate over the array
do i = 1, len - 1
! Check if the current element is equal to the key
if (nums(i) == key) then
! Increment the count
count = count + 1
! Check if the next element is equal to the target
if (nums(i + 1) == target) then
! Increment the count
count = count + 1
end if
! Check if the count is greater than the maximum count
if (count > max_count) then
! Update the maximum count and the target
max_count = count
target = nums(i + 1)
end if
end if
end do
! Print the output
write (*, *) target
end program maxCount
At line 8 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7bd27063e960 in ??? #1 0x7bd27063f4d9 in ??? #2 0x7bd27089317b in ??? #3 0x7bd27088c684 in ??? #4 0x7bd27088d2aa in ??? #5 0x5765170f720a in MAIN__ #6 0x5765170f7459 in main
def count_once_in_both(words1, words2):
counter1 = {}
counter2 = {}
for word in words1:
counter1[word] = counter1.get(word, 0) + 1
for word in words2:
counter2[word] = counter2.get(word, 0) + 1
count = 0
for item in counter1.items():
if item[1] == 1 and counter2.get(item[0], 0) == 1:
count += 1
return count
#include <vector>
#include <string>
#include <unordered_map>
int countOnceInBoth(const std::vector<std::string>& words1, const std::vector<std::string>& words2) {
std::unordered_map<std::string, int> counter1, counter2;
for (const auto& word : words1)
counter1[word]++;
for (const auto& word : words2)
counter2[word]++;
int count = 0;
for (const auto& item : counter1)
if (item.second == 1 && counter2[item.first] == 1)
count++;
return count;
}