-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1249 from Shreya7tripathy/add-arrays
Add DSA Array Questions (Easy, Medium, Hard)
- Loading branch information
Showing
6 changed files
with
644 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,98 @@ | ||
--- | ||
id: find-missing-number | ||
title: Find the Missing Number in Array | ||
sidebar_label: Find Missing Number in Array | ||
sidebar_position: 1 | ||
description: Find the missing number in an array of size N containing numbers from 1 to N. | ||
tags: [Array, XOR, DSA] | ||
--- | ||
|
||
# Problem Statement | ||
You are given an array `a` of size `N-1` containing numbers from `1` to `N`. The array has one number missing. Find the missing number. | ||
|
||
[LeetCode Problem Link](https://leetcode.com/problems/missing-number/description/) | ||
|
||
--- | ||
|
||
## Examples | ||
|
||
**Example 1**: | ||
Input: | ||
`a = [1, 2, 4, 5]`, `N = 5` | ||
|
||
Output: | ||
`3` | ||
|
||
Explanation: | ||
Numbers between `1` to `N` are `[1, 2, 3, 4, 5]`. The missing number is `3`. | ||
|
||
--- | ||
|
||
**Example 2**: | ||
Input: | ||
`a = [2, 3, 4, 5]`, `N = 5` | ||
|
||
Output: | ||
`1` | ||
|
||
Explanation: | ||
Numbers between `1` to `N` are `[1, 2, 3, 4, 5]`. The missing number is `1`. | ||
|
||
--- | ||
|
||
## Intuition | ||
This problem can be solved using the properties of XOR: | ||
1. **Property 1**: XOR of two identical numbers is always 0 (`a ^ a = 0`). | ||
2. **Property 2**: XOR of a number with 0 results in the number itself (`0 ^ a = a`). | ||
|
||
### Key Idea: | ||
- XOR all the numbers from `1` to `N`, which results in `xor1 = 1 ^ 2 ^ ... ^ N`. | ||
- XOR all the elements in the array `a[]`, which results in `xor2 = 1 ^ 2 ^ ... ^ N` (excluding the missing number). | ||
- The result of `xor1 ^ xor2` will cancel out all the numbers except the missing one, as explained by the XOR properties. Hence, `missing number = xor1 ^ xor2`. | ||
|
||
--- | ||
|
||
## Approach | ||
1. Initialize two variables `xor1` and `xor2` to 0. | ||
2. XOR all the numbers from `1` to `N` and store the result in `xor1`. | ||
3. XOR all the elements of the array and store the result in `xor2`. | ||
4. XOR the values of `xor1` and `xor2`. The result will be the missing number. | ||
|
||
--- | ||
|
||
## Java Implementation | ||
|
||
```java | ||
import java.util.*; | ||
|
||
public class FindMissingNumber { | ||
public static int missingNumber(int []a, int N) { | ||
int xor1 = 0, xor2 = 0; | ||
|
||
for (int i = 0; i < N - 1; i++) { | ||
xor2 = xor2 ^ a[i]; // XOR of array elements | ||
xor1 = xor1 ^ (i + 1); // XOR up to [1...N-1] | ||
} | ||
xor1 = xor1 ^ N; // XOR up to [1...N] | ||
|
||
return (xor1 ^ xor2); // the missing number | ||
} | ||
|
||
public static void main(String args[]) { | ||
int N = 5; | ||
int a[] = {1, 2, 4, 5}; | ||
|
||
int ans = missingNumber(a, N); | ||
System.out.println("The missing number is: " + ans); | ||
} | ||
} | ||
``` | ||
--- | ||
## Time Complexity | ||
**Time Complexity**: `O(N)`, where N is the number of elements in the array. We only need to iterate over the array once to calculate the XORs. | ||
|
||
**Space Complexity**: `O(1)`, as we are only using a constant amount of extra space. | ||
|
||
--- | ||
## Conclusion | ||
Using XOR properties, we can find the missing number in linear time without using any extra space, making it an optimal solution for this problem. |
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,96 @@ | ||
--- | ||
id: remove-duplicates-sorted-array | ||
title: Remove Duplicates in-place from Sorted Array | ||
sidebar_label: Remove Duplicates in-place | ||
sidebar_position: 1 | ||
description: Given a sorted array, remove the duplicates in-place and return the new length. | ||
tags: [Array, In-place, DSA] | ||
--- | ||
|
||
# Remove Duplicates in-place from Sorted Array | ||
|
||
## Problem Statement | ||
Given a sorted array `arr`, remove duplicates in-place such that each element appears only once and return the new length. The solution should modify the input array in-place and not use extra space. | ||
|
||
[LeetCode Problem Link](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/) | ||
|
||
--- | ||
|
||
## Examples | ||
|
||
**Example 1**: | ||
Input: | ||
`arr = [1, 1, 2, 2, 2, 3, 3]` | ||
|
||
Output: | ||
`arr = [1, 2, 3, _, _, _, _]` | ||
|
||
Explanation: | ||
The unique elements are `[1, 2, 3]`, so the function returns `3` and places `[1, 2, 3]` at the start of the array. | ||
|
||
--- | ||
|
||
**Example 2**: | ||
Input: | ||
`arr = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4]` | ||
|
||
Output: | ||
`arr = [1, 2, 3, 4, _, _, _, _, _, _, _]` | ||
|
||
Explanation: | ||
The unique elements are `[1, 2, 3, 4]`, so the function returns `4` and places `[1, 2, 3, 4]` at the start of the array. | ||
|
||
--- | ||
|
||
## Approach | ||
The problem can be solved using the two-pointer technique: | ||
|
||
1. Maintain two pointers, `i` and `j`, where `i` tracks the position of unique elements and `j` iterates over the array. | ||
2. If the element at index `j` is different from the element at index `i`, increment `i` and set `arr[i] = arr[j]`. | ||
3. The new length of the array will be `i + 1`. | ||
|
||
### Steps: | ||
- Initialize `i = 0` to track the first unique element. | ||
- Traverse the array from `j = 1` to the end. | ||
- If `arr[j]` is different from `arr[i]`, update `arr[i + 1]` with `arr[j]` and increment `i`. | ||
- After the loop, the first `i + 1` elements of the array will be unique. | ||
|
||
--- | ||
|
||
## Java Implementation | ||
|
||
```java | ||
import java.util.*; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
int arr[] = {1, 1, 2, 2, 2, 3, 3}; | ||
int k = removeDuplicates(arr); | ||
System.out.println("The array after removing duplicate elements is "); | ||
for (int i = 0; i < k; i++) { | ||
System.out.print(arr[i] + " "); | ||
} | ||
} | ||
|
||
static int removeDuplicates(int[] arr) { | ||
int i = 0; | ||
for (int j = 1; j < arr.length; j++) { | ||
if (arr[i] != arr[j]) { | ||
i++; | ||
arr[i] = arr[j]; | ||
} | ||
} | ||
return i + 1; | ||
} | ||
} | ||
``` | ||
|
||
--- | ||
## Time Complexity | ||
**Time Complexity**: `O(n)`, where n is the length of the input array. We are using a single loop to traverse the array. | ||
|
||
**Space Complexity**: `O(1)`, since we are modifying the array in-place without using any extra space. | ||
|
||
--- | ||
## Conclusion | ||
This solution efficiently removes duplicates from a sorted array in-place using the two-pointer technique. It has a time complexity of `O(n)` and does not require extra space, making it optimal for large inputs. |
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,104 @@ | ||
--- | ||
id: kadanes-algorithm> | ||
title: Kadane's Algorithm- Maximum Subarray Sum | ||
sidebar_label: Maximum Subarray Sum | ||
sidebar_position: 1 | ||
description: Find the maximum sum of a contiguous subarray in an integer array. | ||
tags: [Array, Dynamic Programming, Greedy, DSA] | ||
--- | ||
|
||
# Kadane's Algorithm: Maximum Subarray Sum | ||
|
||
## Problem Statement | ||
Given an integer array `arr`, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. | ||
|
||
[LeetCode Problem Link](https://leetcode.com/problems/maximum-subarray/description/) | ||
|
||
## Examples | ||
|
||
**Example 1**: | ||
Input: | ||
`arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]` | ||
|
||
Output: | ||
`6` | ||
|
||
Explanation: | ||
The subarray `[4, -1, 2, 1]` has the largest sum = 6. | ||
|
||
--- | ||
|
||
**Example 2**: | ||
Input: | ||
`arr = [1]` | ||
|
||
Output: | ||
`1` | ||
|
||
Explanation: | ||
Array has only one element, which gives a positive sum of 1. | ||
|
||
--- | ||
|
||
## Intuition | ||
The intuition behind **Kadane's Algorithm** is simple: a subarray with a negative sum will always reduce the sum of the total subarray, so it's better to discard such subarrays and reset the sum to 0 whenever the sum goes below 0. | ||
|
||
### Key Idea | ||
- Iterate through the array and add elements to a running sum. | ||
- If the sum becomes negative at any point, reset it to zero since no subarray with a negative sum is worth considering. | ||
- Track the maximum sum encountered during the iteration. | ||
|
||
## Approach | ||
1. Initialize two variables: `maxi` to store the maximum sum and `sum` to store the current subarray sum. | ||
2. Iterate through the array: | ||
- Add the current element to `sum`. | ||
- If `sum` exceeds `maxi`, update `maxi`. | ||
- If `sum` becomes negative, reset it to `0`. | ||
3. Return `maxi` as the result. | ||
|
||
### Edge Case | ||
In some scenarios, the question may mention that the sum of an empty subarray should be considered. In that case, compare `maxi` with `0` before returning the result, and ensure you return the larger value. | ||
|
||
## Java Implementation | ||
|
||
```java | ||
import java.util.*; | ||
|
||
public class Main { | ||
public static long maxSubarraySum(int[] arr, int n) { | ||
long maxi = Long.MIN_VALUE; // maximum sum | ||
long sum = 0; | ||
|
||
for (int i = 0; i < n; i++) { | ||
sum += arr[i]; | ||
if (sum > maxi) { | ||
maxi = sum; | ||
} | ||
// If sum < 0: discard the sum calculated | ||
if (sum < 0) { | ||
sum = 0; | ||
} | ||
} | ||
|
||
return maxi; | ||
} | ||
|
||
public static void main(String args[]) { | ||
int[] arr = { -2, 1, -3, 4, -1, 2, 1, -5, 4}; | ||
int n = arr.length; | ||
long maxSum = maxSubarraySum(arr, n); | ||
System.out.println("The maximum subarray sum is: " + maxSum); | ||
} | ||
} | ||
|
||
``` | ||
|
||
--- | ||
## Time Complexity | ||
The **Time complexity** of Kadane's Algorithm is `O(n)`, where n is the number of elements in the array. This is because we make a single pass through the array to calculate the maximum subarray sum. | ||
|
||
**Space Complexity**: `O(1)` as we are not using any extra space. | ||
|
||
--- | ||
## Conclusion | ||
Kadane's Algorithm efficiently finds the maximum subarray sum in linear time, making it a powerful technique for solving problems related to contiguous subarrays. The algorithm's simplicity and effectiveness make it a staple in competitive programming and interviews. |
Oops, something went wrong.