Count Distinct Subarrays With At Most K Odd Elements

You are given an array “A” of N integers. Your task is to find the total number of distinct subarrays of A having at most K odd elements.

Note :
Two subarrays are distinct when they have at least one different element.
Example :
If A = [3,2,3], and K = 1 
then there are 4 subarrays with at most K odd elements:
 [3], [3,2], [2,3],[2]
Input format :
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 

Then the T test cases follow.

The first line of each test case contains single space-separated two integers N and K.

The second line of each test case contains N space-separated integers, denoting the elements of array A.
Output format :
For each test case, print the number of distinct subarrays with at most K odd elements.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10    
1 <= N <= 100
0 <= K <= N
1 <= A[i] <= 10 ^ 9

Where A[i] denotes the 'ith' element of A.

Time Limit: 1sec
CodingNinjas
author
2y
Hashing Approach

In this solution, we try to generate all subarrays and check if the subarray contains at most K odd elements or not. Also, we will build a HashSet of the subarray to check if any subar...read more

Help your peers!
Add answer anonymously...
Jio Platforms Graduate Engineer Trainee (Get) Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter