Skip to content

Commit

Permalink
Merge branch 'AlgoGenesis:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Riya7045 authored Oct 29, 2024
2 parents 14f99a1 + 4477ddb commit b08359e
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>

void diagonalBoundarySpiral(int arr[][100], int rows, int cols) {
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;

while (top <= bottom && left <= right) {
// Top-left to bottom-right diagonal
for (int i = top, j = left; i <= bottom && j <= right; i++, j++) {
printf("%d ", arr[i][j]);
}
top++; // Move top boundary down

// Top-right to bottom-left diagonal
for (int i = top, j = right; i <= bottom && j >= left; i++, j--) {
printf("%d ", arr[i][j]);
}
right--; // Move right boundary left

if (top <= bottom) {
// Bottom-right to top-left diagonal
for (int i = bottom, j = right; i >= top && j >= left; i--, j--) {
printf("%d ", arr[i][j]);
}
bottom--; // Move bottom boundary up
}

if (left <= right) {
// Bottom-left to top-right diagonal
for (int i = bottom, j = left; i >= top && j <= right; i--, j++) {
printf("%d ", arr[i][j]);
}
left++; // Move left boundary right
}
}
}

int main() {
int arr[100][100], rows, cols;

// Input dimensions of the 2D array
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);

// Input elements of the 2D array
printf("Enter the elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}

// Perform diagonal boundary spiral traversal
printf("Diagonal Boundary Spiral Traversal: ");
diagonalBoundarySpiral(arr, rows, cols);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Diagonal Boundary Spiral Traversal of a 2D Array in C

This C program performs a diagonal boundary spiral traversal of a 2D array. It moves along the matrix's outer boundaries in a diagonal pattern until all elements are traversed.

# Description
The program accepts a 2D array of fixed maximum size, though its effective size (rows and columns) can be specified by the user. The diagonal boundary spiral traversal involves moving along the matrix's outermost boundaries diagonally, starting from the top-left corner and spiraling inward.

# Features
Traverses a 2D array in a spiral diagonal pattern.
Moves along four primary diagonals (top-left to bottom-right, top-right to bottom-left, bottom-right to top-left, and bottom-left to top-right) and narrows the boundaries after each traversal.
Outputs all elements in the specified traversal order.
Usage
Clone the repository or download the source code.
Open the code in your preferred C compiler or IDE.
Enter the desired row and column dimensions, followed by the elements of the array.
Compile and run the program.
# Example
For an array:

| 1 2 3 |
| 4 5 6 |
| 7 8 9 |

The output will be the elements in a diagonal boundary spiral order: 1 5 9 6 3 2 4 8 7

# Time and Space Complexity
Time Complexity
The time complexity of this diagonal boundary spiral traversal algorithm is:
O(m × n), where m is the number of rows and n is the number of columns in the array. This complexity arises from the need to visit each element once.
Space Complexity
The space complexity of the algorithm is:
O(1) if we are printing directly, as it only requires a few pointers for traversal and boundaries.
O(m × n) if storing the traversal output in an array, as additional storage is needed for output elements.
# Requirements
A C compiler (e.g., GCC)
Basic knowledge of C programming





Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>

// Function to calculate GCD of two numbers
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

// Function to calculate GCD of an array
int gcdArray(int arr[], int n) {
int result = arr[0];
for (int i = 1; i < n; i++) {
result = gcd(result, arr[i]);
if (result == 1) { // Early exit if GCD becomes 1
break;
}
}
return result;
}

// Function to find the minimum deletions required
int minimumDeletionsToDivisible(int arr[], int n) {
int divisor = gcdArray(arr, n);
int deletions = 0;

// Count elements not divisible by the GCD of the array
for (int i = 0; i < n; i++) {
if (arr[i] % divisor != 0) {
deletions++;
}
}

return deletions;
}

int main() {
int n;

// Read the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);

int arr[n];

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

// Find and print the minimum deletions required
int result = minimumDeletionsToDivisible(arr, n);
printf("Minimum deletions required: %d\n", result);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Minimum Deletions to Make Array Divisible by Array's GCD

## Description

The **Minimum Deletions to Make Array Divisible by Array's GCD** algorithm determines the minimum number of deletions required to make all elements in an array divisible by the array’s greatest common divisor (GCD). This ensures uniformity in the divisibility of array elements, which is useful in contexts like data processing, modular arithmetic, and mathematical computations requiring standardized factors.

## Key Features
- **GCD Calculation**: Finds the GCD of the array to identify the required divisor for all elements.
- **Divisibility Check**: Counts elements not divisible by the GCD, as these must be removed to achieve the goal.
- **Minimum Deletions Calculation**: Outputs the minimum deletions needed for the array to be fully divisible by its GCD.

## Problem Definition

Given an integer array `arr`, the goal is to:
1. Calculate the GCD of all elements in the array.
2. Count the number of elements that are not divisible by this GCD.
3. Return this count as the minimum deletions required for full divisibility by the array's GCD.

- **Input**: An integer array, `arr`.
- **Output**: The minimum number of deletions required for all elements in the array to be divisible by the array's GCD.

## Algorithm Review

### 1. Calculate the GCD of the Array
- Traverse the array and calculate the GCD of all elements using the GCD of each successive pair.

### 2. Check Divisibility of Elements
- For each element in the array, check if it’s divisible by the calculated GCD.
- Count elements that are not divisible, as these need to be deleted to meet the requirement.

### 3. Return Result
- Return the count of non-divisible elements as the minimum deletions needed.

## Time Complexity
- **GCD Calculation**: \(O(n \cdot \log(\text{min}(a, b)))\), where \(n\) is the number of elements and \(a\) and \(b\) are elements in the array.
- **Divisibility Check**: \(O(n)\), since each element is checked once.

Overall, the algorithm has a time complexity of \(O(n \cdot \log(\text{min}(a, b)))\).

## Applications

This algorithm is useful in scenarios requiring uniform divisibility in data elements:
- **Data Validation**: Ensures uniform factors in datasets, commonly used in data pipelines.
- **Mathematical Computations**: Simplifies modular arithmetic and operations by ensuring elements align with the array’s GCD.
- **Array Processing**: Provides an efficient method for preparing arrays in applications requiring homogeneous divisibility.

## Conclusion

The **Minimum Deletions to Make Array Divisible by Array's GCD** algorithm offers an efficient way to enforce uniformity in divisibility within an array. This algorithm finds applications in fields like data processing, modular arithmetic, and number theory, where uniform divisibility is beneficial for simplifying computations and ensuring data consistency.

0 comments on commit b08359e

Please sign in to comment.