Skip to content

Commit 5c34c90

Browse files
authored
Merge pull request CodingWallah#2 from PallavJain01/main
Added links for questions
2 parents 35aa325 + 3f201b9 commit 5c34c90

File tree

3 files changed

+182
-139
lines changed

3 files changed

+182
-139
lines changed

README.md

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,69 @@ These questions test your ability to apply various techniques such as hashing, s
44

55
## Foundation Questions ❤️
66
| Array Questions | Practice Link | Code Solutions | Video Solutions | HINT
7-
| ------------- | :-------------: | :-------------: | :---------: |:---------: |
8-
Wave Array
9-
Subarray with given sum
10-
Missing number in array
11-
Rotate Array | [Solve](https://leetcode.com/problems/rotate-array/) | [CODE](#) | [Video](https://youtu.be/0OTPqrEd74g)
12-
Find duplicates in an array
13-
Remove all occurrences of Given Number | [Solve](https://leetcode.com/problems/remove-element/)
14-
Remove Duplicates from Sorted Array | [Solve](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)
15-
Move all zeroes to end of array
16-
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](#)
17-
Intersection of two arrays
18-
Reverse an array
19-
Swapping Elements in an array
20-
Find Min and Max in an Array
7+
-|-|-|-|-
8+
Wave Array| [Solve](https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1?utm_source=geeksforgeeks) |
9+
Subarray with given sum| [Solve](https://Solve.com/problems/subarray-sum-equals-k/) |
10+
Missing number in array| [Solve](https://Solve.com/problems/missing-number/) |
11+
Rotate Array | [Solve](https://Solve.com/problems/rotate-array/) | | [Video](https://youtu.be/0OTPqrEd74g)
12+
Find duplicates in an array| [Solve](https://Solve.com/problems/find-all-duplicates-in-an-array/) |
13+
Remove all occurrences of Given Number | [Solve](https://Solve.com/problems/remove-element/)
14+
Remove Duplicates from Sorted Array | [Solve](https://Solve.com/problems/remove-duplicates-from-sorted-array/)
15+
Move all zeroes to end of array| [Solve](https://Solve.com/problems/move-zeroes/)
16+
Sort an array of 0s, 1s and 2s | [Solve](https://Solve.com/problems/sort-colors/submissions/) |
17+
Intersection of two arrays| [Solve](https://Solve.com/problems/intersection-of-two-arrays/)
18+
Reverse an array| [Solve](https://Solve.com/problems/reverse-string/)
19+
Swapping Elements in an array|
20+
Remove Min and Max in an Array| [Solve](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/)
2121

2222
## Medium Questions ❤️‍🔥
2323
| Array Questions | Practice Link | Code Solutions | Video Solutions | HINT
2424
| ------------- | :-------------: | :-------------: | :---------: |:---------: |
25-
Valid Moutain Array | [Solve](https://leetcode.com/problems/valid-mountain-array/) | [Code](#) | [Video](https://youtu.be/tVDTjm_fYbQ) | 2 pointers
26-
Majority Element | [Solve](https://leetcode.com/problems/majority-element/) | [Code](#) | [Video](https://www.youtube.com/watch?v=cLE1J34pYUo) | Sorting / or Boyer-Moore Voting Algorithm
27-
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
28-
Trapping Rain Water
29-
Coin Change
30-
Stock span problem
31-
Merge k Sorted Arrays
32-
Next Permutation | [Solve](https://leetcode.com/problems/next-permutation) | [Code](#) | [Video](#) | 2 poniters
33-
Kadane's Algorithm
25+
Valid Moutain Array | [Solve](https://Solve.com/problems/valid-mountain-array/) | [Code](#) | [Video](https://youtu.be/tVDTjm_fYbQ) | 2 pointers
26+
Majority Element | [Solve](https://Solve.com/problems/majority-element/) | [Code](#) | [Video](https://www.youtube.com/watch?v=cLE1J34pYUo) | Sorting / or Boyer-Moore Voting Algorithm
27+
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
28+
Trapping Rain Water|[Solve](https://Solve.com/problems/trapping-rain-water/)
29+
Coin Change|[Solve](https://Solve.com/problems/coin-change/)
30+
Stock span problem|[Solve](https://Solve.com/problems/online-stock-span/)
31+
Merge k Sorted Arrays|[Solve](https://Solve.com/problems/merge-k-sorted-lists/)
32+
Next Permutation |[Solve](https://Solve.com/problems/next-permutation) | [Code](#) | [Video](#) | 2 poniters
33+
Kadane's Algorithm|[Solve](https://Solve.com/problems/maximum-subarray)
3434

3535

3636

3737
## Hard Questions 💔
3838
| Array Questions | Practice Link | Code Solutions | Video Solutions | HINT
39-
| ------------- | :-------------: | :-------------: | :---------: |:---------: |
40-
Merge Without Extra Space
41-
Next Smallest Palindrome
39+
-|-|-|-|-
40+
Merge Without Extra Space|[GeeksForGeeks](https://practice.geeksforgeeks.org/problems/merge-two-sorted-arrays-1587115620/1)
41+
Next Smallest Palindrome|[GeeksForGeeks](https://practice.geeksforgeeks.org/problems/next-smallest-palindrome4740/1?utm_source=geeksforgeeks)
42+
43+
44+
---
45+
46+
<div align="center">
47+
48+
<a href="https://discord.gg/YvBacAxu">
49+
<img src="https://dcbadge.vercel.app/api/server/YvBacAxu?theme=discord" />
50+
</a>
51+
52+
<br />
53+
54+
<a href="https://www.youtube.com/@CodingWallah-Family/streams">
55+
<img src="https://img.shields.io/youtube/channel/subscribers/UC7HdeXvGFw962sWv31mjEqA?label=Coding%20Wallah%20-%20family&logo=youtube&logoColor=ff0000&style=for-the-badge" />
56+
</a>
57+
58+
<a href="https://www.youtube.com/@CodingWallahSir/videos">
59+
<img src="https://img.shields.io/youtube/channel/subscribers/UC2EF2l4DhSG3PVBXVyQhHfA?label=Coding%20Wallah%20Sir&logo=youtube&logoColor=f00&style=for-the-badge" />
60+
</a>
61+
62+
</div>
63+
64+
<!--
65+
66+
![Discord Server](https://dcbadge.vercel.app/api/server/YvBacAxu?theme=discord)
67+
68+
![Coding Wallah - Family](https://img.shields.io/youtube/channel/subscribers/UC7HdeXvGFw962sWv31mjEqA?label=Coding%20Wallah%20-%20family&logo=youtube&logoColor=ff0000&style=for-the-badge)
69+
70+
![Coding Wallah Sir](https://img.shields.io/youtube/channel/subscribers/UC2EF2l4DhSG3PVBXVyQhHfA?label=Coding%20Wallah%20Sir&logo=youtube&logoColor=f00&style=for-the-badge)
71+
72+
-->

Sort Array 0-1-2.md

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,91 @@
11
### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#)
2+
3+
4+
5+
### Solution 1 `Best`
6+
#### Time Complexity: `O(n)`
7+
#### Space Complexity: `O(1)`
28
```java
39

4-
//Best Solution 1 || Time = O(n) , Space = O(1)
510
class Solution {
6-
public void sortColors(int[] nums) {
7-
int start = 0;
8-
int end = nums.length - 1;
9-
int ptr = 0, temp = 0;
10-
while (ptr <= end) {
11-
switch (nums[ptr]) {
12-
case 0: {
13-
temp = nums[start];
14-
nums[start] = nums[ptr];
15-
nums[ptr] = temp;
16-
start++;
17-
ptr++;
18-
break;
19-
}
20-
case 1:
21-
ptr++;
22-
break;
23-
case 2: {
24-
temp = nums[ptr];
25-
nums[ptr] = nums[end];
26-
nums[end] = temp;
27-
end--;
28-
break;
29-
}
30-
}
11+
public void sortColors(int[] nums) {
12+
int start = 0;
13+
int end = nums.length - 1;
14+
int ptr = 0, temp = 0;
15+
16+
while (ptr <= end) {
17+
switch (nums[ptr]) {
18+
case 0: {
19+
temp = nums[start];
20+
nums[start] = nums[ptr];
21+
nums[ptr] = temp;
22+
start++;
23+
ptr++;
24+
break;
25+
}
26+
case 1: {
27+
ptr++;
28+
break;
29+
}
30+
case 2: {
31+
temp = nums[ptr];
32+
nums[ptr] = nums[end];
33+
nums[end] = temp;
34+
end--;
35+
break;
3136
}
37+
}
3238
}
39+
}
3340
}
34-
3541
```
3642

43+
##
44+
45+
### Solution 2
46+
#### Time Complexity: `O(n)`
47+
#### Space Complexity: `O(1)`
3748
```java
3849

39-
//Solution2 - time complexity = O(n) | space = O(1)
4050
class Solution {
41-
public void sortColors(int[] nums) {
42-
int i=0, cnt0 = 0, cnt1 = 0, cnt2 = 0;
43-
for (i = 0; i < nums.length; i++) {
44-
if(nums[i]==0)
45-
cnt0++;
46-
else if(nums[i]==1)
47-
cnt1++;
48-
else if(nums[i]==2)
49-
cnt2++;
50-
}
51-
52-
i = 0;
53-
54-
while (cnt0 > 0) {
55-
nums[i++] = 0;
56-
cnt0--;
57-
}
58-
59-
while (cnt1 > 0) {
60-
nums[i++] = 1;
61-
cnt1--;
62-
}
63-
64-
while (cnt2 > 0) {
65-
nums[i++] = 2;
66-
cnt2--;
67-
}
51+
public void sortColors(int[] nums) {
52+
int i = 0, count0 = 0, count1 = 0, count2 = 0;
53+
for (i = 0; i < nums.length; i++) {
54+
if (nums[i] == 0)
55+
count0++;
56+
else if (nums[i] == 1)
57+
count1++;
58+
else if (nums[i] == 2)
59+
count2++;
60+
}
61+
62+
i = 0;
63+
64+
while (count0 > 0) {
65+
nums[i++] = 0;
66+
count0--;
67+
}
68+
69+
while (count1 > 0) {
70+
nums[i++] = 1;
71+
count1--;
6872
}
73+
74+
while (count2 > 0) {
75+
nums[i++] = 2;
76+
count2--;
77+
}
78+
}
6979
}
7080

7181
```
82+
### Solution 3 `Not Recommended`
83+
#### Time Complexity: `O(n*logn)`
7284

7385
```java
74-
75-
//sol3 || Time = O(nlogn)
76-
class Solution {
77-
public void sortColors(int[] nums) {
78-
Arrays.sort(nums);
79-
}
86+
class Solution {
87+
public void sortColors(int[] nums) {
88+
Arrays.sort(nums);
89+
}
8090
}
8191
```

kth-largest-smallest.md

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,86 @@
11
### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#)
22

3-
1) min heap = uses for largest ?
3+
##
4+
5+
#### `When` max Heaps and when min Heaps and `why`?
46

7+
1) min heap = uses for largest ?
58
2) max heap = uses for smallest ?
69

7-
### WHEN max and when min HEAPS and WHY ?
10+
> #### :exclamation: k is small = min; | k is large = MAX;
811
9-
Note- k is small = min; | k is large = MAX;
10-
```java
12+
##
1113

12-
//sol1 = using sorting or trick
13-
// Time complexity = O(NlogN)
14+
### Solution 1 : Using Sorting
15+
#### Time Complexity: `O(n*logn)`
16+
```java
1417
class Solution {
1518
public int findKthLargest(int[] nums, int k) {
1619
Arrays.sort(nums);
1720
return nums[nums.length-k];
1821
}
1922
}
20-
2123
```
2224

23-
```java
25+
##
2426

25-
//MIN HEAP APPROACH = Best Solution O(N + NlogK) time complexity
27+
### Solution 2: Min Heap Approach
28+
#### Time Complexity: `O(n + n*logk)`
29+
`Best Solution`
30+
```java
2631
PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
27-
for(int i=0;i<k;i++)
28-
minheap.add(nums[i]);
29-
30-
31-
for(int i=k;i<nums.length;i++){
32-
if(nums[i]>minheap.peek()){
33-
minheap.poll();
34-
minheap.add(nums[i]);
35-
}
36-
}
32+
for(int i=0;i<k;i++)
33+
minheap.add(nums[i]);
34+
35+
for(int i=k;i<nums.length;i++){
36+
if(nums[i]>minheap.peek()){
37+
minheap.poll();
38+
minheap.add(nums[i]);
39+
}
40+
}
3741
return minheap.peek();
3842
```
39-
```java
4043

41-
//MIN HEAP EASY & CONCISE IMPLEMENTATION
42-
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
43-
for(int i : nums){
44-
pq.add(i);
45-
if(pq.size()>k){
46-
pq.remove();
47-
}
48-
}
49-
50-
return pq.remove();
44+
`Concise Implementation`
5145

46+
```java
47+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
48+
for(int i : nums){
49+
pq.add(i);
50+
if(pq.size()>k){
51+
pq.remove();
52+
}
53+
}
54+
return pq.remove();
5255
```
5356

57+
##
5458

59+
### Solution 3: Max Heap Approach
60+
#### Time Complexity: `O(n + n*logk)`
61+
`Best Solution`
5562
```java
56-
//MAX HEAP APPROACH = Best Solution O(N + NlogK) time complexity
5763
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
58-
for(int i=0;i<nums.length-k+1;i++)
59-
maxHeap.add(nums[i]);
60-
61-
for(int i=nums.length-k+1;i<nums.length;i++){
62-
if(nums[i]<maxHeap.peek()){
63-
maxHeap.poll();
64-
maxHeap.add(nums[i]);
65-
}
66-
}
67-
68-
return maxHeap.peek();
64+
for(int i=0;i<nums.length-k+1;i++)
65+
maxHeap.add(nums[i]);
66+
67+
for(int i=nums.length-k+1;i<nums.length;i++){
68+
if(nums[i]<maxHeap.peek()){
69+
maxHeap.poll();
70+
maxHeap.add(nums[i]);
6971
}
70-
72+
}
73+
return maxHeap.peek();
7174
```
7275

76+
`Consice Implementation`
7377
```java
74-
//MAX HEAP EASY & CONCISE IMPLEMENTATION
7578
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
76-
for(int i : nums){
77-
pq.add(i);
78-
79-
for(int i=1;i<k;i++)
80-
pq.poll();
81-
82-
return pq.peek();
83-
79+
for(int i : nums){
80+
pq.add(i);
81+
82+
for(int i=1;i<k;i++)
83+
pq.poll();
84+
85+
return pq.peek();
8486
```

0 commit comments

Comments
 (0)