Subarray c program odd integers count

Problem Statement

Write a c program which accepts N integers and X integer as input and the program must print the count of subarray having exactly X odd integer as output.

Solution

#include<stdio.h>

int main()

{

    int n,x,i,j,k,sum=0,count;

    scanf("%d%d",&n,&x);

    int a[n];

    for(i=0;i<n;i++)

    {

        scanf("%d",&a[i]);

    }

    for(i=0;i<n;i++)

    {

        for(j=i;j<n;j++)

        {

            count=0;

            for(k=i;k<=j;k++)

            {

                if(a[k]%2!=0)

                count++;

            }

            if(count==x)

            sum++;

        }

    }

    printf("%d",sum);  

}

Input

4 2

3 4 3 1

Output

3

Explanation

(3 4 3),(4 3 1),(3 1) are subarrays having x count odd integers.

Input 2

5 3

1 2 2 3 4

Output 2

0

Rajesh

This Blog will help you to find out the c programs solution easily and also you can able to understand the program.Please follow our blog for more useful updates.

Post a Comment (0)
Previous Post Next Post