-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'AlgoGenesis:main' into main
- Loading branch information
Showing
36 changed files
with
2,295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Intersection of Two Arrays in C | ||
|
||
This project contains a C program that calculates the intersection of two integer arrays, provided by the user. The program takes the sizes and elements of two arrays as input, finds their intersection, and outputs the common elements. | ||
|
||
## Process | ||
|
||
1. **User Input**: | ||
- The user is prompted to enter the number of elements and the elements themselves for two arrays, `arr1` and `arr2`. | ||
|
||
2. **Intersection Calculation**: | ||
- The function `findIntersection` iterates through each element of `arr1` and checks if it exists in `arr2` using a nested `while` loop. | ||
- If an element in `arr1` matches any element in `arr2`, it is printed as part of the intersection. | ||
|
||
3. **Output**: | ||
- The program prints the elements found in both arrays, representing the intersection. | ||
|
||
### Example | ||
Consider the example arrays `{1, 2, 3, 4, 5}` and `{1, 2, 3}`. | ||
|
||
1. **Input**: | ||
- `arr1 = {1, 2, 3, 4, 5}` | ||
- `arr2 = {1, 2, 3}` | ||
|
||
2. **Output**: | ||
-Intersection of the two arrays: `{1, 2, 3}` | ||
|
||
## Complexity Analysis | ||
|
||
### Time Complexity | ||
- **Worst Case**: \(O(n1 \times n2)\), where `n1` is the length of `arr1` and `n2` is the length of `arr2`. | ||
- This is because each element in `arr1` is checked against every element in `arr2`, resulting in a nested loop. | ||
|
||
### Space Complexity | ||
- **Space Complexity**: \(O(1)\) (excluding the input arrays). | ||
- The program does not use any extra storage for results other than printing directly. | ||
|
||
### Assumptions | ||
- The program assumes the arrays contain only integer values. | ||
- It does not handle duplicates in the output; if duplicates are present in the input arrays, each duplicate will be checked independently. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <stdio.h> | ||
|
||
void findIntersection(int arr1[], int arr2[], int n1, int n2) { | ||
printf("Intersection of the two arrays: "); | ||
int i = 0; | ||
while (i < n1) { | ||
int j = 0; | ||
while (j < n2) { | ||
if (arr1[i] == arr2[j]) { | ||
printf("%d ", arr1[i]); | ||
break; // Move to the next element in arr1 after finding a match | ||
} | ||
j++; | ||
} | ||
i++; | ||
} | ||
printf("\n"); | ||
} | ||
|
||
int main() { | ||
int n1, n2; | ||
|
||
// Take the size of the first array from the user | ||
printf("Enter the number of elements in the first array: "); | ||
scanf("%d", &n1); | ||
int arr1[n1]; | ||
printf("Enter the elements of the first array:\n"); | ||
for (int i = 0; i < n1; i++) { | ||
scanf("%d", &arr1[i]); | ||
} | ||
|
||
// Take the size of the second array from the user | ||
printf("Enter the number of elements in the second array: "); | ||
scanf("%d", &n2); | ||
int arr2[n2]; | ||
printf("Enter the elements of the second array:\n"); | ||
for (int i = 0; i < n2; i++) { | ||
scanf("%d", &arr2[i]); | ||
} | ||
|
||
// Find and print the intersection of the two arrays | ||
findIntersection(arr1, arr2, n1, n2); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <stdio.h> | ||
|
||
#define MAX 100 | ||
|
||
// Function to print the matrix | ||
void printMatrix(int matrix[][MAX], int rows, int cols) { | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
printf("%d ", matrix[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
} | ||
|
||
// Function to set the rows and columns to zero where element is zero | ||
void setMatrixZero(int matrix[][MAX], int rows, int cols) { | ||
int rowFlag[MAX] = {0}; // Array to mark rows to be set to zero | ||
int colFlag[MAX] = {0}; // Array to mark columns to be set to zero | ||
|
||
// First pass: Identify rows and columns to be zeroed | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (matrix[i][j] == 0) { | ||
rowFlag[i] = 1; // Mark row i for zeroing | ||
colFlag[j] = 1; // Mark column j for zeroing | ||
} | ||
} | ||
} | ||
|
||
// Second pass: Set identified rows and columns to zero | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (rowFlag[i] == 1 || colFlag[j] == 1) { | ||
matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
int matrix[MAX][MAX]; | ||
int rows, cols; | ||
|
||
// Taking input for the dimensions of the matrix | ||
printf("Enter the number of rows and columns: "); | ||
scanf("%d %d", &rows, &cols); | ||
|
||
// Taking input for matrix elements | ||
printf("Enter the elements of the matrix:\n"); | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
scanf("%d", &matrix[i][j]); | ||
} | ||
} | ||
|
||
printf("Original Matrix:\n"); | ||
printMatrix(matrix, rows, cols); | ||
|
||
// Setting the required rows and columns to zero | ||
setMatrixZero(matrix, rows, cols); | ||
|
||
printf("Modified Matrix:\n"); | ||
printMatrix(matrix, rows, cols); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Set Matrix Zero | ||
|
||
## Problem Statement | ||
Given a matrix, if an element in the matrix is `0`, you must set its entire row and column to `0`. The task is to modify the matrix in place to achieve this result. | ||
|
||
### Example | ||
Input: | ||
matrix = [[1,1,1], [1,0,1], [1,1,1]] | ||
|
||
Output: | ||
[[1,0,1], [0,0,0], [1,0,1]] | ||
|
||
### Explanation | ||
Since `matrix[1][1]` is `0`, the entire row and column where this element is located are set to `0`. | ||
|
||
--- | ||
|
||
## Code Explanation | ||
|
||
The C program provided below takes the matrix dimensions and elements as input and sets rows and columns to zero according to the problem statement. | ||
|
||
### Code Flow | ||
1. **Input Matrix Dimensions and Elements**: The user enters the matrix dimensions (`rows` and `cols`) and the elements. | ||
2. **Identifying Rows and Columns for Zeroing**: | ||
- Two arrays, `rowFlag` and `colFlag`, are used to keep track of which rows and columns should be set to zero. | ||
- In a first pass through the matrix, if a `0` is found at any position `matrix[i][j]`, the program marks the respective row and column in `rowFlag` and `colFlag`. | ||
3. **Setting Rows and Columns to Zero**: | ||
- In a second pass through the matrix, if a row or column is marked in the `rowFlag` or `colFlag`, all elements in that row or column are set to zero. | ||
4. **Output Modified Matrix**: The program prints the modified matrix with the correct rows and columns set to zero. | ||
|
||
### Example Usage | ||
|
||
#### Input | ||
Enter the number of rows and columns: 3 3 Enter the elements of the matrix: 1 1 1 1 0 1 1 1 1 | ||
|
||
#### Output | ||
Original Matrix: 1 1 1 1 0 1 1 1 1 | ||
|
||
Modified Matrix: 1 0 1 0 0 0 1 0 1 | ||
|
||
|
||
--- | ||
|
||
## Complexity Analysis | ||
- **Time Complexity**: `O(rows * cols)` for both passes over the matrix. | ||
- **Space Complexity**: `O(rows + cols)` for the additional `rowFlag` and `colFlag` arrays used to mark rows and columns to be zeroed. | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
### SUM TREE | ||
--- | ||
### Problem Description: | ||
|
||
Given a binary tree, the task is to check if it is a Sum Tree. A Sum Tree is a Binary Tree where the value of a node is equal to the sum of the nodes present in its left subtree and right subtree. An empty tree is Sum Tree and the sum of an empty tree can be considered as 0. A leaf node is also considered a Sum Tree. | ||
|
||
--- | ||
### Appraoch | ||
The approach to checking if a binary tree is a Sum Tree involves recursively traversing the tree, where each node checks if it is `NULL` or a leaf. The algorithm computes the sums of the left and right subtrees, then verifies if the current node's value equals the combined sum of its children. If all nodes satisfy this condition, the tree is classified as a Sum Tree; otherwise, it is not. | ||
|
||
### Algorithm Steps: | ||
|
||
1. Pass the root node of the binary tree to the Sum Tree check function. | ||
2. If the node is `NULL`, return `true` with a sum of 0; if it's a leaf, return `true` with its value. | ||
3. Recursively call the function for the left child and right child to get their sums and check for Sum Tree property. | ||
4. Add the values of the left and right subtree sums to the current node’s value. | ||
5. Return `true` if the current node's value equals the sum of its left and right subtree sums; otherwise, return `false`. | ||
6. If the root’s check passes, return `true` ; if any node fails, return `false`. | ||
|
||
|
||
--- | ||
### Sample Input: | ||
// 26 | ||
// / \ | ||
// 10 3 | ||
// / \ \ | ||
// 4 6 3 | ||
|
||
### Sample Output: | ||
The binary tree is a Sum Tree. | ||
|
||
--- | ||
### Time Complexity: | ||
- The time complexity of checking if a binary tree is a Sum Tree is `O(n)`, where `n` is the number of nodes in the tree. This is because the algorithm visits each node exactly once to compute sums and check the Sum Tree property, resulting in linear traversal. | ||
|
||
### Space Complexity: | ||
- The space complexity of the algorithm for checking if a binary tree is a Sum Tree is `O(h)`, where `h` is the height of the tree, due to the recursive call stack. In the worst case, this can be `O(n)` for a skewed tree, while for a balanced tree, it is `O(log n)`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct TreeNode { | ||
int value; | ||
struct TreeNode* left; | ||
struct TreeNode* right; | ||
}; | ||
|
||
struct TreeNode* createNode(int val) { | ||
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); | ||
newNode->value = val; | ||
newNode->left = NULL; | ||
newNode->right = NULL; | ||
return newNode; | ||
} | ||
|
||
int isSumTree(struct TreeNode* root, int* sum) { | ||
// An empty tree is a Sum Tree | ||
if (root == NULL) { | ||
*sum = 0; | ||
return 1; // true | ||
} | ||
|
||
// Leaf nodes are Sum Trees | ||
if (root->left == NULL && root->right == NULL) { | ||
*sum = root->value; | ||
return 1; // true | ||
} | ||
|
||
int leftSum = 0, rightSum = 0; | ||
|
||
// Recursively check the left and right subtrees | ||
int leftIsSumTree = isSumTree(root->left, &leftSum); | ||
int rightIsSumTree = isSumTree(root->right, &rightSum); | ||
|
||
// The current node's value should equal the sum of left and right subtrees | ||
*sum = leftSum + rightSum + root->value; | ||
|
||
return leftIsSumTree && rightIsSumTree && (root->value == leftSum + rightSum); | ||
} | ||
|
||
// Helper function to initiate the check | ||
int isSumTreeWrapper(struct TreeNode* root) { | ||
int sum = 0; | ||
return isSumTree(root, &sum); | ||
} | ||
|
||
int main() { | ||
struct TreeNode* root = createNode(26); | ||
root->left = createNode(10); | ||
root->right = createNode(3); | ||
root->left->left = createNode(4); | ||
root->left->right = createNode(6); | ||
root->right->right = createNode(3); | ||
|
||
if (isSumTreeWrapper(root)) { | ||
printf("The binary tree is a Sum Tree.\n"); | ||
} else { | ||
printf("The binary tree is NOT a Sum Tree.\n"); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Count the Number of Set Bits | ||
|
||
## Description | ||
This algorithm counts the number of 1 bits (set bits) in the binary representation of a given integer. Counting set bits is useful in various fields like cryptography, networking, and data compression. | ||
|
||
## Example Usage | ||
Enter an integer: 29 | ||
Number of set bits in 29 is 4 | ||
|
||
## Complexity | ||
** Time Complexity **: | ||
O(number of set bits) — The loop runs once for each set bit. | ||
|
||
** Space Complexity **: | ||
O(1) — Constant space usage. | ||
|
||
## Applications | ||
- Cryptography: For analyzing binary data. | ||
- Networking: Useful for IP address and network mask operations. | ||
- Data Compression: Helps in efficient data encoding and decoding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <stdio.h> | ||
|
||
int countSetBits(int n) { | ||
int count = 0; | ||
while (n > 0) { | ||
n = n & (n - 1); // Removes the rightmost set bit | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
int main() { | ||
int num; | ||
printf("Enter an integer: "); | ||
scanf("%d", &num); | ||
|
||
int result = countSetBits(num); | ||
printf("Number of set bits in %d is %d\n", num, result); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.