diff --git a/Find the missing integer/saishwari.c b/Find the missing integer/saishwari.c new file mode 100644 index 0000000..969dd6f --- /dev/null +++ b/Find the missing integer/saishwari.c @@ -0,0 +1,28 @@ +#include + +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; +} diff --git a/Subarrays with equal 1s and 0s/saishwari.java b/Subarrays with equal 1s and 0s/saishwari.java new file mode 100644 index 0000000..cedebc2 --- /dev/null +++ b/Subarrays with equal 1s and 0s/saishwari.java @@ -0,0 +1,31 @@ +public class SubarraysWithEqualOnesAndZeros { + public static int countSubarrays(int[] nums) { + int count = 0; + int sum = 0; + HashMap 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); + } +}