Coding Notes New
Coding Notes New
Question 2:
If we xor the given array with array of 1 to N-1 then the duplicate element
occurs thrice and all others occurs twice so only the duplicate element
gets left behind
Problem 3:
Because of the constraints given we can also set the value above 10^5 or
below 0 instead if INT_MIN
Optimisation 1:
Optimisation 2: O(max(M,N))
Problem 4:
Problem 5:
https://www.geeksforgeeks.org/problems/data-type-1666706751/1?
utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=data-type
In java for List<Double> arr; arr[0] does not work we must use arr.get[0]
Integer.SIZE: This returns the size of the int type in bits. The
constant Integer.SIZE is 32 because an int in Java is 32 bits.
Integer.SIZE / 8: Divides the size in bits by 8 to convert it to bytes.
So, 32 bits / 8 = 4 bytes.
2D Arrays
Searching in 2D arrays in Java
Here we use return new int[]{row, col}; because we haven’t declared it unlike below
Binary Search
If target is not found then the answer would be at arr[start] at the last
If we need to find element greatest element smaller than target the answer would be arr[end].
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/
Ans:- https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/lectures/10-binary
%20search/code/src/com/kunal/FirstAndLastPosition.java
https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/lectures/10-binary%20search/
code/src/com/kunal/InfiniteArray.java
https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/lectures/10-binary%20search/
code/src/com/kunal/Mountain.java
https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/lectures/10-binary%20search/
code/src/com/kunal/SearchInMountain.java
https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/lectures/10-binary%20search/
code/src/com/kunal/RBS.java -->Binary Search in Rotated array
https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/lectures/10-binary%20search/
code/src/com/kunal/RotationCount.java -->if duplicates exist in Rotated sorted array
https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/lectures/10-binary%20search/
code/src/com/kunal/SplitArray.java -->split array largest sum
Binary Search in 2D arrays
If we go through each row and compare with first and last element of that row and then perform a
binary search the time complexity would be O(n)+O(log2m) since after going through each row we
only perform binary search on one row not on all rows.
https://www.youtube.com/watch?v=JXU4Akft7yk&t=345s
Recursion:
F(n)=F(n-1)+F(n-2) Linear Recurrence relationSame functions are being
called again and again to avoid this we use DP
F(n) =O(1)+F(n/2) Divide and Conquer recurrence relation.
For recursion space complexity = O(height of the recursion tree)
Palindrome or not
JAVA
Way -2
Multiple Occurrences:
Copy of ref to list is being passed so all changes will be reflected in above.
What If we don’t want to take list as an argument but want to return arraylist then
The above is not optimized because we are creating array list for each step in recursive
tree.
If we created an arraylist for each it looks like above it is inefficient. If needed, we should
pass it as an argument.
It is somewhat similar to below code where we created new arraylist for every iteration
ASCII value of a character
o/p:-97
o/p:-b
If we want to include ASCII values as well then we should use the code
o/p:-
Using iteration:
In every level above copy + adding current element to all subsets in above copy
Following above process 3 would be added
Time complexity:O(N*2^N)
Space Complexity: O(N*2^N);
When you find a duplicate element only add it to the newly created subset. -->This only
works if duplicates are together i.e works for [1,2,2] but not for [2,1,2] i.e array should be
sorted.
For Permuations and Combinations , Recursion call can have more than 2 sub calls so we use
for loop along with Recursion and the no:of calls =size of processes string+1
Question: https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
description/
Answer: https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/main/lectures/14-
recursion/code/src/com/kunal/strings/Dice.java
Backtracking:
If we consider first row as n and first column as n in a n*n matrix
And if we can only go right or down then no: of paths to reach last block.
If we wanna print the path then
If there are is a restriction in a maze like we are not supposed to enter a block here in this
case it’s board[1][1].
If all paths are allowed then we will go into an recursion that will never end because we will
go to the same path again and again to avoid this we can mark the cells that we already
traversed.
All cells that are visited mark them as false
Making cell=false means we have that cell in the current path and if that path is over this cell
must be made true again to include it in other paths. i.e while you move back we restore
maze to as it was This is backtracking.
DP-Optimization Problems
Sometimes BT problems can be solved using greedy and if greedy doesn’t work then BT must be used
Rat in a Maze:
https://takeuforward.org/data-structure/rat-in-a-maze/
Printing Subsequences:
o/p:
If we want in the reverse manner we can use below code
If we want the count of subsequences resulting in the given sum we can use below code
If array contains positives only we can also use the base condition if(s>sum) return 0; in the
above code.
If we want to print just 1 result instead of all combinations, we can use below code
Heap & Priority Queue:
If we want the smallest element then we should use max heap and for largest element we
should use min heap for optimized solution
In C++, the std::priority_queue is a container adaptor that provides a priority queue interface.
The std::priority_queue uses another container to store the elements, and it maintains the
heap property using the specified comparison function.
Dynamic Programming:
Top Down: Recursion
https://takeuforward.org/data-structure/dynamic-programming-introduction/
Recursion: All possible Ways, Count all ways
DP: - usually Best way
Memoization to tabulation:
Base case
Start from Reverse of changing parameters
Copy the recurrence
e.g:
https://www.youtube.com/watch?
v=EgG3jsGoPvQ&list=PLgUwDviBIf0qUlt5H_kiKYaNSqJ81PMMY&index=4
https://www.naukri.com/code360/problems/frog-jump_3621012
using Recursion Using steps to solve the problem after identification as given above
if(index==0) return 0;
single_jump = f(ind-1)+abs(a[ind]-a[ind-1]);
if(ind>1)
Conversion to Tabulation:
If(ind>1)
dp[ind]=min(single_jump, double_jump);
}
If there is ind-1 and ind-2 there is always chance of space optimization.
Problem 1: https://leetcode.com/problems/climbing-stairs/
Using only Recursion gives TLE:
Top Down with Memoization:
Problem 2: https://leetcode.com/problems/min-cost-climbing-stairs/description/
Top Down(Recursion+Memoization—Giving TLE for large n)
Way-2
Bottom up Approach