https://www.gravatar.com/avatar/0e2f7fc4c0996f33a4ee09b16f2ebb14?s=240&d=mp

CODEPHILOSOPHER

compute|

Implementing runtime-re-sizable dynamic array in C

You can implement your own dynamic array using malloc, realloc. 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 #include <stdio.h>#include <stdlib.h> typedef struct { int *data; size_t size; size_t capacity; } DynamicArray; void initArray(DynamicArray *arr, size_t initialCapacity){ arr->data = (int*)malloc(initialCapacity*sizeof(int)); arr->size = 0; arr->capacity = initialCapacity; } void resizeArray(DynamicArray *arr, size_t newCapacity) { arr->data = (int*)realloc(arr->data, newCapacity*sizeof(int)); arr->capacity = newCapacity; } void addElement(DynamicArray *arr, int element) { if (arr->size >= arr->capacity) { resizeArray(arr, arr->capacity*2); } arr->data[arr->size++] = element; } void freeMem(DynamicArray *arr) { free(arr->data); arr->data = NULL; arr->size = 0; arr->capacity = 0; } int main() { DynamicArray arr; initArray(&arr, 4); for (int i = 1; i <= 10; i++) { addElement(&arr, i*10); } for (size_t i = 0; i < arr.

Two Pair Sum

#pairs Given an array containing N integers and an number S denoting a target Sum. Find two distinct integers that can pair up to form target sum. Let us assume there will be only one such pair. Input: array = [10, 5, 2, 3, -6, 9, 11 ] S = 4 output: [10, -6] Solution: Naive Approach: We can use two loops which will compare each element with other elements that gives the sum.