//Given an array of integers and a number k . Find the count of distinct elements in every window of size k in the array .
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
// Function to count distinct elements in every window of size k
public static int[] countDistinctInWindows(int[] arr, int k) {
int n = arr.length;
int[] result = new int[n - k + 1];
for (int i = 0; i <= n - k; i++) {
int count = 0;
for (int j = 0; j < k; j++) {
int curr = arr[i + j];
boolean distinct = true;
// Check if the current element is distinct within the window
for (int m = 0; m < j; m++) {
if (curr == arr[i + m]) {
distinct = false;
break;
}
}
if (distinct) {
count++;
}
}
result[i] = count;
}
return result;
}
public static void main(String[] args) {
int[] arr = {2, 4, 3, 8, 3, 9, 4, 9};
int k = 4;
int[] result = countDistinctInWindows(arr, k);
System.out.println("Count of distinct elements in windows: " + Arrays.toString(result));
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: