Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Ananya-vastare/C
Browse files Browse the repository at this point in the history
  • Loading branch information
Ananya-vastare committed Oct 27, 2024
2 parents 288cea0 + 959dc43 commit 6a10260
Show file tree
Hide file tree
Showing 354 changed files with 42,679 additions and 361 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/add-contributor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Update Contributors in README

on:
push:
branches: ["master"]
workflow_dispatch:

jobs:
contrib-readme-job:
runs-on: ubuntu-latest
name: A job to automate contrib in readme
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Update Contributors List
uses: akhilmhdh/[email protected]
with:
commit_message: "Updated contributors list"
committer_username: "yashksaini"
committer_email: "[email protected]"
readme_path: "static-site/src/pages/Contributors.md"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53 changes: 53 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Deploy to GitHub Pages

on:
push:
branches:
- main
- static-site
# Review gh actions docs if you want to further define triggers, paths, etc
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on
workflow_dispatch:

jobs:
build:
name: Build Docusaurus
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 18
cache: npm

- name: Install dependencies
run: cd static-site && npm install
- name: Build website
run: cd static-site && npm run build

- name: Upload Build Artifact
uses: actions/upload-pages-artifact@v3
with:
path: static-site/build

deploy:
name: Deploy to GitHub Pages
needs: build

# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source

# Deploy to the github-pages environment
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
26 changes: 26 additions & 0 deletions .github/workflows/test-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test deployment

on:
pull_request:
branches:
- main
# Review gh actions docs if you want to further define triggers, paths, etc
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on

jobs:
test-deploy:
name: Test deployment
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 18
cache: npm

- name: Install dependencies
run: cd static-site && npm install
- name: Test build website
run: cd static-site && npm run build
43 changes: 43 additions & 0 deletions 1D Arrays/Maximum Circular Sum Subarray/Program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// C program to find maximum Subarray Sum in Circular
// subarray by considering all possible subarrays

#include <stdio.h>

int circularSubarraySum(int arr[], int n) {
int totalSum = 0;
int currMaxSum = 0, currMinSum = 0;
int maxSum = arr[0], minSum = arr[0];

for(int i = 0; i < n; i++) {

// Kadane's to find maximum sum subarray
currMaxSum = (currMaxSum + arr[i] > arr[i]) ?
currMaxSum + arr[i] : arr[i];
maxSum = (maxSum > currMaxSum) ? maxSum : currMaxSum;

// Kadane's to find minimum sum subarray
currMinSum = (currMinSum + arr[i] < arr[i]) ?
currMinSum + arr[i] : arr[i];
minSum = (minSum < currMinSum) ? minSum : currMinSum;

// Sum of all the elements of input array
totalSum += arr[i];
}

int normalSum = maxSum;
int circularSum = totalSum - minSum;

// If the minimum subarray is equal to total Sum
// then we just need to return normalSum
if(minSum == totalSum)
return normalSum;

return (normalSum > circularSum) ? normalSum : circularSum;
}

int main() {
int arr[] = {8, -8, 9, -9, 10, -11, 12};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", circularSubarraySum(arr, n));
return 0;
}
26 changes: 26 additions & 0 deletions 1D Arrays/Maximum Circular Sum Subarray/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Maximum Circular Sum Subarray

## Problem Statement

Given an array of integers, find the maximum sum of a subarray with the constraint that the subarray may be circular. In other words, the subarray can wrap around to the beginning or end of the array.

## Algorithm Approach

To solve this problem, we can use the Kadane's algorithm, which is a well-known algorithm for finding the maximum subarray sum in a linear time complexity.

1. Initialize two variables, `max_sum` and `min_sum`, to the first element of the array.
2. Initialize two more variables, `current_max` and `current_min`, to the first element of the array.
3. Iterate through the array starting from the second element.
4. Update `current_max` and `current_min` as follows:
- `current_max = max(current_max + element, element)`
- `current_min = min(current_min + element, element)`
5. Update `max_sum` and `min_sum` as follows:
- `max_sum = max(max_sum, current_max)`
- `min_sum = min(min_sum, current_min)`
6. If `max_sum` is negative, it means that all elements in the array are negative. In this case, return `max_sum` as the maximum circular sum.
7. Otherwise, return the maximum of `max_sum` and the sum of all elements in the array minus `min_sum`.

