-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion4.c
119 lines (102 loc) · 2.8 KB
/
question4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Given an array A, Count the distinct elements in all windows of size K
METHOD1:
Naive approach: For each window, compare each element in the window with the remaining elements to see it its
repeated. comparison will take O(k^2) time for each window.
Total windows = n-k+1 (dry check)
Time complexity: O(n*k^2)
Space complexity: O(1)
METHOD2:
sort each window and scan each window
Time complexity: O(nklogk)
Space complexity: O(1)
METHOD3:
Create a hash table for each window storing count of each element and scan it and print the total non
repeating elements.
Time complexity: O(nk)
Space complexity: O(k)
METHOD4:
Using a hashtable, but this time we scan the array once, store values in hash table, as we scan, there
is a variable that keeps a check on the count of unique elements. The variable is incremented for everytime,
we encounter a 1 and decremented everytime we encounter greater or less than 1. The hash is searched as window
is moved deleting one element and adding one each time, which may decrease or increase the count
Time complexity: O(n)
Space complexity: O(1)
*/
// //METHOD1
// #include <stdio.h>
// #include <stdlib.h>
// void findUniqueWindowWise(int arr[],int limit, int size){
// int sum = 0, flag = 0;
// int i,j,k;
// for(i=0; i<size-limit+1;i++){
// for(j=i; j<limit+i;j++){
// int temp = arr[j];
// for(k=i;k<limit+i;k++){
// if(k==j){
// continue;
// }else{
// if(temp == arr[k]){
// flag = 1;
// }
// }
// }
// if(!flag){
// sum++;
// }
// flag = 0;
// }
// printf("%d\n", sum);
// sum = 0;
// }
// }
// int main(){
// int arr[] = {10,10,20,30,30,40,10};
// int size = sizeof(arr)/sizeof(arr[0]);
// int k = 3;
// findUniqueWindowWise(arr,k, size);
// return 0;
// }
// //================================================================================================
// //METHOD2
// #include <stdio.h>
// #include <stdlib.h>
// void findUniqueWindowWise(int arr[],int limit, int size){
// int i,j,k, sum=0;
// for(i=0;i<size-limit+1;i++){
// for(j=i+1;j<limit+i;j++){
// //ASSUMING THAT IT IS SORTED
// int temp = arr[j];
// if(temp != arr[j-1]){
// sum++;
// if(sum == limit-1){
// sum++;
// }
// }
// }
// printf("%d\n", sum);
// sum = 0;
// }
// }
// int main(){
// int arr[] = {10,10,20,30,30,40,50};
// int size = sizeof(arr)/sizeof(arr[0]);
// int k = 3;
// findUniqueWindowWise(arr,k, size);
// return 0;
// }
//================================================================================================
//METHOD3
#include <stdio.h>
#include <stdlib.h>
struct node{
};
void findUniqueWindowWise(int arr[],int limit, int size){
}
int main(){
int arr[] = {10,10,20,30,30,40,50};
int size = sizeof(arr)/sizeof(arr[0]);
int k = 3;
findUniqueWindowWise(arr,k, size);
return 0;
}