Skip to content

Create saishwari.c #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Find the missing integer/saishwari.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>

int findMissingInteger(int arr[], int n) {
// Calculate the expected sum of integers from 1 to n+1
int expectedSum = (n + 1) * (n + 2) / 2;

// Calculate the actual sum of elements in the array
int actualSum = 0;
for (int i = 0; i < n; i++) {
actualSum += arr[i];
}

// The missing integer is the difference between the expected and actual sums
int missingInteger = expectedSum - actualSum;

return missingInteger;
}

int main() {
int arr[] = {1, 2, 4, 6, 3, 7, 8};
int n = sizeof(arr) / sizeof(arr[0]);

int missingInteger = findMissingInteger(arr, n);

printf("The missing integer is: %d\n", missingInteger);

return 0;
}
31 changes: 31 additions & 0 deletions Subarrays with equal 1s and 0s/saishwari.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class SubarraysWithEqualOnesAndZeros {
public static int countSubarrays(int[] nums) {
int count = 0;
int sum = 0;
HashMap<Integer, Integer> map = new HashMap<>();

// Initialize the sum to 0 with count 1 (for the empty subarray)
map.put(0, 1);

for (int num : nums) {
// Calculate the running sum
sum += num;

// If the sum - k has been seen before, add its count to the result
if (map.containsKey(sum - 0)) {
count += map.get(sum - 0);
}

// Update the count for the current sum
map.put(sum, map.getOrDefault(sum, 0) + 1);
}

return count;
}

public static void main(String[] args) {
int[] nums = {0, 1, 0, 1, 0};
int result = countSubarrays(nums);
System.out.println("Count of subarrays with equal 1s and 0s: " + result);
}
}