MASTER DSA PROBLEMS - JAVA WITH
EXPLANATION
ARRAYS
Find Smallest Element
int min=arr[0]; for(int i=1;i<[Link];i++) if(arr[i]<min) min=arr[i];
Find Largest Element
int max=arr[0]; for(int i=1;i<[Link];i++) if(arr[i]>max) max=arr[i];
Reverse Array
int i=0,j=n-1; while(i<j){int t=a[i];a[i]=a[j];a[j]=t;i++;j--;}
Second Largest
Maintain first and second maximum variables.
Rotate Array by K
Reverse whole array then parts.
NUMBERS
Palindrome
Reverse number and compare.
Prime Check
Check divisibility till sqrt(n).
Factorial
Multiply 1 to n.
Fibonacci
Use loop to print series.
GCD
Use Euclidean algorithm.
NUMBER SYSTEM
Binary to Decimal
Multiply bits by powers of 2.
Decimal to Binary
Repeated division by 2.
Octal to Decimal
Multiply by powers of 8.
SORTING
Bubble Sort
Compare adjacent and swap.
Selection Sort
Select minimum and swap.
Insertion Sort
Insert element in sorted part.
Quick Sort
Partition and recurse.
Merge Sort
Divide and merge.
STRINGS
Palindrome String
Reverse and compare.
Reverse String
Use StringBuilder reverse.
Anagram
Sort both and compare.
Remove Vowels
Skip vowels.
Count Characters
Use HashMap.