Max Odd Numbers in K Window

66
0

Given an integer array nums and an integer k, return the maximum number of odd elements in any contiguous subarray of exactly k elements.

Example 1

Input: nums = [2, 3, 3, 2, 9, 2, 5], k = 4
Output: 3
Explanation: The window [3, 3, 2, 9] contains three odd elements. Both occurrences of 3 count.

Example 2

Input: nums = [9, 2, 2, 5, 8, 6], k = 3
Output: 1
Explanation: The odd elements 9 and 5 are three positions apart, so no window of length 3 can contain both. Every valid window contains one odd element.

Brute Force Approach

Check every subarray of size k and count how many odd elements it contains. Start with the first k elements, then move the starting index one position forward and count the odd elements in the next subarray.

For each starting index, reset the count to 0 and examine all k elements. If a value is odd, increment the count. Compare this count with the maximum found so far and update the maximum if the current count is larger.

The last valid starting index is n - k, so every subarray checked contains exactly k elements.

Approach

  1. Initialise maxOddCount to 0.

  2. Loop through each starting index from 0 to n - k, inclusive.

  3. Reset oddCount to 0 for the current subarray.

  4. Examine the next k elements. Increment oddCount whenever an element is odd.

  5. Update maxOddCount if oddCount is greater.

  6. Return maxOddCount after checking all subarrays of size k.

Dry Run

Max Odd Numbers in K Window

Max Odd Numbers in K Window

Solution

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Find the maximum odd count in any contiguous window of size k.
int maxOddinKSizeWindow(vector<int>& nums, int k) {
const int n = static_cast<int>(nums.size());
int maxOddCount = 0;
// The last full window starts at n - k.
for (int start = 0; start <= n - k; ++start) {
// Count the odd elements from scratch for each window.
int oddCount = 0;
for (int i = start; i < start + k; ++i) {
// A nonzero remainder also detects negative odd numbers.
if (nums[i] % 2 != 0) {
++oddCount;
}
}
maxOddCount = max(maxOddCount, oddCount);
}
return maxOddCount;
}
};
int main() {
Solution solution;
vector<int> first = {2, 3, 3, 2, 9, 2, 5};
cout << solution.maxOddinKSizeWindow(first, 4) << '\n'; // Prints 3.
return 0;
}

Complexity Analysis

Time Complexity : O((N - K + 1) × K). There are N - K + 1 windows, and each takes K parity checks. This is also bounded by O(N × K).

Space Complexity : O(1) auxiliary space. The algorithm stores only indices and counters

Optimal Approach

Use a sliding window of size k to track the number of odd elements. First, count the odd elements in the first k positions and use this as the initial maximum.

When the window moves one position to the right, it keeps k - 1 elements from the previous window. Only two elements affect the count. The leftmost element leaves the window, and a new element enters from the right.

If the outgoing element is odd, decrease the count by one. If the incoming element is odd, increase it by one. The updated count now represents the new window. Compare it with the maximum after every shift.

Approach

  1. Count the odd elements in the first k positions and store the result in oddCount.

  2. Initialise maxOddCount with oddCount.

  3. Loop through each incoming index right from k to n - 1. The outgoing index is right - k.

  4. If the outgoing element is odd, decrement oddCount.

  5. If the incoming element is odd, increment oddCount.

  6. Update maxOddCount if the current count is greater.

  7. Return maxOddCount after checking every window of size k.

Dry Run

Max Odd Numbers in K Window Optimal

Max Odd Numbers in K Window Optimal

Solution

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Find the maximum odd count in any contiguous window of size k.
int maxOddinKSizeWindow(vector<int>& nums, int k) {
const int n = nums.size();
int oddCount = 0;
// Count the odd elements in the first window, from index 0 to k - 1.
for (int i = 0; i < k; ++i) {
if (nums[i] % 2 != 0) {
++oddCount;
}
}
int maxOddCount = oddCount;
// At each step, right enters the window and right - k leaves it.
for (int right = k; right < n; ++right) {
const int outgoingIndex = right - k;
if (nums[outgoingIndex] % 2 != 0) {
--oddCount;
}
if (nums[right] % 2 != 0) {
++oddCount;
}
// Compare the new window's count with the best so far.
maxOddCount = max(maxOddCount, oddCount);
}
return maxOddCount;
}
};
int main() {
Solution solution;
vector<int> arr = {2, 3, 3, 2, 9, 2, 5};
cout << solution.maxOddinKSizeWindow(arr, 4) << '\n'; // Prints 3.
return 0;
}

Complexity Analysis

Time Complexity : O(N). Counting the first window takes O(K), and each of the remaining N- K shifts takes constant time.

Space Complexity : O(1) auxiliary space. Only the current count, the maximum, and the indices are tracked.

Interview follow-up Questions

There is one valid window. Its odd count is the answer, and the sliding loop runs zero times.

Sliding WindowArrays

Read Similar Blogs

Comments0