![alt text](image.png)
## Time Complexity

The time complexity of this algorithm is O(n), where n is the size of the input array. This is because we iterate through the array only once.
Binary file added 1D Arrays/Maximum Circular Sum Subarray/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions 1D Arrays/MooreVotingAlgorithm/Program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Function to find the candidate for the majority element
int findCandidate(int* arr, int n) {
int count = 1, candidate = arr[0];

for (int i = 1; i < n; i++) {
if (arr[i] == candidate)
count++;
else
count--;

if (count == 0) {
candidate = arr[i];
count = 1;
}
}

return candidate;
}

// Function to check if the candidate is actually the majority element
bool isMajority(int* arr, int n, int candidate) {
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == candidate)
count++;
}
return count > n / 2;
}

// Function to print the majority element, if it exists
void printMajority(int* arr, int n) {
int candidate = findCandidate(arr, n);

if (isMajority(arr, n, candidate))
printf("The majority element is %d\n", candidate);
else
printf("No majority element exists\n");
}

// Main function
int main() {
int n;

// Input the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);

// Dynamically allocate memory for the array
int* arr = (int*)malloc(n * sizeof(int));

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

// Call the function to print the majority element
printMajority(arr, n);

// Free the dynamically allocated memory
free(arr);

return 0;
}
80 changes: 80 additions & 0 deletions 1D Arrays/MooreVotingAlgorithm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Moore's Algorithm

## Description

This program implements Moore's algorithm to find the majority element in an array. The majority element is defined as an element that appears more than n/2 times in an array of size n. Moore's algorithm is an efficient method that solves this problem in linear time and constant space.

## Algorithm Overview

Moore's algorithm works on the principle of pair cancellation. It iterates through the array once to find a candidate for the majority element and then verifies if this candidate is indeed the majority element in a second pass.

## Structures

1. No specific structures are required for this algorithm.

## Functions

1. `int findCandidate(int arr[], int size)`
- Description: Finds a candidate for the majority element.
- Parameters:
- `arr[]`: The input array
- `size`: The size of the array
- Returns: The candidate element

2. `bool isMajority(int arr[], int size, int candidate)`
- Description: Verifies if the candidate is the majority element.
- Parameters:
- `arr[]`: The input array
- `size`: The size of the array
- `candidate`: The candidate element to verify
- Returns: `true` if the candidate is the majority element, `false` otherwise

## Main Function

- Description: The entry point of the program.
- Details:
* Prompts the user to enter the size of the array.
* Dynamically allocates memory for the array based on the size.
* Prompts the user to enter the elements of the array.
* Calls `findCandidate()` to get a potential majority element.
* Calls `isMajority()` to verify if the candidate is indeed the majority element.
* Prints the majority element if found, or a message if no majority element exists.
* Frees the dynamically allocated memory for the array before exiting.

## Memory Management

- Dynamic Memory Allocation: Used for creating the input array.
- Freeing Memory: The allocated memory is freed after use to prevent memory leaks.

## Time Complexity

- O(n), where n is the number of elements in the array.

## Space Complexity

- O(1), as it uses only a constant amount of extra space.

## Usage

1. Compile the program using a C compiler (e.g., gcc):
```
gcc Program.c -o moores_algorithm
```
2. Run the compiled program:
```
./Program
```
3. Follow the prompts to input the array size and elements.

## Example

```
Enter the size of the array: 7
Enter 7 elements:
2 2 1 1 1 2 2
The majority element is: 2
```

## Note

This implementation assumes that a majority element always exists in the input array. If you want to handle cases where a majority element might not exist, you can modify the `isMajority` function to return `false` if the candidate doesn't appear more than n/2 times.
Loading

0 comments on commit 6a10260

Please sign in to comment.