Distinct Subarrays with At Most K Odd Elements

Given an array A of N integers, determine the total number of distinct subarrays that contain at most K odd elements.

Example:

Input:
A = [3, 2, 3], K = 1
Output:
4
Explanation:

The distinct subarrays with at most 1 odd element are [3], [3,2], [2,3], and [2].

Input:

First line: single integer T (number of test cases)
For each test case:
First line: two space-separated integers N and K
Second line: N space-separated integers representing array A

Output:

For each test case, output the number of distinct subarrays with at most K odd elements.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 100
  • 0 <= K <= N
  • 1 <= A[i] <= 109 (for each element A[i])
  • Time Limit: 1 sec

Note:

You are not required to print anything. Just implement the provided function.

AnswerBot
4mo

Count the total number of distinct subarrays with at most K odd elements in an array.

  • Iterate through all subarrays and count the number of odd elements in each subarray.

  • Use a hashmap to keep track of ...read more

Help your peers!
Select
Add answer anonymously...

Top Graduate Engineer Trainee (Get) Interview Questions Asked at Jio Platforms

Q. Given the head of a singly linked list, return the middle node of the linked lis...read more
Q. Write code for inorder traversal without using recursion.
Q. Explain decorators in Python.
Graduate Engineer Trainee (Get) Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits