├── .vscode └── settings.json ├── ArrayQuestions.md ├── ArraysSolutions ├── Find All Duplicates in an Array.md ├── Intersection of two arrays.md ├── Missing Number.md ├── Remove all occurrence of Given number.md ├── Reverse an Array.md ├── Rotate-Array.md ├── Sort Array 0-1-2.md ├── Subarray-sum-equals-k.md ├── Wave Array Solution.md └── kth-largest-smallest.md ├── BinarySearch.md ├── BinarySearchSolutions ├── CountDuplicates.md ├── FindInRSA.md ├── FindMaxInRotatedSortedArray.java ├── First&Last.md ├── InsertPosn.md ├── MaxInRSA.md ├── MinmInRSA.md ├── PeakElementMountain.java └── Rotation-of-RSA.md ├── LecturesCode ├── ArraysLecture.java ├── BinarySearchLecture.java ├── ConditionalStatements.java ├── DataTypesLecture.java ├── InputOutputLecture.java ├── LoopsLecture.java ├── MethodsLecture.java ├── MostlyAskedBasicQuestions.java ├── OperatorsLecture.java ├── OutputQuestions.java ├── PatternPrograms.java ├── RecursionLecture.java ├── RuffClass.java ├── Searching.java └── TxtInputListener.java ├── LinkedList.md ├── PatternQuestions.md └── README.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "terminal.integrated.fontSize": 18 3 | } -------------------------------------------------------------------------------- /ArrayQuestions.md: -------------------------------------------------------------------------------- 1 | # Arrays-DSA-Coding-Questions 🚀 2 | Arrays are one of the most fundamental and widely used data structures in programming. 3 | These questions test your ability to apply techniques such as hashing, sorting, binary search, two-pointers, sliding window, divide and conquer, dynamic programming, etc. 4 | 5 | ## Foundation Questions ❤️ 6 | | Array Questions | Practice Link | Code Solutions | Video Solutions | HINT 7 | -|-|-|-|- 8 | Wave Array| [GeeksForGeeks](https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1?utm_source=geeksforgeeks) | 9 | Subarray with given sum| [Leetcode](https://Leetcode.com/problems/subarray-sum-equals-k/) | 10 | Missing number in array| [Leetcode](https://Leetcode.com/problems/missing-number/) | 11 | Rotate Array | [Leetcode](https://Leetcode.com/problems/rotate-array/) | | [Video](https://youtu.be/0OTPqrEd74g) 12 | Find duplicates in an array| [Leetcode](https://Leetcode.com/problems/find-all-duplicates-in-an-array/) | 13 | Remove all occurrences of Given Number | [Leetcode](https://Leetcode.com/problems/remove-element/) 14 | Remove Duplicates from Sorted Array | [Leetcode](https://Leetcode.com/problems/remove-duplicates-from-sorted-array/) 15 | Move all zeroes to the end of the array| [Leetcode](https://Leetcode.com/problems/move-zeroes/) 16 | Sort an array of 0s, 1s and 2s | [Leetcode](https://Leetcode.com/problems/sort-colors/submissions/) | 17 | Intersection of two arrays| [Leetcode](https://Leetcode.com/problems/intersection-of-two-arrays/) 18 | Reverse an array| [Leetcode](https://Leetcode.com/problems/reverse-string/) 19 | Swapping Elements in an Array | 20 | Remove Min and Max in an Array| [Leetcode](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/) 21 | Two Sum | [Leetcode](https://leetcode.com/problems/two-sum/) 22 | 23 | ## Medium Questions ❤️‍🔥 24 | | Array Questions | Practice Link | Code Solutions | Video Solutions | HINT 25 | | ------------- | :-------------: | :-------------: | :---------: |:---------: | 26 | Valid Moutain Array | [Leetcode](https://Leetcode.com/problems/valid-mountain-array/) | [Code](#) | [Video](https://youtu.be/tVDTjm_fYbQ) | 2 pointers 27 | Majority Element | [Leetcode](https://Leetcode.com/problems/majority-element/) | [Code](#) | [Video](https://www.youtube.com/watch?v=cLE1J34pYUo) | Sorting / or Boyer-Moore Voting Algorithm 28 | Kth largest/smallest element in an array | [Leetcode](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 29 | Trapping Rain Water|[Leetcode](https://Leetcode.com/problems/trapping-rain-water/) 30 | Coin Change|[Leetcode](https://Leetcode.com/problems/coin-change/) 31 | Stock span problem|[Leetcode](https://Leetcode.com/problems/online-stock-span/) 32 | Merge k Sorted Arrays|[Leetcode](https://Leetcode.com/problems/merge-k-sorted-lists/) 33 | Next Permutation |[Leetcode](https://Leetcode.com/problems/next-permutation) | [Code](#) | [Video](#) | 2 poniters 34 | Kadane's Algorithm|[Leetcode](https://Leetcode.com/problems/maximum-subarray) 35 | 36 | 37 | 38 | ## Hard Questions 💔 39 | | Array Questions | Practice Link | Code Solutions | Video Solutions | HINT 40 | -|-|-|-|- 41 | Merge Without Extra Space|[GeeksForGeeks](https://practice.geeksforgeeks.org/problems/merge-two-sorted-arrays-1587115620/1) 42 | Next Smallest Palindrome|[GeeksForGeeks](https://practice.geeksforgeeks.org/problems/next-smallest-palindrome4740/1?utm_source=geeksforgeeks) 43 | 44 | 45 | --- 46 | 47 |
48 | 49 | 50 | 51 | 52 | 53 |
54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
64 | 65 | 74 | -------------------------------------------------------------------------------- /ArraysSolutions/Find All Duplicates in an Array.md: -------------------------------------------------------------------------------- 1 | ## Solution of Find All Duplicates in an Array 2 | 3 | ### First Approach 4 | 5 | #### Time Complexity - O (nlogn) 6 | 7 | #### Space Complexity - O(1) 8 | 9 | ```java 10 | 11 | class Solution { 12 | 13 | public List findDuplicates(int[] nums) { 14 | 15 | List list = new ArrayList (); 16 | 17 | int start=0; 18 | 19 | int ref; 20 | 21 | Arrays.sort(nums); 22 | 23 | for (int i = 0 ; i < nums.length-1 ; i++){ 24 | 25 | ref = nums[i] ^ nums[i+1]; 26 | 27 | if(ref == 0 ){ 28 | 29 | list.add(start,nums[i]); 30 | 31 | start++; 32 | 33 | } 34 | 35 | } 36 | 37 | return list; 38 | 39 | } 40 | 41 | } 42 | 43 | ``` 44 | ### Second Approach 45 | 46 | #### Time Complexity - O (n) 47 | 48 | #### Space Complexity - O(1) 49 | 50 | ```java 51 | class Solution { 52 | public List findDuplicates(int[] nums) { 53 | List list = new ArrayList (); 54 | int n = nums.length; 55 | int index; 56 | for (int i = 0 ; i < n; i++){ 57 | int num = nums[i]; 58 | index = Math.abs(num) - 1 ; 59 | if ( nums[index] < 0){ 60 | list.add(Math.abs(nums[i])); 61 | } 62 | else{ 63 | nums[index] = nums[index] * -1; 64 | } 65 | } 66 | return list; 67 | } 68 | } 69 | 70 | ``` 71 | 72 | 73 |
74 | Contributed By Muneer Ahmad 75 |
76 | -------------------------------------------------------------------------------- /ArraysSolutions/Intersection of two arrays.md: -------------------------------------------------------------------------------- 1 | 2 | ## Solution of Intersection Of Two Arrays 3 | 4 | #### Time Complexity - O (n*m) 5 | 6 | #### Space Complexity - O(n) 7 | 8 | ```java 9 | 10 | class Solution { 11 | public int[] intersection(int[] nums1, int[] nums2) { 12 | HashSet hs = new HashSet<>(); 13 | for (int i = 0; i hs = new HashSet<>(); 49 | Arrays.sort(nums1); 50 | Arrays.sort(nums2); 51 | int i=0; 52 | int j=0; 53 | while(i 80 | Contributed By Love Garg 81 | -------------------------------------------------------------------------------- /ArraysSolutions/Missing Number.md: -------------------------------------------------------------------------------- 1 | 2 | ## Solution of Missing-Number 3 | #### Time Complexity - O (n) 4 | #### Space Complexity - O(1) 5 | 6 | ```java 7 | class Solution { 8 | public int missingNumber(int[] nums) { 9 | int num = nums.length; 10 | int sunOfNaturalNumbers = num*(num+1)/2; 11 | int sumOfArrayNum = 0; 12 | for(int i = 0; i 20 | Contributed By Muneer Ahmad 21 | -------------------------------------------------------------------------------- /ArraysSolutions/Remove all occurrence of Given number.md: -------------------------------------------------------------------------------- 1 | ## Solution of Remove all occurrences of Given Number 2 | 3 | #### Time Complexity - O (n) 4 | 5 | #### Space Complexity - O(1) 6 | 7 | ```java 8 | 9 | class Solution { 10 | public int removeElement(int[] nums, int val) { 11 | 12 | int i = 0; 13 | int n = nums.length; 14 | while (i < n) { 15 | if (nums[i] == val) { 16 | nums[i] = nums[n - 1]; 17 | n--; 18 | } else { 19 | i++; 20 | } 21 | } 22 | return n; 23 | } 24 | } 25 | 26 | ``` 27 |
28 | Contributed By Love Garg 29 |
-------------------------------------------------------------------------------- /ArraysSolutions/Reverse an Array.md: -------------------------------------------------------------------------------- 1 | ## Solution of Reverse an Array 2 | #### Time Complexity - O (n) 3 | #### Space Complexity - O(1) 4 | 5 | ```java 6 | class Solution { 7 | public void reverseString(char[] s) { 8 | int start = 0; 9 | int end = s.length-1; 10 | while ( start < end){ 11 | char temp = s[start]; 12 | s[start] = s[end]; 13 | s[end]= temp; 14 | start++; 15 | end--; 16 | } 17 | } 18 | } 19 | ``` 20 |
21 | Contributed By Muneer Ahmad 22 |
-------------------------------------------------------------------------------- /ArraysSolutions/Rotate-Array.md: -------------------------------------------------------------------------------- 1 | ## Solution of Rotate-Array 2 | #### Time Complexity - O (n) 3 | #### Space Complexity - O(1) 4 | 5 | ```java 6 | class Solution { 7 | public static void reverse(int [] a , int start , int end ){ 8 | while (start 27 | Contributed By Muneer Ahmad 28 | -------------------------------------------------------------------------------- /ArraysSolutions/Sort Array 0-1-2.md: -------------------------------------------------------------------------------- 1 | ### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#) 2 | 3 | ## 4 | 5 | ### Solution 1 `Not Recommended` 6 | #### Time Complexity: `O(n*logn)` 7 | 8 | ```java 9 | class Solution { 10 | public void sortColors(int[] nums) { 11 | Arrays.sort(nums); 12 | } 13 | } 14 | ``` 15 | 16 | ### Solution 2 `Alternative` 17 | #### Time Complexity: `O(n)` 18 | #### Space Complexity: `O(1)` 19 | ```java 20 | 21 | class Solution { 22 | public void sortColors(int[] nums) { 23 | int i = 0, count0 = 0, count1 = 0, count2 = 0; 24 | for (i = 0; i < nums.length; i++) { 25 | if (nums[i] == 0) 26 | count0++; 27 | else if (nums[i] == 1) 28 | count1++; 29 | else if (nums[i] == 2) 30 | count2++; 31 | } 32 | 33 | i = 0; 34 | 35 | while (count0 > 0) { 36 | nums[i++] = 0; 37 | count0--; 38 | } 39 | 40 | while (count1 > 0) { 41 | nums[i++] = 1; 42 | count1--; 43 | } 44 | 45 | while (count2 > 0) { 46 | nums[i++] = 2; 47 | count2--; 48 | } 49 | } 50 | } 51 | ``` 52 | 53 | ### Solution 3 `Best` 54 | #### Time Complexity: `O(n)` 55 | #### Space Complexity: `O(1)` 56 | 57 | ```java 58 | 59 | class Solution { 60 | public void sortColors(int[] nums) { 61 | int start = 0; 62 | int end = nums.length - 1; 63 | int ptr = 0, temp = 0; 64 | 65 | while (ptr <= end) { 66 | switch (nums[ptr]) { 67 | case 0: { 68 | temp = nums[start]; 69 | nums[start] = nums[ptr]; 70 | nums[ptr] = temp; 71 | start++; 72 | ptr++; 73 | break; 74 | } 75 | case 1: { 76 | ptr++; 77 | break; 78 | } 79 | case 2: { 80 | temp = nums[ptr]; 81 | nums[ptr] = nums[end]; 82 | nums[end] = temp; 83 | end--; 84 | break; 85 | } 86 | } 87 | } 88 | } 89 | } 90 | ``` -------------------------------------------------------------------------------- /ArraysSolutions/Subarray-sum-equals-k.md: -------------------------------------------------------------------------------- 1 | #### Time Complexity - O (n^2) 2 | #### Space Complexity - O(1) 3 | 4 | ```java 5 | class Solution { 6 | public int subarraySum(int[] nums, int k) { 7 | int count = 0; 8 | for(int i = 0 ; i < nums.length ; i++){ 9 | int sum = 0; 10 | for (int j = i ; j < nums.length ; j++){ 11 | sum+=nums[j]; 12 | if (sum == k){ 13 | count++; 14 | } 15 | } 16 | } 17 | return count++; 18 | } 19 | } 20 | ``` 21 |
22 | Contributed By Muneer Ahmad 23 |
24 | -------------------------------------------------------------------------------- /ArraysSolutions/Wave Array Solution.md: -------------------------------------------------------------------------------- 1 | ## Solution of Wave Array 2 | #### Time Complexity - O (n) 3 | #### Space Complexity - O(1) 4 | 5 | ```java 6 | class Solution { 7 | 8 | public static void convertToWave(int n, int[] a) { 9 | 10 | for (int i = 0; i 20 | Contributed By Muneer Ahmad 21 | 22 | -------------------------------------------------------------------------------- /ArraysSolutions/kth-largest-smallest.md: -------------------------------------------------------------------------------- 1 | ### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#) 2 | 3 | ## 4 | 5 | #### `When` max Heaps and when min Heaps and `why`? 6 | 7 | 1) min heap = uses for largest ? 8 | 2) max heap = uses for smallest ? 9 | 10 | > #### :exclamation: k is small = min; | k is large = MAX; 11 | 12 | ## 13 | 14 | ### Solution 1 : Using Sorting 15 | #### Time Complexity: `O(n*logn)` 16 | `Not Recommended` 17 | ```java 18 | class Solution { 19 | public int findKthLargest(int[] nums, int k) { 20 | Arrays.sort(nums); 21 | return nums[nums.length-k]; 22 | } 23 | } 24 | ``` 25 | 26 | ## 27 | 28 | ### Solution 2: Min Heap Approach 29 | #### Time Complexity: `O(n + n*logk)` 30 | 31 | `Concise Implementation` 32 | 33 | ```java 34 | PriorityQueue pq = new PriorityQueue(); 35 | for(int i : nums){ 36 | pq.add(i); 37 | if(pq.size()>k){ 38 | pq.remove(); 39 | } 40 | } 41 | return pq.remove(); 42 | ``` 43 | 44 | `Best Solution` 45 | ```java 46 | PriorityQueue minheap = new PriorityQueue(); 47 | for(int i=0;iminheap.peek()){ 52 | minheap.poll(); 53 | minheap.add(nums[i]); 54 | } 55 | } 56 | return minheap.peek(); 57 | ``` 58 | 59 | 60 | ## 61 | 62 | ### Solution 3: Max Heap Approach 63 | #### Time Complexity: `O(n + n*logk)` 64 | 65 | `Consice Implementation` 66 | ```java 67 | PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); 68 | for(int i : nums){ 69 | pq.add(i); 70 | 71 | for(int i=1;i maxHeap = new PriorityQueue(Collections.reverseOrder()); 80 | for(int i=0;i 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 61 | -------------------------------------------------------------------------------- /BinarySearchSolutions/CountDuplicates.md: -------------------------------------------------------------------------------- 1 | ## Solution of Count Duplicates using Linear Search 2 | #### Time Complexity - O (n) 3 | #### Space Complexity - O(1) 4 | 5 | ```java 6 | public class CountDuplicates { 7 | public static int countDuplicates(int[] arr) { 8 | if (arr.length == 0) { 9 | return 0; 10 | } 11 | 12 | int count = 0; 13 | for (int i = 0; i < arr.length - 1; i++) { 14 | if (arr[i] == arr[i + 1]) { 15 | count++; 16 | } 17 | } 18 | 19 | return count; 20 | } 21 | 22 | public static void main(String[] args) { 23 | int[] sortedArray = {1, 2, 2, 3, 3, 3, 4, 5, 5}; 24 | int duplicateCount = countDuplicates(sortedArray); 25 | System.out.println("Number of duplicates: " + duplicateCount); 26 | } 27 | } 28 | 29 | ``` 30 | 31 | ## Solution of Count Duplicates using Binary Search 32 | #### Time Complexity: 33 | - Best Case - O (logn) 34 | - But worst case - O(nlogn) 35 | #### Space Complexity - O(1) 36 | 37 | 🔥 Note : Using binary search to count duplicates in a sorted array won't be an efficient approach because binary search is designed to find specific elements in a sorted array, not to count duplicates. 38 | 39 | To count duplicates in a sorted array efficiently, you should use a linear scan, 40 | 41 | 42 | ```java 43 | class Solution { 44 | public int countDuplicates(int[] nums, int target) { 45 | int first = findFirst(nums, target); 46 | int last = findLast(nums, target); 47 | return last-first + 1; 48 | } 49 | static int findLast(int[] nums, int target) { 50 | int left = 0; 51 | int right = nums.length - 1; 52 | int lastOccurrence = -1; 53 | 54 | while (left <= right) { 55 | int mid = left + (right - left) / 2; 56 | 57 | if (nums[mid] == target) { 58 | lastOccurrence = mid; 59 | left = mid + 1; // Continue searching in the right half for the last occurrence 60 | } else if (nums[mid] < target) { 61 | left = mid + 1; 62 | } else { 63 | right = mid - 1; 64 | } 65 | } 66 | 67 | return lastOccurrence; 68 | } 69 | static int findFirst(int[] nums, int target) { 70 | int left = 0; 71 | int right = nums.length - 1; 72 | int firstOccurrence = -1; 73 | 74 | while (left <= right) { 75 | int mid = left + (right - left) / 2; 76 | 77 | if (nums[mid] == target) { 78 | firstOccurrence = mid; 79 | right = mid - 1; // Continue searching in the left half for the first occurrence 80 | } else if (nums[mid] < target) { 81 | left = mid + 1; 82 | } else { 83 | right = mid - 1; 84 | } 85 | } 86 | 87 | return firstOccurrence; 88 | } 89 | } 90 | 91 | 92 | 93 | 94 | ``` -------------------------------------------------------------------------------- /BinarySearchSolutions/FindInRSA.md: -------------------------------------------------------------------------------- 1 | ## Find in Sorted Rotated array ? 2 | #### Time Complexity - O (logn) 3 | #### Space Complexity - O(1) 4 | 5 | ## Solution1 6 | ```java 7 | public int search(int[] nums, int target) { 8 | 9 | int min = findMin(nums); 10 | 11 | int rs = bsearch(nums,target,0,min-1); 12 | if(rs!=-1) return rs; 13 | 14 | rs = bsearch(nums,target,min,nums.length-1); 15 | return rs; 16 | } 17 | 18 | public int findMin(int[] arr) { 19 | int start =0, end = arr.length-1; 20 | 21 | while(startarr[end]) start = mid+1; 24 | else end = mid; 25 | } 26 | return start; 27 | } 28 | 29 | public int bsearch(int[] nums, int target,int s, int e) 30 | { 31 | int start= s; 32 | int end = e; 33 | while(start<=end){ 34 | int mid = start + (end-start)/2; 35 | if(nums[mid] == target) return mid; 36 | else if(nums[mid] > target) end = mid-1; 37 | else start = mid+1; 38 | } 39 | return -1; 40 | } 41 | ``` 42 | ## Solution2 43 | ```java 44 | class Solution { 45 | public int search(int[] nums, int target) { 46 | int start = 0; 47 | int end = nums.length - 1; 48 | 49 | while (start <= end) { 50 | int mid = (start + end) / 2; 51 | 52 | //found at mid 53 | if (target == nums[mid]) return mid; 54 | 55 | // If the left side is sorted 56 | if (nums[start] <= nums[mid]) { 57 | if (nums[start] <= target && nums[mid] >= target) 58 | end = mid - 1; 59 | 60 | else start = mid + 1; 61 | } 62 | // If the right side is sorted 63 | else { 64 | if (nums[end] >= target && nums[mid] <= target) 65 | start = mid + 1; 66 | 67 | else end = mid - 1; 68 | } 69 | } 70 | return -1; 71 | } 72 | } 73 | 74 | ``` -------------------------------------------------------------------------------- /BinarySearchSolutions/FindMaxInRotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | package BinarySearchSolutions; 2 | 3 | public class FindMaxInRotatedSortedArray { 4 | public static int findMin(int[] arr) { 5 | int left = 0; 6 | int right = arr.length - 1; 7 | 8 | while (left < right) { 9 | int mid = left + (right - left) / 2; 10 | 11 | if (arr[mid] > arr[right]) { 12 | left = mid + 1; // minm is in the right subarray 13 | } else { 14 | right = mid; // minm is in the left subarray (including mid) 15 | } 16 | } 17 | 18 | return left; // The minm element is at index 'left' 19 | } 20 | 21 | public static void main(String[] args) { 22 | int[] rotatedArray = {4, 5, 6, 7, 9,3}; 23 | int maxElement = findMin(rotatedArray)-1; 24 | if (maxElement!=-1) 25 | System.out.println("Maximum element: " + rotatedArray[maxElement]); 26 | else 27 | System.out.println("Maximum element: " + rotatedArray[rotatedArray.length-1]); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /BinarySearchSolutions/First&Last.md: -------------------------------------------------------------------------------- 1 | # Find First and Last Position of Element in Sorted Array 2 | 3 | ### Time complexity - O(log n) 4 | ### Space complexity - O(1) 5 | 6 | ```java 7 | class Solution { 8 | public int[] searchRange(int[] nums, int target) { 9 | int[] ans = new int[2]; 10 | ans[0] = findFirst(nums, target); 11 | ans[1] = findLast(nums, target); 12 | return ans; 13 | } 14 | static int findLast(int[] nums, int target) { 15 | int left = 0; 16 | int right = nums.length - 1; 17 | int lastOccurrence = -1; 18 | 19 | while (left <= right) { 20 | int mid = left + (right - left) / 2; 21 | 22 | if (nums[mid] == target) { 23 | lastOccurrence = mid; 24 | left = mid + 1; // Continue searching in the right half for the last occurrence 25 | } else if (nums[mid] < target) { 26 | left = mid + 1; 27 | } else { 28 | right = mid - 1; 29 | } 30 | } 31 | 32 | return lastOccurrence; 33 | } 34 | static int findFirst(int[] nums, int target) { 35 | int left = 0; 36 | int right = nums.length - 1; 37 | int firstOccurrence = -1; 38 | 39 | while (left <= right) { 40 | int mid = left + (right - left) / 2; 41 | 42 | if (nums[mid] == target) { 43 | firstOccurrence = mid; 44 | right = mid - 1; // Continue searching in the left half for the first occurrence 45 | } else if (nums[mid] < target) { 46 | left = mid + 1; 47 | } else { 48 | right = mid - 1; 49 | } 50 | } 51 | 52 | return firstOccurrence; 53 | } 54 | } 55 | 56 | 57 | 58 | 59 | ``` -------------------------------------------------------------------------------- /BinarySearchSolutions/InsertPosn.md: -------------------------------------------------------------------------------- 1 | # Search Insert Position 2 | ## Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 3 | 4 | ### Time complexity - O(log n) 5 | ### Space complexity - O(1) 6 | 7 | ```java 8 | class Solution { 9 | public int searchInsert(int[] nums, int target) { 10 | 11 | int start= 0,mid=0; 12 | int end = nums.length-1; 13 | while(start<=end){ 14 | mid = start + (end-start)/2; 15 | if(nums[mid] == target) return mid; 16 | else if(nums[mid] > target) end = mid-1; 17 | else start = mid+1; 18 | } 19 | return start; 20 | 21 | } 22 | } 23 | ``` -------------------------------------------------------------------------------- /BinarySearchSolutions/MaxInRSA.md: -------------------------------------------------------------------------------- 1 | # Find Max In Rotated Sorted Array 2 | ```java 3 | 4 | public class FindMaxInRotatedSortedArray { 5 | public static int findMin(int[] arr) { 6 | int left = 0; 7 | int right = arr.length - 1; 8 | 9 | while (left < right) { 10 | int mid = left + (right - left) / 2; 11 | 12 | if (arr[mid] > arr[right]) { 13 | left = mid + 1; // minm is in the right subarray 14 | } else { 15 | right = mid; // minm is in the left subarray (including mid) 16 | } 17 | } 18 | 19 | return left; // The minm element is at index 'left' 20 | } 21 | 22 | public static void main(String[] args) { 23 | int[] rotatedArray = {4, 5, 6, 7, 9,3}; 24 | int maxElement = findMin(rotatedArray)-1; 25 | if (maxElement!=-1) 26 | System.out.println("Maximum element: " + rotatedArray[maxElement]); 27 | else 28 | System.out.println("Maximum element: " + rotatedArray[rotatedArray.length-1]); 29 | } 30 | } 31 | ``` -------------------------------------------------------------------------------- /BinarySearchSolutions/MinmInRSA.md: -------------------------------------------------------------------------------- 1 | # Find Minimum in Rotated Sorted Array 2 | 3 | ## Time = O (logn) 4 | ```java 5 | class Solution { 6 | public int findMin(int[] arr) { 7 | int start =0, end = arr.length-1; 8 | 9 | while(startarr[end]) 15 | start = mid+1; 16 | 17 | // If the mid element is less than or equal to the right element 18 | //the minimum must be on the left side, 19 | // including the mid element 20 | else 21 | end = mid; 22 | } 23 | return arr[start]; 24 | } 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | ``` -------------------------------------------------------------------------------- /BinarySearchSolutions/PeakElementMountain.java: -------------------------------------------------------------------------------- 1 | public class PeakElementMountain { 2 | public static void main(String[] args) { 3 | int nums[] = { 1, 2, 3, 1 }; 4 | System.out.println(peakElement(nums)); 5 | } 6 | 7 | public static int peakElement(int nums[]) { 8 | int start = 0, mid = 0, end = nums.length - 1; 9 | while (start < end) { 10 | mid = start + (end - start) / 2; 11 | // if mid lies on left line of mountain move start to mid+1 12 | if (nums[mid] < nums[mid + 1]) 13 | start = mid + 1; // Right Half 14 | else 15 | // if mid lies on peak of mountain we cant go back by moving end i.e.(end-1=mid) 16 | end = mid; // Left Half 17 | } 18 | return nums[start]; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BinarySearchSolutions/Rotation-of-RSA.md: -------------------------------------------------------------------------------- 1 | ## Solution 1 2 | ```java 3 | public class CountRotationsInSortedArray { 4 | public static int countRotations(int[] arr) { 5 | //rotation btw (0,n) only 6 | return findMin(arr); 7 | } 8 | 9 | public int findMin(int[] arr) { 10 | int start =0, end = arr.length-1; 11 | 12 | while(startarr[end]) start = mid+1; 15 | else 16 | end = mid; 17 | } 18 | return start; 19 | } 20 | } 21 | ``` 22 | ## Solution 2 23 | ```java 24 | public class CountRotationsInSortedArray { 25 | public static int countRotations(int[] arr) { 26 | int left = 0; 27 | int right = arr.length - 1; 28 | 29 | while (left <= right) { 30 | if (arr[left] <= arr[right]) { 31 | return left; // No rotations in the array 32 | } 33 | 34 | int mid = left + (right - left) / 2; 35 | int next = (mid + 1) % arr.length; 36 | int prev = (mid + arr.length - 1) % arr.length; 37 | 38 | if (arr[mid] <= arr[next] && arr[mid] <= arr[prev]) { 39 | return mid; // Number of rotations is equal to the index of the minimum element 40 | } else if (arr[mid] <= arr[right]) { 41 | right = mid - 1; // The minimum element is on the left side 42 | } else { 43 | left = mid + 1; // The minimum element is on the right side 44 | } 45 | } 46 | 47 | return -1; // Array is not rotated 48 | } 49 | 50 | public static void main(String[] args) { 51 | int[] rotatedArray = {4, 5, 6, 7, 0, 1, 2}; 52 | int rotations = countRotations(rotatedArray); 53 | System.out.println("Number of rotations: " + rotations); 54 | } 55 | } 56 | 57 | ``` -------------------------------------------------------------------------------- /LecturesCode/ArraysLecture.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class ArraysLecture { 5 | 6 | static int[] inputArray(){ 7 | Scanner scanner = new Scanner(System.in); 8 | System.out.println("Enter Size of Array"); 9 | int n = scanner.nextInt(); 10 | int[] intArray = new int[n]; 11 | for (int i = 0; i < n; i++) { 12 | System.out.println("Enter "+i+" Element of Array"); 13 | intArray[i] = scanner.nextInt(); 14 | } 15 | scanner.close(); 16 | return intArray; 17 | } 18 | 19 | static void outputArray(int[] intArray){ 20 | for (int i = 0; i < intArray.length; i++) { 21 | System.out.println(intArray[i]); 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | // All are valid declarations - 27 | 28 | int[] intArray = {10,20,30}; 29 | int[][] my2dArray = { 30 | {10,20,30}, 31 | {40,50,60} 32 | }; 33 | 34 | // outputArray(inputArray()); 35 | // System.out.println(intArray[0]); 36 | // System.out.println(my2dArray[0][0]); 37 | // for(int e : intArray){ 38 | // System.out.println(e); 39 | // } 40 | 41 | // System.out.println(intArray.length); 42 | // for(int i=0;i<=intArray.length;i++){ 43 | // System.out.println(intArray[i]); 44 | // } 45 | 46 | // System.out.println("Hellow World"); 47 | // byte[] byteArray; 48 | // short[] shortsArray; 49 | // boolean[] booleanArray; 50 | // long[] longArray; 51 | // float[] floatArray; 52 | // Float[] fl; 53 | 54 | // int intArray[]; 55 | // byte byteArray[]; 56 | // short shortsArray[]; 57 | // boolean booleanArray[]; 58 | // long longArray[]; 59 | // float floatArray[]; 60 | // double doubleArray[]; 61 | // char charArray[]; 62 | 63 | // int[] intArray = new int[10]; 64 | // byte[] byteArray = new byte[10]; 65 | // short[] shortsArray = new short[10]; 66 | // boolean[] booleanArray = new boolean[10]; 67 | // long[] longArray = new long[10]; 68 | // float[] floatArray = new float[10]; 69 | ArrayList al = new ArrayList<>(); 70 | al.add("shiv"); 71 | al.add("Coding"); 72 | al.add("Wallah"); 73 | al.add(0,"first"); 74 | System.out.println(al); 75 | } 76 | } 77 | 78 | -------------------------------------------------------------------------------- /LecturesCode/BinarySearchLecture.java: -------------------------------------------------------------------------------- 1 | class BinarySearchLecture { 2 | public static void main(String[] args) { 3 | int[] sarray = {10,20,30,55,65,87,98}; 4 | int[] descarray = {98,58,48,32,22,20,10}; 5 | int target = 22; 6 | int result = binarySearchI(sarray,target); 7 | System.out.println(result); 8 | result = binarySearchReverse(descarray,target); 9 | System.out.println(result); 10 | } 11 | 12 | 13 | public static int binarySearchI(int[] arr, int target) { 14 | int s=0,mid=0,e = arr.length-1; 15 | while(s<=e) { 16 | // Find Mid 17 | // mid = (s+e)/2; 18 | mid = s + (e-s)/2; 19 | // Element found at index mid 20 | if(arr[mid]==target) return mid; 21 | // Search in the right half 22 | else if(arr[mid]target) s=mid+1; 40 | // Search in the left half 41 | else e = mid-1; 42 | } 43 | // Element not found in the array 44 | return -1; 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /LecturesCode/ConditionalStatements.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | import javax.lang.model.util.ElementScanner14; 4 | 5 | public class ConditionalStatements { 6 | 7 | public static void main(String[] args) { 8 | // System.out.println("Hi 1"); 9 | int num =85; 10 | int num2 = 20; 11 | // Scanner scan = new Scanner(System.in); 12 | // String pay = scan.next(); 13 | // if(pay.equals("Success")) 14 | // System.out.println("Yes Registerd Successfuly !!"); 15 | // else 16 | // System.out.println("Failed to register.."); 17 | // if(num>90){ 18 | // System.out.println("A"); 19 | // } 20 | // else if(num>70 && num<80){ 21 | // System.out.println("C"); 22 | // } 23 | // else if(num>80){ 24 | // System.out.println("B"); 25 | // } 26 | // else if(num>50){ 27 | // System.out.println("D"); 28 | // } 29 | int marks =90; 30 | 31 | int mnum =2; 32 | // if(marks>=80) 33 | // switch(marks){ 34 | // // case "shivam30" : if(10>2) System.out.println("A"); break; 35 | // case 90 : switch(mnum){ 36 | // case 2 : System.out.println("B"); 37 | // } 38 | // } 39 | if(true){ 40 | // continue; 41 | System.out.println("hi"); 42 | // mnum = 20; 43 | } 44 | 45 | } 46 | } -------------------------------------------------------------------------------- /LecturesCode/DataTypesLecture.java: -------------------------------------------------------------------------------- 1 | class DataTypesLecture{ 2 | 3 | int myLocal = 2; 4 | // void my(){ 5 | // myLocal=3; 6 | // } 7 | 8 | public static void main(String abc[]) { 9 | int myLocal = 35; 10 | /* System.out.print("Hello G"); 11 | System.out.println("Hello G"); 12 | System.out.print("Hello G"); 13 | */ 14 | // int num1 = 1; 15 | // int num2 = 2; 16 | System.out.println("--------------------------------"); 17 | boolean result = true ; 18 | // System.out.println(num1); 19 | long num1 = 1934567899976543433L; 20 | //int num2=3.5; 21 | byte bt = 127; 22 | short sh = 12723; 23 | 24 | float f2 = 10.56f; 25 | double db = 100.67777; 26 | //type casting 27 | //up type casting 28 | double db1 = 10.45f; 29 | float f1 = 1; 30 | char ch= 'A'; 31 | char ch1= 'a'; 32 | char ch3= 'z'; 33 | //down type casting 34 | int num2 = (int) num1; 35 | int num3 = (int) ch; 36 | int num4 = (int) ch1; 37 | int num5 = (int) ch3; 38 | String st = "New String"; 39 | String st1 = " New String"; 40 | System.out.println(num3); 41 | System.out.println(num4); 42 | System.out.println(num5); 43 | System.out.print(ch+" "); 44 | System.out.println(st+st1); 45 | //myLocal=3; 46 | System.out.print(myLocal); 47 | // System.out.println(bt); 48 | // System.out.println(sh); 49 | // System.out.println(f1); 50 | // System.out.println(f2); 51 | // System.out.println(db); 52 | // System.out.println(db1); 53 | 54 | //ABcdEF = //ABCDEF 55 | System.out.println("\n--------------------------------"); 56 | } 57 | 58 | 59 | } -------------------------------------------------------------------------------- /LecturesCode/InputOutputLecture.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | import java.util.Scanner; 7 | 8 | import javax.xml.transform.Source; 9 | 10 | public class InputOutputLecture { 11 | public static void main(String[] args) { 12 | // Input- Command line arguments 13 | // System.out.println(args[1]); 14 | // Input- Using Scanner Class 15 | // Scanner scan = new Scanner(System.in); 16 | //int a = scan.nextInt(); 17 | // String st = scan.nextLine(); 18 | // System.out.println("Your entered value is " + st); 19 | // char ch = scan.next().charAt(1); 20 | // System.out.println("Your Enterd Value is "+ch); 21 | 22 | 23 | // Input- Using BufferedReader 24 | // BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 25 | // try { 26 | // String input = reader.readLine(); 27 | // System.out.println("Your input = "+input); 28 | // } catch (IOException e) { 29 | // // TODO Auto-generated catch block 30 | // e.printStackTrace(); 31 | // } 32 | 33 | 34 | //Output - print, println, printf, format, err 35 | // System.out.print("Shivam"); 36 | // System.out.println("Sharma"); 37 | // float n = 5.2222232f; 38 | // System.out.printf("%10.5f",n); 39 | 40 | //Date Format 41 | System.out.println(new Date()); 42 | SimpleDateFormat ft = new SimpleDateFormat("dd-MM-yyyy"); 43 | String str = ft.format(new Date()); 44 | System.out.println("Formatted Date : " + str); 45 | 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LecturesCode/LoopsLecture.java: -------------------------------------------------------------------------------- 1 | public class LoopsLecture { 2 | public static void main(String[] args) { 3 | int i=0, j =1,sum=0; 4 | while(i<=100){ 5 | //System.out.println("while = "+i); 6 | // // System.out.println((++count)+ " Sorry !!"); 7 | // // i+=2; 8 | if(i%2==0) 9 | sum+=i; 10 | i++; 11 | } 12 | System.out.println(sum); 13 | 14 | do{ 15 | System.out.println("do = "+j); 16 | j++; 17 | }while(j<1); 18 | 19 | //increasing 20 | for(i=1; i<=100; i++){ 21 | System.out.println(i); 22 | } 23 | 24 | //dcreasing 25 | for(i=5; i>=1; --i){ 26 | int arr[] = {1,2,3,4}; 27 | for(int val: arr) 28 | System.out.print(val); 29 | System.out.println(); 30 | } 31 | 32 | // break & continue 33 | for(int a=1;a<10;a++){ 34 | System.out.println(a); 35 | if(a==5) break; 36 | } 37 | int b=100; 38 | for(b=1;b<5;b++){ 39 | } 40 | System.out.println(b); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /LecturesCode/MethodsLecture.java: -------------------------------------------------------------------------------- 1 | public class MethodsLecture { 2 | public static void main(String[] args) { 3 | System.out.println("Hi"); 4 | recursion(5); 5 | // int i= getReturnType(); 6 | //System.out.println(sum(12,13)); 7 | // sum1(1,2); 8 | // println(); 9 | // MethodsLecture.println(); 10 | // MethodsLecture obj = new MethodsLecture(); 11 | // obj.println(); 12 | 13 | } 14 | 15 | // // Methods in Java 16 | // // Non Return Type 17 | // static void println(){ 18 | // System.out.println("Hi I am non return type"); 19 | // } 20 | // // Return Type 21 | // static Integer getReturnType(){ 22 | // return 12; 23 | // } 24 | // //Return Type with Parameter 25 | // static int sum(int num1, int num2){ 26 | // return num1+num2; 27 | // } 28 | // // Non Return Type with Parameter 29 | // static void sum(int x, int y){ 30 | // System.out.println(x+y); 31 | // } 32 | 33 | ////Method Overloading 34 | static void sum(int x, float y){ 35 | System.out.println(x+y); 36 | } 37 | 38 | static int sum(int num1, int num2){ 39 | return num1+num2; 40 | } 41 | 42 | static void sum(int num1, int num2, int num3, int num4){ 43 | //return num1+num2; 44 | } 45 | 46 | //Recursion 47 | static void recursion(int num){ 48 | 49 | if(num>0) 50 | recursion(num-1); 51 | 52 | System.out.println(num); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /LecturesCode/MostlyAskedBasicQuestions.java: -------------------------------------------------------------------------------- 1 | public class MostlyAskedBasicQuestions { 2 | // method to find factorial of given number using recursion 3 | // HW - using recursion 4 | static int factorial(int n) 5 | { 6 | int res = 1, i; 7 | for (i = 2; i <= n; i++) 8 | res *= i; 9 | return res; 10 | } 11 | 12 | //print fibonacci series | HW - using recursion 13 | static void fibonacci(int n){ 14 | int n1=0,n2=1,n3; 15 | System.out.print(n1+" "+n2);//printing 0 and 1 16 | for(int i=2;i 0) { 30 | rev_num = rev_num * 10 + num % 10; 31 | num = num / 10; 32 | } 33 | return rev_num; 34 | } 35 | 36 | //Count Even or Odd digits and Summ , Palindrome - similar 37 | 38 | //palindrome string 39 | static boolean palindromeString(String string){ 40 | String newstString = ""; 41 | 42 | for(int i = string.length()-1; i>=0; i--) 43 | { 44 | newstString +=string.charAt(i); 45 | } 46 | 47 | if(string.equals(newstString)) 48 | return true; 49 | return false; 50 | } 51 | 52 | 53 | static void swap(int num1, int num2){ 54 | // num1 = num1 + num2; 55 | // num2 = num1 - num2; 56 | // num1 = num1 - num2; 57 | int temp = num1; 58 | num1=num2; 59 | num2=temp; 60 | } 61 | // Driver method 62 | public static void main(String[] args) 63 | { 64 | int num = 5; 65 | // System.out.println(num+"! = "+factorial(0)); 66 | // fibonacci(5); 67 | // System.out.println(121==reverseDigits(121)); 68 | //System.out.println(palindromeString("aba")); 69 | 70 | // int x = 10; 71 | // int y = 20; 72 | System.out.println(Integer.MAX_VALUE); 73 | System.out.println(Integer.MIN_VALUE); 74 | // System.out.println("Before Swapping "+ x + y); 75 | // // int temp = x; 76 | // x= x+y; 77 | // y=x-y; 78 | // x=x-y; 79 | // //swap(x, y); 80 | // System.out.println("After Swapping "+ x + y); 81 | 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /LecturesCode/OperatorsLecture.java: -------------------------------------------------------------------------------- 1 | public class OperatorsLecture { 2 | public static void main(String[] args) { 3 | //CONCEPTS 4 | //Arithmetic Operators +, -, *, /, % 5 | //Unary Operators U++ , U-- , ++U, --U 6 | //Realtional Operators >, <, >=, <= , == != 7 | //Ternary Operator COndition ? True : False 8 | //Logical Operators && , || , ! 9 | //Bitwise Operators %, |, ! 10 | //Shifted Operators <<, >> , >>> 11 | //Short Hand Operators = += -= *= /= %= &= ^= |= <<= >>= >>>= 12 | 13 | 14 | //OUTPUT 15 | System.out.println("----------------------------------------------"); 16 | int a=2; 17 | int b=2; 18 | int c=1; 19 | // System.out.println(a+b); 20 | // System.out.println(a-b); 21 | // System.out.println(a*b); 22 | // System.out.println(a/b); 23 | // System.out.println(b%a); 24 | 25 | // System.out.println("pre = "+(--a)); 26 | // System.out.println("Post "+(b--)); 27 | // System.out.println(!true); 28 | 29 | 30 | // System.out.println(2>=3); 31 | // System.out.println(a<3); 32 | // System.out.println(a==b); 33 | // boolean bl = (1>3)? ((2>3)?true:false) : ((2<3)?true:false) ; 34 | // System.out.println(bl); 35 | 36 | // System.out.println(2<3 && (a++)<3); 37 | // System.out.println(a); 38 | // // System.out.println(2>3 || 2<3); 39 | 40 | // //Bit wise 41 | // System.out.println(2>3 & (b++)<3); 42 | // System.out.println(a); 43 | // //System.out.println(2>3 | 2<3); 44 | 45 | //Bitwise 46 | // System.out.println(~1); 47 | // System.out.println(~2); 48 | // System.out.println(~3); 49 | // System.out.println(~4); 50 | // System.out.println(1|2); 51 | System.out.println(2^3); 52 | System.out.println(10<<0); 53 | System.out.println(10<<1); 54 | System.out.println(10<<2); 55 | System.out.println(10<<3); 56 | System.out.println(10<<4); 57 | System.out.println(10>>1); 58 | System.out.println(10>>2); 59 | System.out.println(10>>3); 60 | //System.out.println(-10>>>5); 61 | System.out.println(-(10>>6)); 62 | 63 | // System.out.println(2|3); 64 | 65 | 66 | System.out.println("\n----------------------------------------------"); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /LecturesCode/OutputQuestions.java: -------------------------------------------------------------------------------- 1 | class OutputQuestions { 2 | public static void main(String args[]) 3 | { 4 | //1st question - 31, 32, 35, 32.0 5 | int g = 3; 6 | System.out.print(++g * 8); 7 | 8 | System.out.println("\n-------"); 9 | //2nd question - Nan , infinity, 0.0 , all 10 | double a, b,c; 11 | a = 3.0/0; 12 | b = 0/4.0; 13 | c=0/0.0; 14 | 15 | System.out.println(a); 16 | System.out.println(b); 17 | System.out.println(c); 18 | 19 | System.out.println("\n-------"); 20 | //3rd Question - 5 6 6 5, 5 6 5, Compile Error, Runtime Error 21 | int x; 22 | x = 5; 23 | { 24 | int y = 6; 25 | System.out.print(x + " " + y); 26 | } 27 | // System.out.println(x + " " + y); 28 | 29 | System.out.println("\n-------"); 30 | //4th Question - Guess Output ? 31 | byte by = 50; 32 | //by = by * 50; 33 | System.out.println(by); 34 | 35 | System.out.println("\n-------"); 36 | //5th Question 37 | int p,q,r,s; 38 | p=q=r=s=20; 39 | p+=q-=r*=s/=20; 40 | System.out.println(p+" "+q+" "+r+" "+s); 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /LecturesCode/PatternPrograms.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PatternPrograms { 4 | public static void main(String[] args) { 5 | int size=5; 6 | //pattern1(size); 7 | // pattern2(size); 8 | //pattern3(size); 9 | // pattern4(size); 10 | // pattern5(size); 11 | pattern6(size); 12 | //HourGlass(); 13 | // diamond(); 14 | // Full_Triangle(); 15 | 16 | } 17 | 18 | public static void pattern1(int n){ 19 | 20 | for(int row=1;row n ? 2 * n - row: row; 105 | for (int col = 1; col = 1; i--) 212 | { 213 | //Printing the spaces 214 | for (int j = 1; j < i; j++) 215 | System.out.print(" "); 216 | 217 | //printing star or numbers 218 | for (int j = i; j <= rows; j++) 219 | { 220 | //System.out.print(j+" "); 221 | System.out.print("* "); 222 | } 223 | System.out.println(); 224 | } 225 | } 226 | 227 | public static void diamond() { 228 | Scanner scanner = new Scanner(System.in); 229 | System.out.print("Enter the number of rows (odd number): "); 230 | int rows = scanner.nextInt(); 231 | 232 | int spaces = rows / 2; 233 | int stars = 1; 234 | 235 | // Upper half of the diamond 236 | for (int i = 1; i <= rows; i++) { 237 | 238 | //print spaces 239 | for (int j = 1; j <= spaces; j++) { 240 | System.out.print(" "); 241 | } 242 | 243 | //print stars 244 | for (int j = 1; j <= stars; j++) { 245 | //System.out.print("*"); 246 | System.out.print(j); 247 | } 248 | 249 | System.out.println(); 250 | 251 | if (i <= rows / 2) { 252 | spaces--; 253 | stars += 2; 254 | } else { 255 | spaces++; 256 | stars -= 2; 257 | } 258 | } 259 | } 260 | 261 | 262 | public static void Full_Triangle() { 263 | Scanner scanner = new Scanner(System.in); 264 | System.out.print("Enter the height of the triangle: "); 265 | int height = scanner.nextInt(); 266 | scanner.nextLine(); // Consume the newline character 267 | 268 | System.out.print("Enter the character: "); 269 | char ch = scanner.nextLine().charAt(0); 270 | 271 | for (int row = 1; row <= height; row++) { 272 | 273 | //print the spaces 274 | for (int col = 1; col <= height - row; col++) { 275 | System.out.print(" "); 276 | } 277 | 278 | //print character 279 | for (int col = 1; col <= row; col++) { 280 | System.out.print(ch + " "); 281 | } 282 | 283 | System.out.println(); 284 | } 285 | } 286 | 287 | } 288 | 289 | -------------------------------------------------------------------------------- /LecturesCode/RecursionLecture.java: -------------------------------------------------------------------------------- 1 | public class RecursionLecture { 2 | static int num=2; 3 | public static void recursion(){ 4 | if(num>0){ 5 | num--; 6 | recursion(); 7 | 8 | } 9 | System.out.println("100"); 10 | 11 | } 12 | 13 | // public static void print5(){ 14 | // System.out.println(5); 15 | // print4(); 16 | // } 17 | // public static void print4(){ 18 | // System.out.println(4); 19 | // print3(); 20 | // } 21 | // public static void print3(){ 22 | // System.out.println(3); 23 | // print2(); 24 | // } 25 | // public static void print2(){ 26 | // System.out.println(2); 27 | // print1(); 28 | // } 29 | // public static void print1(){ 30 | // System.out.println(1); 31 | // } 32 | public static void main(String[] args) { 33 | //recursion(); 34 | //print5(); 35 | } 36 | public static class Practice{ 37 | 38 | static void myRFun1(int num){ 39 | if(num== 0) return; 40 | System.out.println(num); 41 | myRFun1(num-1); 42 | 43 | } 44 | 45 | static void myRFun2(int num){ 46 | if(num== 0) return; 47 | 48 | myRFun2(num-1); 49 | System.out.println(num); 50 | myRFun2(num-1); 51 | } 52 | 53 | static void myRFun3(int num){ 54 | if(num== 0) return; 55 | System.out.println(num); 56 | myRFun3(num-1); 57 | System.out.println(num); 58 | } 59 | 60 | public static void main(String[] args) { 61 | myRFun3(3); 62 | } 63 | 64 | } 65 | public static class DirectRecursion { 66 | public static void countdown(int n) { 67 | if (n == 0) { 68 | System.out.println("Blastoff!"); 69 | } else { 70 | System.out.println(n); 71 | countdown(n - 1); 72 | } 73 | } 74 | 75 | public static void main(String[] args) { 76 | countdown(5); 77 | } 78 | } 79 | 80 | public static class IndirectRecursion { 81 | public static void fun1() { 82 | System.out.println("fun1"); 83 | fun2(); 84 | System.out.println("After fun2"); 85 | } 86 | 87 | public static void fun2() { 88 | System.out.println("fun2"); 89 | // fun1(); 90 | // System.out.println("After fun1"); 91 | } 92 | 93 | 94 | public static void main(String[] args) { 95 | fun1(); 96 | } 97 | } 98 | 99 | public static class TreeRecursion { 100 | public static int nthFibonacci(int n) { 101 | if (n <= 1) { 102 | return n; 103 | } else { 104 | return nthFibonacci(n - 1) + nthFibonacci(n - 2); 105 | } 106 | } 107 | 108 | public static void main(String[] args) { 109 | int result = nthFibonacci(4); 110 | System.out.println(result); 111 | } 112 | } 113 | 114 | public static class NestedRecursion { 115 | public static int nestedSum(int n) { 116 | if (n == 0) { 117 | return 0; 118 | } else { 119 | return n + nestedSum(nestedSum(n - 1)); 120 | } 121 | } 122 | 123 | public static void main(String[] args) { 124 | int result = nestedSum(2); 125 | System.out.println(result); 126 | } 127 | } 128 | 129 | public static class MutualRecursion { 130 | public static boolean isEven(int n) { 131 | if (n == 0) { 132 | return true; 133 | } else { 134 | return isOdd(n - 1); 135 | } 136 | } 137 | 138 | public static boolean isOdd(int n) { 139 | if (n == 0) { 140 | return false; 141 | } else { 142 | return isEven(n - 1); 143 | } 144 | } 145 | 146 | public static void main(String[] args) { 147 | System.out.println(isEven(6)); 148 | } 149 | 150 | //ODD EVEN Direct Solution 151 | public static class OddEven { 152 | public static boolean isEven(int n) { 153 | if (n == 0) { 154 | return true; // Base case: 0 is an even number 155 | } else if (n == 1) { 156 | return false; // Base case: 1 is an odd number 157 | } else { 158 | return isEven(n - 2); // Recursive call with n-2 159 | } 160 | } 161 | 162 | public static void main(String[] args) { 163 | int number = 6; 164 | if (isEven(number)) { 165 | System.out.println(number + " is an even number."); 166 | } else { 167 | System.out.println(number + " is an odd number."); 168 | } 169 | } 170 | } 171 | 172 | } 173 | 174 | 175 | } 176 | 177 | -------------------------------------------------------------------------------- /LecturesCode/RuffClass.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class RuffClass { 5 | // public static void main(String[] args) { 6 | // // System.out.println("Coding Wallah Community"); 7 | 8 | // // Long lg = 12395654998765655L; 9 | // // System.out.println(lg.SIZE); 10 | // // System.out.println(lg.MAX_VALUE); 11 | // // System.out.println(lg.MIN_VALUE); 12 | // String a= "coding wallah"; 13 | // String b= "coding wallah"; 14 | // System.out.println("a==b :"); 15 | // System.out.println("a==b :" + a==b); 16 | 17 | // } 18 | 19 | public static int nestedSum(int n) { 20 | if (n <= 0) { 21 | return 0; 22 | } else { 23 | return n + nestedSum(nestedSum(n - 1)); 24 | } 25 | } 26 | //Recursion 27 | // static void recursion(int num){ 28 | 29 | // if(num>0) 30 | // recursion(num-1); 31 | 32 | // System.out.println(num); 33 | // } 34 | 35 | //Recursion 36 | static void recursion(int num){ 37 | if(num< 0) return; 38 | 39 | recursion(num-1); 40 | System.out.println(num); 41 | } 42 | 43 | // public static int peakIndexInMountainArrayBinary(int[] arr) { 44 | // int s=0, e=arr.length-1; 45 | // while(sarr[mid+1]) e = mid; 48 | // else s=mid; 49 | // } 50 | // return s; 51 | // } 52 | 53 | // public static void main(String[] args) { 54 | // //int result = nestedSum(2); 55 | // //System.out.println(result); 56 | // int arr[] = {0,1,2,5,6,7,4,3}; 57 | // // System.out.println("pk :"+peakIndexInMountainArrayBinary(arr)); 58 | // Scanner sc = new Scanner(System.in); 59 | // String name = sc.next(); 60 | // System.out.println("Name :"+name); 61 | // sc.nextLine(); 62 | // String sname = sc.nextLine(); 63 | // System.out.println("Sname :"+sname); 64 | // // recursion(6); 65 | // } 66 | // public static void main(String arr){ 67 | 68 | // } 69 | 70 | 71 | public static void main(String[] args) { 72 | int arr[] = {50,60,30,20,90,50}; 73 | 74 | //QuickSort(arr,0,arr.length-1); 75 | //System.out.println(Arrays.toString(arr)); 76 | } 77 | 78 | private static void QuickSort(int[] arr, int s, int e) { 79 | if(spivot); 96 | // if(i>=j) return j; 97 | // swap(arr,i,j); 98 | // } 99 | 100 | boolean done = false; 101 | while (!done) { 102 | while (left <= right && arr[left] <= pivot) { 103 | left = left + 1; 104 | } 105 | while (arr[right] >= pivot && right >= left) { 106 | right = right - 1; 107 | } 108 | if (right < left) { 109 | done = true; 110 | } else { 111 | // Swap arr[left] and arr[right] 112 | int temp = arr[left]; 113 | arr[left] = arr[right]; 114 | arr[right] = temp; 115 | } 116 | } 117 | 118 | int temp = arr[s]; 119 | arr[s] = arr[right]; 120 | arr[right] = temp; 121 | return right; 122 | } 123 | 124 | private static int Lpartion(int[] arr, int s, int e) { 125 | int pivot = arr[e]; 126 | int i = s-1; 127 | for(int j=s;j<=e-1;j++) { 128 | if(arr[j]=arr.length) return -1; 29 | // Base case: If the target is found at the current index, return the index 30 | if(arr[index]==target) return index; 31 | // Recursive case: Search the next element in the array 32 | return linearSearchR(arr, target, index+1); 33 | 34 | } 35 | 36 | public static int binarySearchI(int[] arr, int target) { 37 | int s=0,mid=0,e = arr.length-1; 38 | while(s<=e) { 39 | // Find Mid 40 | // mid = (s+e)/2; 41 | mid = s + (e-s)/2; 42 | // Element found at index mid 43 | if(arr[mid]==target) return mid; 44 | // Search in the right half 45 | else if(arr[mid]target) s=mid+1; 63 | // Search in the left half 64 | else e = mid-1; 65 | } 66 | // Element not found in the array 67 | return -1; 68 | } 69 | 70 | 71 | public static int binarySearchR(int[] arr, int target, int start, int end) { 72 | if(start<=end) { 73 | // Find Mid 74 | int mid = start+ (end-start)/2; 75 | // Element found at index mid 76 | if(arr[mid]==target) return mid; 77 | // Search in the right half 78 | else if(arr[mid] 11 | 12 | ## Based on 📍Slow & Fast Pointer Questions ❤️ 13 | 14 | | LinkedList Questions | Practice Link | Code Solutions | Video Solutions | HINT 15 | -|-|-|-|- 16 | Detect Cycle/Loop in LinkedList| [Leetcode](https://leetcode.com/problems/linked-list-cycle) | 17 | Find Mid of LinkedList | [Leetcode](https://leetcode.com/problems/middle-of-the-linked-list) 18 | Intersection of 2 LinkedList | [Leetcode](https://leetcode.com/problems/intersection-of-two-linked-lists) 19 | Check LinkedList is Palindrome | [Leetcode](https://leetcode.com/problems/palindrome-linked-list) 20 | Remmove Kth Node from End of LinkedList | [Leetcode](https://leetcode.com/problems/remove-nth-node-from-end-of-list) 21 | 22 | ## Based on Swappings 🔁 23 | - Swap Nodes in Pairs | [Leetcode](https://leetcode.com/problems/swap-nodes-in-pairs/description/) 24 | - Swapping Nodes in Linkedlist | 25 | [Leetcode](https://leetcode.com/problems/swapping-nodes-in-a-linked-list) 26 | - Odd Even Linked List | [Leetcode](https://leetcode.com/problems/odd-even-linked-list/description/) 27 | 28 | ## Medium Questions ❤️‍🔥 29 | | LinkedList Questions | Practice Link | Code Solutions | Video Solutions | HINT 30 | | ------------- | :-------------: | :-------------: | :---------: |:---------: | 31 | Delete Given Node | [Leetcode](https://leetcode.com/problems/delete-node-in-a-linked-list/description/) 32 | Rotate Linked List | [Leetcode](https://leetcode.com/problems/rotate-list/description/) 33 | Delete Middle Node | [Leetcode](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list) 34 | 35 | 36 | ## Hard Questions 💔 37 | | Array Questions | Practice Link | Code Solutions | Video Solutions | HINT 38 | -|-|-|-|- 39 | Coming Soon.. 40 | --- 41 | 42 |
43 | 44 | 45 | 46 | 47 | 48 |
49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 |
59 | 60 | 69 | -------------------------------------------------------------------------------- /PatternQuestions.md: -------------------------------------------------------------------------------- 1 | ## All Pattern Questions - ⭐ Star | 🔢 Number | 🔠 Alphabet 2 | 3 | ### Ninja Steps to Make Any Pattern 😅 4 | 1) Outer Loop => Responsible for Rows | No. of lines printing Horizontally. 5 | 2) Inner Loop => Responsible for Colms | No. of Elements printing Vertically. 6 | 3) Check the Value which u need to print - ⭐ Star, 🔢 Number, 🔠 Alphabet. 7 | 4) Check is any relation btw Rows & Colms. 8 | 5) Check which order is following in the pattern **( 🔢 Number | 🔠 Alphabet )** 9 | 6) Think 🤔, Is this pattern can be formed by mixing other patterns. **(only when you have failed in 4th & 5th step )** 10 | 11 | #### SQUARE Patterns 12 | ```text 13 | 1.1 * * * * * 14 | * * * * * 15 | * * * * * 16 | * * * * * 17 | * * * * * 18 | 19 | 1.2 1 2 3 4 5 20 | 1 2 3 4 5 21 | 1 2 3 4 5 22 | 1 2 3 4 5 23 | 1 2 3 4 5 24 | 25 | 1.3 A B C D E 26 | A B C D E 27 | A B C D E 28 | A B C D E 29 | 30 | ``` 31 | 32 | #### Right Trianlge 33 | ```text 34 | 2.1 * 35 | * * 36 | * * * 37 | * * * * 38 | * * * * * 39 | 40 | 2.2 1 41 | 1 2 42 | 1 2 3 43 | 1 2 3 4 44 | 1 2 3 4 5 45 | 46 | 2.3 1 47 | 2 3 48 | 4 5 6 49 | 7 8 9 10 50 | 11 12 13 14 15 51 | 52 | 2.4 A 53 | B C 54 | D E F 55 | G H I J 56 | k L N N O 57 | 58 | 2.5 E 59 | D E 60 | C D E 61 | B C D E 62 | A B C D E 63 | 64 | 2.6 1 65 | 0 1 66 | 1 0 1 67 | 0 1 0 1 68 | 1 0 1 0 1 69 | ``` 70 | 71 | #### Reverse Right Trianlge 72 | ```text 73 | 74 | 3.1 * * * * * 75 | * * * * 76 | * * * 77 | * * 78 | * 79 | 80 | 3.2 1 2 3 4 5 81 | 1 2 3 4 82 | 1 2 3 83 | 1 2 84 | 1 85 | 86 | 3.3 1 1 1 1 1 1 87 | 2 2 2 2 2 88 | 3 3 3 3 89 | 4 4 4 90 | 5 5 91 | 6 92 | 93 | 3.4 E D C B A 94 | D C B A 95 | C B A 96 | B A 97 | A 98 | 99 | ``` 100 | #### Trianlge 101 | ```text 102 | 103 | 4.1 * 104 | * * 105 | * * * 106 | * * * * 107 | * * * * * 108 | * * * * 109 | * * * 110 | * * 111 | * 112 | 113 | 4.2 1 114 | 1 2 115 | 1 2 3 116 | 1 2 3 4 117 | 1 2 3 4 5 118 | 1 2 3 4 119 | 1 2 3 120 | 1 2 121 | 1 122 | 123 | ``` 124 | #### Right Triangle Mirror Image | Space + ⭐ |🔠 | 🔢 125 | ```text 126 | 127 | 128 | 5.1 * 129 | * * 130 | * * * 131 | * * * * 132 | * * * * * 133 | 134 | 5.2 * 135 | * * 136 | * * * 137 | * * * * 138 | * * * * * 139 | 140 | 141 | 5.3 1 142 | 2 1 2 143 | 3 2 1 2 3 144 | 4 3 2 1 2 3 4 145 | 5 4 3 2 1 2 3 4 5 146 | 147 | 5.4 1 148 | 1 1 149 | 1 2 1 150 | 1 3 3 1 151 | 1 4 6 4 1 152 | 153 | ``` 154 | #### Reverse Right Triangle Mirror Image | Space + ⭐ |🔠 | 🔢 155 | ```text 156 | 157 | 6.1 * * * * * 158 | * * * * 159 | * * * 160 | * * 161 | * 162 | 163 | 6.2 * * * * * 164 | * * * * 165 | * * * 166 | * * 167 | * 168 | 169 | 6.3 1 2 3 4 5 170 | 1 2 3 4 171 | 1 2 3 172 | 1 2 173 | 1 174 | 175 | ``` 176 | #### Hour Glass -⭐ |🔠 | 🔢 177 | ```text 178 | 179 | 1. * * * * * 180 | * * * * 181 | * * * 182 | * * 183 | * 184 | * 185 | * * 186 | * * * 187 | * * * * 188 | * * * * * 189 | 190 | 191 | ``` 192 | #### Other ⭐ Patterns 193 | ```text 194 | 195 | 1. **** 196 | * * 197 | * * 198 | * * 199 | **** 200 | 201 | 2. * 202 | * * 203 | * * 204 | * * 205 | ********* 206 | 207 | 208 | 3. ********* 209 | * * 210 | * * 211 | * * 212 | * 213 | 214 | 4. ***** 215 | * * 216 | * * 217 | * * 218 | ***** 219 | 220 | 5. * 221 | * * 222 | * * 223 | * * 224 | * * 225 | * * 226 | * * 227 | * * 228 | * 229 | 230 | ``` 231 | #### Important Patterns - CHALLENGE 232 | ```text 233 | 234 | 1. 1 235 | 212 236 | 32123 237 | 4321234 238 | 32123 239 | 212 240 | 1 241 | 242 | 243 | 2. 1 1 244 | 1 2 2 1 245 | 1 2 3 3 2 1 246 | 1 2 3 4 4 3 2 1 247 | 248 | 3. 4 4 4 4 4 4 4 249 | 4 3 3 3 3 3 4 250 | 4 3 2 2 2 3 4 251 | 4 3 2 1 2 3 4 252 | 4 3 2 2 2 3 4 253 | 4 3 3 3 3 3 4 254 | 4 4 4 4 4 4 4 255 | 256 | 257 | 4. ********** 258 | **** **** 259 | *** *** 260 | ** ** 261 | * * 262 | * * 263 | ** ** 264 | *** *** 265 | **** **** 266 | ********** 267 | 268 | 269 | 5. * * 270 | ** ** 271 | *** *** 272 | **** **** 273 | ********** 274 | **** **** 275 | *** *** 276 | ** ** 277 | * * 278 | 279 | 280 | 6. * * 281 | ** ** 282 | * * * * 283 | * * * * 284 | * ** * 285 | * ** * 286 | * * * * 287 | * * * * 288 | ** ** 289 | * * 290 | 291 | 292 | 7. * 293 | * * 294 | * * * 295 | * * * * 296 | * * * * * 297 | * * * * 298 | * * * 299 | * * 300 | * 301 | 302 | 8. * 303 | * * * 304 | * * * * * 305 | * * * * * * * 306 | * * * * * * * * * 307 | ``` 308 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #
 JAVA-DSA-Vector-Course 🔥 
2 | 3 | | Topics | Video Link | Questions | 4 | |:---:|:---:|:---:| 5 | Patterns | [Video](https://www.youtube.com/live/-Pq1XjcEgw8?feature=share) | [Questions](https://github.com/CodingWallah/JAVA-DSA-VectorCourse/blob/main/PatternQuestions.md) 6 | Arrays | [Video](https://www.youtube.com/live/tjkXeEN8yFc?feature=share) | [Questions](https://github.com/CodingWallah/JAVA-DSA-VectorCourse/blob/main/ArrayQuestions.md) 7 | Binary Search | [Video](https://youtu.be/qvXZ_e71B2s?si=je3CtmeFsCMoXl_U) | [Questions](https://github.com/CodingWallah/JAVA-DSA-VectorCourse/blob/main/BinarySearch.md) 8 | LinkedList | [Video](https://youtu.be/o3bgJAkKTdU?si=aau7DOsBKeryBZGs) | [Questions](https://github.com/CodingWallah/JAVA-DSA-VectorCourse/blob/main/LinkedList.md) 9 | 10 | - 🧑🏼‍💻 Do you want to master data structures and algorithms in Java and ace your coding interviews ❓ 11 | - 👨🏼‍🏫 Do you want to learn from experienced instructor with live coding sessions and help you solve challenging problems❓ 12 | - ❤️ Do you want to join a community of learners who will support you and Motivate you along the way ❓ 13 | 14 | ###
 If your ans is YES to any of above questions, 👉🏼 then JAVA DSA Vector Course for you ❤️ 
15 | 16 | ### But that’s not all! JAVA DSA Vector Course is not just a collection of videos and exercises. It is a live and interactive learning experience that will keep you engaged and motivated throughout the course. 17 | 18 | 👋🏼 By the end of this course, you will have a solid foundation of data structures and algorithms in Java. You will be able to confidently write code that is clear, concise, and efficient. You will be able to solve complex problems that require logical thinking and creativity. You will be ready to ace your coding interviews and land your dream job as a software engineer. 19 | 20 | 21 | ###
So what are you waiting for❓Join this Revolutionary Journey Now,I am really waiting for you Inside ❤️
22 | 23 | --------------------------------------------------------------------------------