/**
* Problem: LeetCode 3471 - Find the Largest Almost Missing Integer
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int largestInteger(int[] nums, int k) {
int n = nums.length;
int[] count = new int[51]; // values are 0..50 per constraints
for (int start = 0; start <= n - k; start++) {
boolean[] seen = new boolean[51];
for (int i = start; i < start + k; i++) {
seen[nums[i]] = true;
}
for (int v = 0; v <= 50; v++) {
if (seen[v]) {
count[v]++;
}
}
}
int ans = -1;
for (int v = 0; v <= 50; v++) {
if (count[v] == 1) {
ans = v; // ascending scan, so last match is the largest
}
}
return ans;
}
}
"""
Problem: LeetCode 3471 - Find the Largest Almost Missing Integer
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def largestInteger(self, nums: List[int], k: int) -> int:
n = len(nums)
count = [0] * 51
for start in range(n - k + 1):
seen = set(nums[start:start + k])
for v in seen:
count[v] += 1
ans = -1
for v in range(51):
if count[v] == 1:
ans = v
return ans
/**
* Problem: LeetCode 3471 - Find the Largest Almost Missing Integer
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 3471 - Find the Largest Almost Missing Integer
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int largestInteger(vector<int>& nums, int k) {
int n = nums.size();
vector<int> count(51, 0);
for (int start = 0; start <= n - k; ++start) {
vector<bool> seen(51, false);
for (int i = start; i < start + k; ++i) {
seen[nums[i]] = true;
}
for (int v = 0; v <= 50; ++v) {
if (seen[v]) {
count[v]++;
}
}
}
int ans = -1;
for (int v = 0; v <= 50; ++v) {
if (count[v] == 1) {
ans = v;
}
}
return ans;
}
};
/**
* Problem: LeetCode 3471 - Find the Largest Almost Missing Integer
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var largestInteger = function(nums, k) {
const n = nums.length;
const count = new Int32Array(51);
for (let start = 0; start <= n - k; start++) {
const seen = new Uint8Array(51);
for (let i = start; i < start + k; i++) {
seen[nums[i]] = 1;
}
for (let v = 0; v <= 50; v++) {
if (seen[v] === 1) {
count[v]++;
}
}
}
let ans = -1;
for (let v = 0; v <= 50; v++) {
if (count[v] === 1) {
ans = v;
}
}
return ans;
};