Untitled

 avatar
unknown
javascript
a year ago
873 B
5
Indexable
function solution(numbers, k) {
    let count = 0;

    for (let start = 0; start < numbers.length; start++) {
        const freq = new Map();

        for (let end = start; end < numbers.length; end++) {
            const num = numbers[end];
            const prevCount = freq.get(num) || 0;
            freq.set(num, prevCount + 1);

            // Calculate the number of pairs for the current number
            let numPairs = Math.floor(prevCount / 2);

            // If we formed a new pair with the current number, check if we have k pairs in total
            if (prevCount % 2 === 1 && (prevCount + 1) % 2 === 0) {
                let totalPairs = Array.from(freq.values()).reduce((acc, val) => acc + Math.floor(val / 2), 0);
                if (totalPairs >= k) {
                    count++;
                }
            }
        }
    }

    return count;
}
Editor is loading...
Leave a Comment