-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion6.c
215 lines (181 loc) · 4.53 KB
/
question6.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Given an array, print all subarrays in the array which has sum 0
METHOD1:
Find all the sub arrays of the array. For each element there are n subarrays. Total elements are n.
Total subarrays = n*n = n^2. For all these subarrays find sum which will take O(n) time in worst case.
Time complexity: O(n^3)
Space complexity: O(1)
METHOD2:
Find the array containing cumulative sum for all the values.
Now find all the sub arrays and find the value for the sum of each sub array from cumulative sum array in constant
time.
Time complexity: O(n^2)
Space complexity: O(n)
METHOD3:
Store the cumulative sum array in a hashtable. Eg. if the sum is 11 is should be stored at 11th index of hashTable
with count 1. If 11 repeats again increment count to two.
Now scan the hashtable and see all values having count greater than 1. That will be the total number of sub arrays.
Time complexity: O(n)
Space complexity: O(n)
*/
//METHOD1
#include <stdio.h>
#include <stdlib.h>
int findSum(int temp[], int start,int size){
int sum = 0;
for(int i=start; i<=size;i++){
sum += temp[i];
}
return sum;
}
void printIt(int temp[], int start,int size){
for(int i=start; i<=size; i++){
printf("%d\t",temp[i]);
}
printf("\n-----------------\n");
}
void printSubArrays(int arr[], int size){
int sum;
int temp[size];
for(int i=0; i<size-1; i++){
temp[i] = arr[i];
for(int j=i+1;j<size;j++){
temp[j] = arr[j];
if(!findSum(temp, i,j)){
printIt(temp,i,j);
}
}
}
}
int main(){
int arr[] = {11,10,-5,-3,-2,10,5,-1,-6};
int size = sizeof(arr)/sizeof(arr[0]);
printSubArrays(arr,size);
return 0;
}
//============================================================================================
//METHOD2
#include <stdio.h>
#include <stdlib.h>
int findSum(int arr[], int start, int end){
return arr[end]-arr[start-1];
}
void printIt(int arr[], int start, int end){
for(int i=start;i<=end; i++){
printf("%d\t",arr[i]);
}
printf("\n====================\n");
}
void printSubArrays(int arr[], int size){
int cumSum[size], sum=0;
for(int i=0; i<size;i++){
sum += arr[i];
cumSum[i] = sum;
}
for(int i=0; i<size-1;i++){
for(int j=i+1; j<size;j++){
if(!findSum(cumSum, i,j)){
printIt(arr,i,j);
}
}
}
}
int main(){
int arr[] = {11,10,-5,-3,-2,10,5,-1,-6};
int size = sizeof(arr)/sizeof(arr[0]);
printSubArrays(arr,size);
return 0;
}
//========================================================================================
//METHOD3 (done using open addressing with a twist but linked list used still)
#include <stdio.h>
#include <stdlib.h>
struct hash *hashTable = NULL;
struct hash{
int num,key;
struct node *head;
};
struct node{
int end;
struct node *next;
};
void printIt(int arr[], int start, int end){
for(int i=start;i<=end; i++){
printf("%d\t",arr[i]);
}
printf("\n====================\n");
}
void insertInHash(int size, int key, int end){
int index = key%size;
int position = index;
for(int count = 0; count<size;count++){
if(hashTable[position].num == 0){
hashTable[position].key = key;
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->next = NULL;
newnode->end = end;
hashTable[position].head = newnode;
hashTable[position].num++;
break;
}else{
if(hashTable[position].key == key){
hashTable[position].num++;
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->end = end;
newnode->next = hashTable[position].head;
hashTable[position].head = newnode;
break;
}else{
position = (index + count)%size;
}
}
}
}
void printList(int arr[],int index){
struct node *t = hashTable[index].head;
int start,end;
while(t){
end = t->end;
t=t->next;
start = t->end+1;
for(int i=start;i<=end;i++){
printf("%d\t", arr[i]);
}
printf("\n");
if(!t->next){
break;
}
}
}
void displayHash(int size){
for(int i=0; i<size;i++){
printf("POSITION %d\n", hashTable[i].num);
printf("KEY %d \n", hashTable[i].key);
}
printf("==============================\n");
}
void printSubArrays(int arr[], int size){
int hashSize = size+1;
hashTable = (struct hash *)calloc(hashSize,sizeof(struct hash));
// displayHash(hashSize);
int cumSum[size], sum=0;
for(int i=0; i<size; i++){
sum += arr[i];
cumSum[i] = sum;
}
for(int j=0; j<size;j++){
insertInHash(hashSize,cumSum[j],j);
}
// displayHash(hashSize);
for(int k=0; k<hashSize; k++){
if(hashTable[k].num > 1){
printList(arr,k);
}
}
}
int main(){
int arr[] = {11,10,-5,-3,-2,10,5,-1,-6};
int size = sizeof(arr)/sizeof(arr[0]);
printSubArrays(arr,size);
return 0;
}