|
| 1 | +#include<iostream> |
| 2 | +using namespace std; |
| 3 | +struct Array { |
| 4 | + int* A; |
| 5 | + int size; |
| 6 | + int length; |
| 7 | +}; |
| 8 | + |
| 9 | +void Swap(int* x, int* y) { |
| 10 | + int temp = *x; |
| 11 | + *x = *y; |
| 12 | + *y = temp; |
| 13 | +} |
| 14 | + |
| 15 | +void Display(struct Array arr) { |
| 16 | + cout << "The elements of the array is !" << endl; |
| 17 | + for (int i = 0; i < arr.length; i++) |
| 18 | + cout << arr.A[i]<<" "; |
| 19 | +} |
| 20 | + |
| 21 | +void Append(struct Array *arr, int x) { |
| 22 | + if (arr->length < arr->size) { |
| 23 | + arr->A[arr->length] = x; |
| 24 | + arr->length++; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +void Insert(struct Array* arr, int index, int val) { |
| 29 | + for (int i = arr->length; i > index; i--) |
| 30 | + arr->A[i] = arr->A[i - 1]; |
| 31 | + arr->A[index] = val; |
| 32 | + arr->length++; |
| 33 | +} |
| 34 | + |
| 35 | +void Delete(struct Array* arr, int index) { |
| 36 | + int x = arr->A[index]; |
| 37 | + for (int i = index; i < arr->length - 1; i++) |
| 38 | + arr->A[i] = arr->A[i + 1]; |
| 39 | + arr->length--; |
| 40 | +} |
| 41 | + |
| 42 | +int L_Search(struct Array* arr, int key) { |
| 43 | + for (int i = 0; i < arr->length; i++) { |
| 44 | + if (key == arr->A[i]) |
| 45 | + return i; |
| 46 | + } |
| 47 | + return -1; |
| 48 | +} |
| 49 | + |
| 50 | +int Improved_L_Search(struct Array* arr, int key) { |
| 51 | + for (int i = 0; i < arr->length; i++) { |
| 52 | + if (key == arr->A[i]) { |
| 53 | + Swap(&arr->A[0], &arr->A[i]); |
| 54 | + return i; |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + return -1; |
| 59 | +} |
| 60 | + |
| 61 | +int Binar_search(struct Array* arr, int key) |
| 62 | +{ |
| 63 | + int l = 0; |
| 64 | + int r = arr->length - 1; |
| 65 | + while (l <= r) { |
| 66 | + int mid = (l + r) / 2; |
| 67 | + if (key == arr->A[mid]) |
| 68 | + return mid; |
| 69 | + else if (key < arr->A[mid]) |
| 70 | + r = mid - 1; |
| 71 | + else |
| 72 | + l = mid + 1; |
| 73 | + } |
| 74 | + return -1; |
| 75 | +} |
| 76 | + |
| 77 | +int R_B_Search(struct Array* arr, int low, int high, int key) { |
| 78 | + if (low < high) { |
| 79 | + int mid = low + high / 2; |
| 80 | + if (key == arr->A[mid]) |
| 81 | + return mid; |
| 82 | + else if (key < arr->A[mid]) |
| 83 | + return R_B_Search(arr, low, mid - 1, key); |
| 84 | + else |
| 85 | + return R_B_Search(arr, mid + 1, high, key); |
| 86 | + } |
| 87 | + return -1; |
| 88 | +} |
| 89 | + |
| 90 | +int Get(struct Array* arr, int index) { |
| 91 | + if (arr->length < arr->size) { |
| 92 | + return arr->A[index]; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void Set(struct Array* arr, int index, int val) { |
| 97 | + if (arr->length < arr->size) { |
| 98 | + arr->A[index] = val; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +int Min(struct Array* arr) { |
| 103 | + int min = INT_MAX; |
| 104 | + for (int i = 0; i < arr->length; i++) { |
| 105 | + if (arr->A[i] < min) |
| 106 | + min = arr->A[i]; |
| 107 | + } |
| 108 | + return min; |
| 109 | +} |
| 110 | + |
| 111 | +int Max(struct Array* arr) { |
| 112 | + int max = INT_MIN; |
| 113 | + for (int i = 0; i < arr->length; i++) |
| 114 | + if (arr->A[i] > max) |
| 115 | + max = arr->A[i]; |
| 116 | + return max; |
| 117 | +} |
| 118 | + |
| 119 | +int Count(struct Array* arr) { |
| 120 | + int count = 0; |
| 121 | + for (int i = 0; i < arr->length; i++) { |
| 122 | + count++; |
| 123 | + } |
| 124 | + return count; |
| 125 | +} |
| 126 | + |
| 127 | +int Sum(struct Array* arr) { |
| 128 | + int sum = 0; |
| 129 | + for (int i = 0; i < arr->length; i++) |
| 130 | + sum += arr->A[i]; |
| 131 | + return sum; |
| 132 | +} |
| 133 | + |
| 134 | +int Avg(struct Array* arr) { |
| 135 | + return Sum(arr) / Count(arr); |
| 136 | +} |
| 137 | + |
| 138 | +void Reverse1(struct Array* arr) { |
| 139 | + int* B; |
| 140 | + int j; |
| 141 | + B = new int[arr->length]; |
| 142 | + for (int i = arr->length-1,j=0; i>=0; i--,j++) |
| 143 | + { |
| 144 | + B[j] = arr->A[i]; |
| 145 | + } |
| 146 | + for (int i =0; i <arr->length; i++) |
| 147 | + { |
| 148 | + arr->A[i] = B[i]; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void Reverse2(struct Array* arr) { |
| 153 | + int i, j; |
| 154 | + for (i = 0, j = arr->length - 1; i < j; i++, j--) |
| 155 | + Swap(&arr->A[i], &arr->A[j]); |
| 156 | +} |
| 157 | + |
| 158 | +void Insert(struct Array* arr, int val) { |
| 159 | + int i = arr->length-1; |
| 160 | + while(arr->A[i] > val) { |
| 161 | + arr->A[i + 1] = arr->A[i]; |
| 162 | + i--; |
| 163 | + } |
| 164 | + arr->A[i + 1] = val; |
| 165 | + arr->length++; |
| 166 | +} |
| 167 | + |
| 168 | +bool IsSorted(struct Array* arr) { |
| 169 | + for (int i = 0; i < arr->length - 1; i++) { |
| 170 | + if (arr->A[i] > arr->A[i + 1]) |
| 171 | + return false; |
| 172 | + } |
| 173 | + return true; |
| 174 | +} |
| 175 | + |
| 176 | +void Arragenegetive(struct Array* arr) { |
| 177 | + int i = 0; |
| 178 | + int j = arr->length - 1; |
| 179 | + while (i < j) { |
| 180 | + while (arr->A[i] < 0) { i++; } |
| 181 | + while (arr->A[j] > 0) { j--; } |
| 182 | + if (i < j) |
| 183 | + Swap(&arr->A[i], &arr->A[j]); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +void SingleMissingElement(struct Array* arr) { |
| 188 | + int sum = 0; |
| 189 | + int n = arr->A[arr->length-1]; // this denotes the last element of the array |
| 190 | + for (int i = 0; i < arr->length; i++) |
| 191 | + { |
| 192 | + sum += arr->A[i]; // sum of the existing array |
| 193 | + } |
| 194 | + // (n*(n+1))/2 ----> Gives the total sum upto that number |
| 195 | + int target = (n * (n + 1)) / 2- sum; |
| 196 | + cout << "The missing number is " << target; |
| 197 | + |
| 198 | +} |
| 199 | + |
| 200 | +// another method for finding the missing number |
| 201 | + |
| 202 | +void SingleMissingNumber1(struct Array* arr) { |
| 203 | + int low = arr->A[0]; // take the 1st element of the array |
| 204 | + int difference = low - 0;// the difference should be constant if not then add the index & diffence to get missing number |
| 205 | + |
| 206 | + for (int i = 0; i < arr->length; i++) |
| 207 | + { |
| 208 | + if (arr->A[i] - i != difference) { |
| 209 | + cout << "Missing element is " << i + difference; |
| 210 | + break; |
| 211 | + } |
| 212 | + |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +void MultipleMissingElement(struct Array* arr) { |
| 217 | + int low = arr->A[0]; |
| 218 | + int difference = low - 0; |
| 219 | + for (int i = 0; i < arr->length; i++) { |
| 220 | + if (arr->A[i] - i != difference) { |
| 221 | + while (difference < arr->A[i] - i) |
| 222 | + { |
| 223 | + cout << i + difference<<" "; |
| 224 | + difference++; |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | + |
| 231 | +// this is for the unsorted array |
| 232 | +void Missingelemet_Hash_table(struct Array* arr) { |
| 233 | + int* H; |
| 234 | + int i; |
| 235 | + int low = Min(arr); |
| 236 | + int high = Max(arr); |
| 237 | + H = new int[high]; |
| 238 | + for (int i = 0; i < high; i++)H[i] = { 0 }; |
| 239 | + for (i = 0; i < arr->length-1; i++) { |
| 240 | + H[arr->A[i]]++; |
| 241 | + } |
| 242 | + for (i = Min(arr); i <= high; i++) { |
| 243 | + if (H[i] == 0) |
| 244 | + cout << i<<" "; |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +void Find_Duplicate(struct Array* arr) { |
| 249 | + int last_duplicate = 0;// initially set duplicate as zero |
| 250 | + for (int i = 0; i < arr->length; i++) { // iterate through the list of elements |
| 251 | + if (arr->A[i] == arr->A[i + 1] && arr->A[i] != last_duplicate) // if consequtive elements are equal then consider as duplicate |
| 252 | + { |
| 253 | + cout << arr->A[i]<<" "; |
| 254 | + last_duplicate = arr->A[i]; // everytine after printing duplicate update it will next duplicate element |
| 255 | + } |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +void Count_Find_duplicate(struct Array* arr) { |
| 260 | + int i; |
| 261 | + for (i = 0; i < arr->length - 1; i++) { |
| 262 | + if (arr->A[i] == arr->A[i + 1]) // if the consequtive term of the array is equal |
| 263 | + { |
| 264 | + int j = i + 1; // set j one next to i and increment as long as both are equal |
| 265 | + while (arr->A[j] == arr->A[i]) j++; |
| 266 | + cout << arr->A[i] << " is duplicate element and appering for " << j - i << " Times";// j-i will give total number of duplicate element |
| 267 | + i = j - 1; // set i before one before j |
| 268 | + } |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +void Count_Find_duplicate_hash(struct Array* arr) { |
| 273 | + int* H; |
| 274 | + H = new int[Max(arr)]{ 0 }; |
| 275 | + for (int i = 0; i < Max(arr); i++) |
| 276 | + H[i] = { 0 }; |
| 277 | + for (int i = 0; i < arr->length; i++) |
| 278 | + H[arr->A[i]]++; |
| 279 | + for (int i = Min(arr); i <= Max(arr); i++) { |
| 280 | + if (H[i] > 1) |
| 281 | + cout << i << " is duplicate element and appering for " << H[i] << " Times"<<endl; |
| 282 | + } |
| 283 | +} |
| 284 | + |
| 285 | +// Finding duplicate for unsorted array |
| 286 | +void Count_duplicate_unsorted(struct Array* arr) { |
| 287 | + |
| 288 | + for (int i = 0; i < arr->length - 1; i++) { |
| 289 | + int count = 1; // intializing count as 1 |
| 290 | + if (arr->A[i] != -1) { // if element is already checkd then dont go for checking the elements |
| 291 | + for (int j = i + 1; j < arr->length; j++) |
| 292 | + { |
| 293 | + if (arr->A[i] == arr->A[j]) // if duplicate element is found |
| 294 | + { |
| 295 | + count++; // increament the count |
| 296 | + arr->A[j] = -1; // and mark element as -1 so that next time it won't confuse |
| 297 | + } |
| 298 | + } |
| 299 | + if (count > 1) // if count is greater then i.e there is duplicate element |
| 300 | + cout << arr->A[i] << " is appearing for " << count << " Times" << endl; |
| 301 | + } |
| 302 | + } |
| 303 | +} |
| 304 | + |
| 305 | +void Count_duplicate_unsorted_hash(struct Array* arr) { |
| 306 | + int low = Min(arr); |
| 307 | + int high = Max(arr); |
| 308 | + int* H; |
| 309 | + H = new int[high];; |
| 310 | + for (int i = 0; i < high; i++) H[i]={ 0 }; // initialise hash table with zero |
| 311 | + for (int i = 0; i < arr->length; i++) |
| 312 | + H[arr->A[i]]++; // for every element go to that particular index and make increment everytime |
| 313 | + for (int i = low; i <= high; i++) // traverse through the whole hash table and check the elements |
| 314 | + { |
| 315 | + |
| 316 | + if (H[i] > 1) // if hash table elements is greater then 1 that is there is the duplicate element in the array |
| 317 | + cout << i << " is appearing for " << H[i] << " Times" << endl; // print the duplicate and also print its frequency |
| 318 | + } |
| 319 | + |
| 320 | +} |
| 321 | + |
| 322 | +int main() { |
| 323 | + struct Array arr; |
| 324 | + int no; |
| 325 | + cout << "Enter the size of the array " << endl; |
| 326 | + cin >> arr.size; |
| 327 | + arr.A = new int[arr.size]; |
| 328 | + arr.length = 0; |
| 329 | + cout << "Enter the size of the array" << endl; |
| 330 | + cin >> no; |
| 331 | + cout << "Enter the elements of the array " << endl; |
| 332 | + for (int i = 0; i < no; i++) |
| 333 | + cin >> arr.A[i]; |
| 334 | + arr.length = no; |
| 335 | + |
| 336 | + // Append(&arr, 5); |
| 337 | + //Insert(&arr, 2, 7); |
| 338 | + // Delete(&arr, 2); |
| 339 | + |
| 340 | + //cout <<"The element is present at the index "<<L_Search(&arr, 5)<<endl; |
| 341 | + //cout <<"The element is present at the index "<< Improved_L_Search(&arr, 5)<<endl; |
| 342 | + //cout <<"The element is present at the index "<< Binar_search(&arr, 60)<<endl; |
| 343 | + //cout <<"The element is present at the index "<< R_B_Search(&arr, 0,arr.length-1,80)<<endl; |
| 344 | + |
| 345 | + //cout <<"The element at the index is "<< Get(&arr, 2)<<endl; |
| 346 | + //Set(&arr, 6, 55); |
| 347 | + /*cout << "The minimum element in the array is " << Min(&arr)<<endl; |
| 348 | + cout << "The maximum element in the array is " << Max(&arr)<<endl; |
| 349 | + cout << "Total number of element in the array is " << Count(&arr)<<endl; |
| 350 | + cout << "Total number of element in the array is " << Sum(&arr)<<endl; |
| 351 | + cout << "Total number of element in the array is " << Avg(&arr)<<endl;*/ |
| 352 | + |
| 353 | + //Reverse1(&arr); |
| 354 | + //Reverse2(&arr); |
| 355 | + |
| 356 | + //Insert(&arr, 87); |
| 357 | + |
| 358 | + /*if (IsSorted(&arr) == 0) |
| 359 | + cout << "Array is not sorted" << endl; |
| 360 | + else |
| 361 | + cout << "Array is sorted" << endl;*/ |
| 362 | + |
| 363 | + //Arragenegetive(&arr); |
| 364 | + |
| 365 | + // SingleMissingElement(&arr); |
| 366 | + //SingleMissingNumber1(&arr); |
| 367 | + |
| 368 | + /*cout << "The missing elements are ! " << endl; |
| 369 | + MultipleMissingElement(&arr);*/ |
| 370 | + |
| 371 | + //Missingelemet_Hash_table(&arr); |
| 372 | + |
| 373 | + /*cout << "The duplicate elements are ! " << endl; |
| 374 | + Find_Duplicate(&arr);*/ |
| 375 | + //Count_Find_duplicate(&arr); |
| 376 | + |
| 377 | + //Count_Find_duplicate_hash(&arr); |
| 378 | + |
| 379 | + //Count_duplicate_unsorted(&arr); |
| 380 | + |
| 381 | + Count_duplicate_unsorted_hash(&arr); |
| 382 | + |
| 383 | + |
| 384 | + // Display(arr); |
| 385 | + return 0; |
| 386 | + |
| 387 | +} |
0 commit comments