Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 57 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,69 @@ These questions test your ability to apply various techniques such as hashing, s

## Foundation Questions ❤️
| Array Questions | Practice Link | Code Solutions | Video Solutions | HINT
| ------------- | :-------------: | :-------------: | :---------: |:---------: |
Wave Array
Subarray with given sum
Missing number in array
Rotate Array | [Solve](https://leetcode.com/problems/rotate-array/) | [CODE](#) | [Video](https://youtu.be/0OTPqrEd74g)
Find duplicates in an array
Remove all occurrences of Given Number | [Solve](https://leetcode.com/problems/remove-element/)
Remove Duplicates from Sorted Array | [Solve](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)
Move all zeroes to end of array
Sort an array of 0s, 1s and 2s | [Solve](https://leetcode.com/problems/sort-colors/submissions/) , [Solve](https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1/) | [Code](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions/blob/main/Sort%20Array%200-1-2.md) | [Video](#)
Intersection of two arrays
Reverse an array
Swapping Elements in an array
Find Min and Max in an Array
-|-|-|-|-
Wave Array| [Solve](https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1?utm_source=geeksforgeeks) |
Subarray with given sum| [Solve](https://Solve.com/problems/subarray-sum-equals-k/) |
Missing number in array| [Solve](https://Solve.com/problems/missing-number/) |
Rotate Array | [Solve](https://Solve.com/problems/rotate-array/) | | [Video](https://youtu.be/0OTPqrEd74g)
Find duplicates in an array| [Solve](https://Solve.com/problems/find-all-duplicates-in-an-array/) |
Remove all occurrences of Given Number | [Solve](https://Solve.com/problems/remove-element/)
Remove Duplicates from Sorted Array | [Solve](https://Solve.com/problems/remove-duplicates-from-sorted-array/)
Move all zeroes to end of array| [Solve](https://Solve.com/problems/move-zeroes/)
Sort an array of 0s, 1s and 2s | [Solve](https://Solve.com/problems/sort-colors/submissions/) |
Intersection of two arrays| [Solve](https://Solve.com/problems/intersection-of-two-arrays/)
Reverse an array| [Solve](https://Solve.com/problems/reverse-string/)
Swapping Elements in an array|
Remove Min and Max in an Array| [Solve](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/)

## Medium Questions ❤️‍🔥
| Array Questions | Practice Link | Code Solutions | Video Solutions | HINT
| ------------- | :-------------: | :-------------: | :---------: |:---------: |
Valid Moutain Array | [Solve](https://leetcode.com/problems/valid-mountain-array/) | [Code](#) | [Video](https://youtu.be/tVDTjm_fYbQ) | 2 pointers
Majority Element | [Solve](https://leetcode.com/problems/majority-element/) | [Code](#) | [Video](https://www.youtube.com/watch?v=cLE1J34pYUo) | Sorting / or Boyer-Moore Voting Algorithm
Kth largest/smallest element in an array | [Solve](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Code](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions/blob/main/kth-largest-smallest.md) | [Video](#) | soritng / priority q
Trapping Rain Water
Coin Change
Stock span problem
Merge k Sorted Arrays
Next Permutation | [Solve](https://leetcode.com/problems/next-permutation) | [Code](#) | [Video](#) | 2 poniters
Kadane's Algorithm
Valid Moutain Array | [Solve](https://Solve.com/problems/valid-mountain-array/) | [Code](#) | [Video](https://youtu.be/tVDTjm_fYbQ) | 2 pointers
Majority Element | [Solve](https://Solve.com/problems/majority-element/) | [Code](#) | [Video](https://www.youtube.com/watch?v=cLE1J34pYUo) | Sorting / or Boyer-Moore Voting Algorithm
Kth largest/smallest element in an array | [Solve](https://Solve.com/problems/kth-largest-element-in-an-array/) | [Code](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions/blob/main/kth-largest-smallest.md) | [Video](#) | soritng / priority q
Trapping Rain Water|[Solve](https://Solve.com/problems/trapping-rain-water/)
Coin Change|[Solve](https://Solve.com/problems/coin-change/)
Stock span problem|[Solve](https://Solve.com/problems/online-stock-span/)
Merge k Sorted Arrays|[Solve](https://Solve.com/problems/merge-k-sorted-lists/)
Next Permutation |[Solve](https://Solve.com/problems/next-permutation) | [Code](#) | [Video](#) | 2 poniters
Kadane's Algorithm|[Solve](https://Solve.com/problems/maximum-subarray)



## Hard Questions 💔
| Array Questions | Practice Link | Code Solutions | Video Solutions | HINT
| ------------- | :-------------: | :-------------: | :---------: |:---------: |
Merge Without Extra Space
Next Smallest Palindrome
-|-|-|-|-
Merge Without Extra Space|[GeeksForGeeks](https://practice.geeksforgeeks.org/problems/merge-two-sorted-arrays-1587115620/1)
Next Smallest Palindrome|[GeeksForGeeks](https://practice.geeksforgeeks.org/problems/next-smallest-palindrome4740/1?utm_source=geeksforgeeks)


---

<div align="center">

<a href="https://discord.gg/YvBacAxu">
<img src="https://dcbadge.vercel.app/api/server/YvBacAxu?theme=discord" />
</a>

<br />

<a href="https://www.youtube.com/@CodingWallah-Family/streams">
<img src="https://img.shields.io/youtube/channel/subscribers/UC7HdeXvGFw962sWv31mjEqA?label=Coding%20Wallah%20-%20family&logo=youtube&logoColor=ff0000&style=for-the-badge" />
</a>

<a href="https://www.youtube.com/@CodingWallahSir/videos">
<img src="https://img.shields.io/youtube/channel/subscribers/UC2EF2l4DhSG3PVBXVyQhHfA?label=Coding%20Wallah%20Sir&logo=youtube&logoColor=f00&style=for-the-badge" />
</a>

</div>

<!--

![Discord Server](https://dcbadge.vercel.app/api/server/YvBacAxu?theme=discord)

![Coding Wallah - Family](https://img.shields.io/youtube/channel/subscribers/UC7HdeXvGFw962sWv31mjEqA?label=Coding%20Wallah%20-%20family&logo=youtube&logoColor=ff0000&style=for-the-badge)

![Coding Wallah Sir](https://img.shields.io/youtube/channel/subscribers/UC2EF2l4DhSG3PVBXVyQhHfA?label=Coding%20Wallah%20Sir&logo=youtube&logoColor=f00&style=for-the-badge)

-->
132 changes: 71 additions & 61 deletions Sort Array 0-1-2.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,91 @@
### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#)



### Solution 1 `Best`
#### Time Complexity: `O(n)`
#### Space Complexity: `O(1)`
```java

//Best Solution 1 || Time = O(n) , Space = O(1)
class Solution {
public void sortColors(int[] nums) {
int start = 0;
int end = nums.length - 1;
int ptr = 0, temp = 0;
while (ptr <= end) {
switch (nums[ptr]) {
case 0: {
temp = nums[start];
nums[start] = nums[ptr];
nums[ptr] = temp;
start++;
ptr++;
break;
}
case 1:
ptr++;
break;
case 2: {
temp = nums[ptr];
nums[ptr] = nums[end];
nums[end] = temp;
end--;
break;
}
}
public void sortColors(int[] nums) {
int start = 0;
int end = nums.length - 1;
int ptr = 0, temp = 0;

while (ptr <= end) {
switch (nums[ptr]) {
case 0: {
temp = nums[start];
nums[start] = nums[ptr];
nums[ptr] = temp;
start++;
ptr++;
break;
}
case 1: {
ptr++;
break;
}
case 2: {
temp = nums[ptr];
nums[ptr] = nums[end];
nums[end] = temp;
end--;
break;
}
}
}
}
}

```

##

### Solution 2
#### Time Complexity: `O(n)`
#### Space Complexity: `O(1)`
```java

//Solution2 - time complexity = O(n) | space = O(1)
class Solution {
public void sortColors(int[] nums) {
int i=0, cnt0 = 0, cnt1 = 0, cnt2 = 0;
for (i = 0; i < nums.length; i++) {
if(nums[i]==0)
cnt0++;
else if(nums[i]==1)
cnt1++;
else if(nums[i]==2)
cnt2++;
}

i = 0;

while (cnt0 > 0) {
nums[i++] = 0;
cnt0--;
}

while (cnt1 > 0) {
nums[i++] = 1;
cnt1--;
}

while (cnt2 > 0) {
nums[i++] = 2;
cnt2--;
}
public void sortColors(int[] nums) {
int i = 0, count0 = 0, count1 = 0, count2 = 0;
for (i = 0; i < nums.length; i++) {
if (nums[i] == 0)
count0++;
else if (nums[i] == 1)
count1++;
else if (nums[i] == 2)
count2++;
}

i = 0;

while (count0 > 0) {
nums[i++] = 0;
count0--;
}

while (count1 > 0) {
nums[i++] = 1;
count1--;
}

while (count2 > 0) {
nums[i++] = 2;
count2--;
}
}
}

```
### Solution 3 `Not Recommended`
#### Time Complexity: `O(n*logn)`

```java

//sol3 || Time = O(nlogn)
class Solution {
public void sortColors(int[] nums) {
Arrays.sort(nums);
}
class Solution {
public void sortColors(int[] nums) {
Arrays.sort(nums);
}
}
```
106 changes: 54 additions & 52 deletions kth-largest-smallest.md
Original file line number Diff line number Diff line change
@@ -1,84 +1,86 @@
### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#)

1) min heap = uses for largest ?
##

#### `When` max Heaps and when min Heaps and `why`?

1) min heap = uses for largest ?
2) max heap = uses for smallest ?

### WHEN max and when min HEAPS and WHY ?
> #### :exclamation: k is small = min; | k is large = MAX;

Note- k is small = min; | k is large = MAX;
```java
##

//sol1 = using sorting or trick
// Time complexity = O(NlogN)
### Solution 1 : Using Sorting
#### Time Complexity: `O(n*logn)`
```java
class Solution {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length-k];
}
}

```

```java
##

//MIN HEAP APPROACH = Best Solution O(N + NlogK) time complexity
### Solution 2: Min Heap Approach
#### Time Complexity: `O(n + n*logk)`
`Best Solution`
```java
PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
for(int i=0;i<k;i++)
minheap.add(nums[i]);


for(int i=k;i<nums.length;i++){
if(nums[i]>minheap.peek()){
minheap.poll();
minheap.add(nums[i]);
}
}
for(int i=0;i<k;i++)
minheap.add(nums[i]);

for(int i=k;i<nums.length;i++){
if(nums[i]>minheap.peek()){
minheap.poll();
minheap.add(nums[i]);
}
}
return minheap.peek();
```
```java

//MIN HEAP EASY & CONCISE IMPLEMENTATION
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i : nums){
pq.add(i);
if(pq.size()>k){
pq.remove();
}
}

return pq.remove();
`Concise Implementation`

```java
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i : nums){
pq.add(i);
if(pq.size()>k){
pq.remove();
}
}
return pq.remove();
```

##

### Solution 3: Max Heap Approach
#### Time Complexity: `O(n + n*logk)`
`Best Solution`
```java
//MAX HEAP APPROACH = Best Solution O(N + NlogK) time complexity
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
for(int i=0;i<nums.length-k+1;i++)
maxHeap.add(nums[i]);

for(int i=nums.length-k+1;i<nums.length;i++){
if(nums[i]<maxHeap.peek()){
maxHeap.poll();
maxHeap.add(nums[i]);
}
}

return maxHeap.peek();
for(int i=0;i<nums.length-k+1;i++)
maxHeap.add(nums[i]);

for(int i=nums.length-k+1;i<nums.length;i++){
if(nums[i]<maxHeap.peek()){
maxHeap.poll();
maxHeap.add(nums[i]);
}

}
return maxHeap.peek();
```

`Consice Implementation`
```java
//MAX HEAP EASY & CONCISE IMPLEMENTATION
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
for(int i : nums){
pq.add(i);

for(int i=1;i<k;i++)
pq.poll();

return pq.peek();

for(int i : nums){
pq.add(i);

for(int i=1;i<k;i++)
pq.poll();

return pq.peek();
```