Norway


#include <stdio.h>

 

// Function to the of in every sub-array

// of k in the array

void findDistinctCount(int arr[], int n, int k)

{

    // consider every sub-array of size k

    for (int x = 0; x <= n k; x++)

    {

        // maintains counter for distinct elements in current sub-array

        int distinct = 0;

 

        // current sub-array is formed by the sub-array [x, x+k)

        for (int i = x; i < x + k; i++)

        {

            // increase distinct count for arr[i] in current sub-array

            distinct++;

            

            // check if arr[i] is present in the sub-array arr[x,i-1] or not

            for (int j = x; j < i; j++)

            {

                // if duplicate element found in current sub-array

                if (arr[i] == arr[j])

                {

                    // unmark element arr[i] as distinct – decrease count

                    distinctR;;

                    break;

                }

            }

        }

 

        printf(“The count of distinct elements in the sub-array [%d, %d] “

                “is %dn”, x, x + k 1, distinct);

    }

}

 

// main function

int main(void)

{

    int arr[] = { 2, 1, 2, , 2, 1, 4, 5 };

    int k = 5;

 

    int n = sizeof(arr) / sizeof(arr[0]);

    findDistinctCount(arr, n, k);

 

    return 0;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here