15 Question Solution With Dry Run Notes
15 Question Solution With Dry Run Notes
https://www.linkedin.com/in/manojoffcialmj/
Example 1:
Example 1: Example 3:
Input: nums = [1,3,4,2,2] Input: nums = [2,2,2,2,2]
Output: 2 Output: 2
Example 2: Example 4:
Input: nums = [3,1,3,4,2] Input: nums = [1,2,3,4]
Output: 3 Output: -1
APPROACH:
Method 01: Sorting approach
Method 02: Negative marking approach
Method 03: Position and swapping approach
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Problem: Given an array arr[] of size N having integers in the range [1, N] with some of the elements missing.
The task is to find the missing elements.
Approach:
- Negative marking approach
- Sorting approach
Iteration:0
Iteration:1
Iteration:2
Iteration:3
Iteration:4
Brute force approach
Step 02: Traverse array to check each element if it has occurrence in future
Hashing: We can use unordered map to store array's element in pair of key and value
Optimal Approach:
Step 01: remove duplicates from sorted array
Step 02: store common value of all three arrays in new array
Step 01: store unique element in new array with the help of C++ STL Data Structure is set(dataType)
Declaration of set
Step 02: store common value of all three arrays in new array
7. Wave Print A Matrix (GFG)
Approach:
- When number of column is even then print row top to bottom
- When number of column is odd then print row bottom to top
Output:
1 2 3 4 5 6 12 18 24 30 29 28 27 26 25
19 13 7 8 9 10 11 17 23 22 21 14 15 16
Optimal Approach:
Step 01: find total elements
Step 02: iterate matrix till end of the total element and print
- Print startingRow
- Print endingCol
- Print endingRow
- Print startingCol
Example 05:
Input : A[] = {0, 0, 2, 1, 4}, B[] = {0, 0, 2, 1, 4}
Output : 428
Example 02:
Iteration 0
Iteration 1
Iteration 2
Iteration 1
Iteration 2
Iteration 3
Iteration 4
9. Factorial of A Large Number (GFG)