Skip to content
Closed
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ C
- QuickSort
- SelectionSort
- ShakerSort
- StoogeSort
- HeapSort

## Hashing
Expand Down
3 changes: 3 additions & 0 deletions leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ LeetCode
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [C](./src/160.c)|Easy|
|169|[Majority Element](https://leetcode.com/problems/majority-element/) | [C](./src/169.c)|Easy|
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [C](./src/173.c)|Medium|
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C](./src/190.c)|Easy|
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C](./src/191.c)|Easy|
|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [C](./src/203.c)|Easy|
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [C](./src/206.c)|Easy|
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c)|Medium|
Expand All @@ -46,6 +48,7 @@ LeetCode
|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C](./src/389.c)|Easy|
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C](./src/404.c)|Easy|
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [C](./src/442.c)|Medium|
|461|[Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C](./src/461.c) |Easy|
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [C](./src/509.c)|Easy|
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C](./src/520.c)|Easy|
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c)|Easy|
Expand Down
16 changes: 8 additions & 8 deletions leetcode/src/1189.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ int maxNumberOfBalloons(char * text){
int i, min_counter_ballons;

for (char *ptr = text; *ptr; ptr++) {
if(*ptr == 'b') {
if (*ptr == 'b') {
count_letters[0]++;
}else if(*ptr == 'a') {
} else if(*ptr == 'a') {
count_letters[1]++;
}else if(*ptr == 'l') {
} else if (*ptr == 'l') {
count_letters[2]++;
}else if(*ptr == 'o') {
} else if(*ptr == 'o') {
count_letters[3]++;
}else if(*ptr == 'n') {
} else if(*ptr == 'n') {
count_letters[4]++;
}
}
Expand All @@ -29,10 +29,10 @@ int maxNumberOfBalloons(char * text){

/* Max number of times which we can write ballon is equal to min value of letters on count_letter */
min_counter_ballons = count_letters[0];
for(i = 1; i < 5; i++){
if(count_letters[i] < min_counter_ballons)
for (i = 1; i < 5; i++) {
if (count_letters[i] < min_counter_ballons)
min_counter_ballons = count_letters[i];
}

return min_counter_ballons;
}
}
13 changes: 13 additions & 0 deletions leetcode/src/190.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
uint32_t reverseBits(uint32_t n) {
uint TotalBits = 32;
uint32_t reverse_int = 0; //stored in memory as 32 bits, each bit valued 0
uint i;
for(i = 0; i < TotalBits; i++) {
if((n & (UINT32_C(1) << i))) //if the bit on the ith position of 32 bit input is 1, then proceed
//Further note the use of UINT32_C to convert 1 to unsigned 32 bit int, since just 1 is treated as int which cannot be shifted left more than 30 times
reverse_int = reverse_int | (UINT32_C(1) << (TotalBits - 1 - i)); //Convert the ith bit from the end in reverse_int from 0 to 1, if ith bit from beginning in n is 1
//This is achieved by using bitwise OR on reverse_int (where ith bit from end is currently 0) and
//1 shifted left 31 - i bits (to ith bit from the end)
}
return reverse_int;
}
10 changes: 10 additions & 0 deletions leetcode/src/191.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
int hammingWeight(uint32_t n) {
int TotalBits = 32;
int i, weight = 0;
for(i = 0; i < TotalBits; i++) {
if(n & (UINT32_C(1) << i)) //if the bit on the ith position of 32 bit input is 1, then proceed
//Further note the use of UINT32_C to convert 1 to unsigned 32 bit int, as just 1 is treated as int which cannot be shifted left more than 30 times
weight += 1;
}
return weight;
}
10 changes: 6 additions & 4 deletions leetcode/src/283.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
void moveZeroes(int* nums, int numsSize){
int i, start = 0;
void moveZeroes(int* nums, int numsSize) {
int i = 0, start = 0;

for (i = 0; i < numsSize; i++) {
if(nums[i])
if (nums[i])
nums[start++] = nums[i];
}
for(;start < numsSize; start++) {

for (start; start < numsSize; start++) {
nums[start] = 0;
}
}
12 changes: 12 additions & 0 deletions leetcode/src/461.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
int hammingDistance(int x, int y){
int difference = x ^ y; //The XOR operator generates the bitwise difference in the binary representation of two numbers
//If bit in ith position of both numbers is same, bit in difference is 0, otherwise 1
int TotalBits = sizeof(difference)*8; //total number of bits
int i, distance = 0;
for(i = 0; i < TotalBits; i++) {
if(difference & (UINT32_C(1) << i)) //if the bit on the ith position of 32 bit input is 1, then proceed
//Further note the use of UINT32_C to convert 1 to unsigned 32 bit int, as just 1 is treated as int which cannot be shifted left more than 30 times
distance += 1;
}
return distance;
}
2 changes: 1 addition & 1 deletion leetcode/src/617.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct TreeNode * newNode (int item) {
}

struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2){
if(t1 == NULL && t2 == NULL)
if (t1 == NULL && t2 == NULL)
return NULL;
int item = (t1 == NULL ? 0 : t1->val) + (t2 == NULL ? 0 : t2->val);
struct TreeNode *node = newNode(item);
Expand Down
8 changes: 5 additions & 3 deletions leetcode/src/700.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
struct TreeNode* searchBST(struct TreeNode* root, int val){
if(!root)
return NULL;
if(root->val == val)

if (root->val == val) {
return root;
else if (root->val > val)
} else if (root->val > val) {
return searchBST(root->left, val);
else
} else {
return searchBST(root->right, val);
}
}
7 changes: 4 additions & 3 deletions leetcode/src/704.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ int search(int* nums, int numsSize, int target){
int low = 0, high = numsSize - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (target > nums[mid])
if (target > nums[mid]) {
low = mid + 1;
else if (target < nums[mid])
} else if (target < nums[mid]) {
high = mid - 1;
else
} else {
return mid;
}
}
return -1;
}
Expand Down
18 changes: 9 additions & 9 deletions leetcode/src/771.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// for strlen( )
#include<string.h>
// for strlen()
#include <string.h>

int numJewelsInStones(char * j, char * s){
// as strlen is O(n), store it once rather than using it in for loop
int cnt[500],lens=strlen(s),lenj=strlen(j),sol=0;
memset(cnt,0,sizeof(cnt));
int numJewelsInStones(char * j, char * s) {
// as strlen is O(n), store it once rather than using it in for loop
int cnt[500], lens = strlen(s), lenj = strlen(j), sol = 0;
memset(cnt, 0, sizeof(cnt));

// lookup to know which character occurs in j
for(int i=0;i<lenj;i++)
for (int i = 0; i < lenj; i++)
cnt[j[i]]++;

// count the characters in s
for(int i=0;i<lens;i++)
sol+=cnt[s[i]];
for (int i = 0; i < lens; i++)
sol += cnt[s[i]];

return sol;
}
Expand Down
8 changes: 5 additions & 3 deletions leetcode/src/82.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
struct ListNode* deleteDuplicates(struct ListNode* head) {
if(head == NULL)
if (head == NULL)
return NULL;
if(head->next && head->val == head->next->val) {

if (head->next && head->val == head->next->val) {
/* Remove all duplicate numbers */
while(head->next && head->val == head->next->val)
while (head->next && head->val == head->next->val) {
head = head -> next;
}
return deleteDuplicates(head->next);
} else {
head->next = deleteDuplicates(head->next);
Expand Down
4 changes: 2 additions & 2 deletions leetcode/src/905.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
*
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sortArrayByParity(int* A, int ASize, int* returnSize){
int* sortArrayByParity(int* A, int ASize, int* returnSize) {
int *retArr = malloc(ASize * sizeof(int));
int oddIndex = ASize - 1;
int evenIndex = 0;
*returnSize = ASize;
for (int i = 0; i < ASize; i++) {
if(A[i] % 2 == 0) {
if (A[i] % 2 == 0) {
retArr[evenIndex] = A[i];
evenIndex++;
} else {
Expand Down
8 changes: 4 additions & 4 deletions leetcode/src/938.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
int rangeSumBST(struct TreeNode* root, int L, int R){
if (root == NULL)
if (root == NULL) {
return 0;
else if (root->val >= L && root->val <= R)
} else if (root->val >= L && root->val <= R) {
return root->val + rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);
else
} else {
return rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);

}
}
7 changes: 4 additions & 3 deletions leetcode/src/977.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* 1st way: Using 2 pointer */
/* 1st way: Using 2 pointers */
int* sortedSquares(int* A, int ASize, int* returnSize){
int i, start = 0, end = ASize - 1;
int *res = malloc(ASize * sizeof(int));
*returnSize = ASize;
for (i = ASize - 1; i >= 0; i--) {
if(abs(A[start]) > A[end]) {
if (abs(A[start]) > A[end]) {
res[i] = A[start] * A[start];
start++;
} else {
Expand All @@ -19,7 +19,8 @@ int* sortedSquares(int* A, int ASize, int* returnSize){
int cmpval(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int* sortedSquares(int* A, int ASize, int* returnSize){

int* sortedSquares(int* A, int ASize, int* returnSize) {
int *res = malloc(ASize * sizeof(int));
for (int i = 0; i < ASize; i++)
res[i] = A[i] * A[i];
Expand Down
79 changes: 79 additions & 0 deletions sorting/StoogeSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Sorting of array list using stooge sort
#include <stdio.h>
#include <stdlib.h>

// Displays the array, passed to this method
void display(int arr[], int n)
{

int i;
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

printf("\n");

}

// Swap function to swap two values
void swap(int *first, int *second)
{

int temp = *first;
*first = *second;
*second = temp;

}

// Function to sort the array using stooge sort
void stoogesort(int arr[], int l, int h)
{
if (l >= h)
return;

// If first element is smaller than last, swap them
if (arr[l] > arr[h])
swap(&arr[l], &arr[h]);

// If there are more than 2 elements in the array
if (h - l + 1 > 2)
{
int t = (h - l + 1) / 3;

// Recursively sort first 2/3 elements
stoogesort(arr, l, h - t);

// Recursively sort last 2/3 elements
stoogesort(arr, l + t, h);

// Recursively sort first 2/3 elements
// again to confirm
stoogesort(arr, l, h - t);
}
}

// Driver program to test above function
int main()
{
int n; // Size of array elements
int arr[n];

printf("Enter size of array:\n");
scanf("%d", &n); // E.g. 8

printf("Enter the elements of the array\n");
for(int i = 0; i < n; i++)
{
scanf("%d", &arr[i] );
}

printf("Original array: ");
display(arr, n);

stoogesort(arr, 0, n - 1);
printf("Sorted array: ");
display(arr, n);

return 0;
}