├── DSA-revision-guide-2022.pdf ├── DS_01_Time Complexiety └── timeComplexiety.txt ├── DS_02_Pattern_Questions ├── Merged_Patterns │ └── Pattern1.java ├── Pattern_With_Spaces │ └── Pattern.java ├── Pattern_with_Alphabets │ └── Pattern.java ├── Patterns_With_Numbers │ └── Pattern.java ├── README.md └── Star_Patterns │ └── Pattern.java ├── DS_03_Number_System ├── Bit_Manipulation │ ├── Part_1 │ │ ├── BM01_Binary_to_Decimal.java │ │ ├── BM02_Decimal_to_Binary.java │ │ ├── BM03_Not_Operator.java │ │ ├── BM04_Shifting_Operator.java │ │ ├── BM05_Swap_Numbers.java │ │ └── BM06_Find_EvenOdd.java │ ├── Part_2 │ │ ├── BM01_FindBit.java │ │ ├── BM02_SetBit.java │ │ ├── BM03_ResetBit.java │ │ ├── BM04_ClearBit.java │ │ ├── BM05_RightmostSetBit.java │ │ ├── BM06_Number_of_SetBit.java │ │ └── BM07_ChangeNumber.java │ ├── README.md │ ├── TestQuestion │ │ ├── BM01_DigitsInNumber.java │ │ ├── BM02_FindXOR.java │ │ ├── BM03_FindXORinRange.java │ │ ├── BM04_FindUniqueElement.java │ │ ├── BM05_FindTwoUnique.java │ │ ├── BM06_FindUniqueWithThreeElements.java │ │ ├── BM07_Nth_MagicNumber.java │ │ ├── BM08_PowerOfTwo.java │ │ ├── BM09_Power.java │ │ ├── BM10_RotateBitsInBinary.java │ │ ├── BM11_TrailingZerosInBinary.java │ │ ├── BM12_FlipImage.java │ │ ├── BM13_FindDuplicate.java │ │ ├── BM14_ReverseBitsinBinary.java │ │ └── BM15_ComplimentOfNumber.java │ └── bitManipulation.txt └── Math_for_DSA │ ├── Part_1 │ ├── MD01_ReverseNumber.java │ ├── MD02_PalindromeNumber.java │ ├── MD03_ArmstrongNumber.java │ ├── MD04_PerfectNumber.java │ ├── MD05_HappyNumber.java │ ├── MD06_FibonacciNumber.java │ ├── MD07_CatalanNumber.java │ └── MD08_SmithNumber.java │ ├── Part_2 │ ├── MD01_Factorial.java │ ├── MD02_TrailingZerosInFactorial.java │ ├── MD03_FastPower.java │ ├── MD04_SquareRoot.java │ ├── MD05_FactorsOfNumber.java │ ├── MD06_EuclidGCD.java │ ├── MD07_PrimeNumber.java │ ├── MD08_SieveOfEratosthenes.java │ ├── MD09_ModuloArithematics.java │ └── part2.txt │ └── README.md ├── DS_04_Array_and_Strings └── Arrays_and_Questions │ ├── MajorityElement.java │ ├── Max_Till_i.java │ ├── MaximunSumSubArray.java │ ├── MultiDimensionalArray │ └── SpiralPrint.java │ ├── RainwaterTrapping.java │ ├── StockBuyAndSell1.java │ ├── StockBuyAndSell2.java │ ├── SumOfAllSubArrays.java │ └── array.txt ├── DS_05_Binary_Search └── binarySearch.txt ├── DS_06_Sorting_Algorithm └── sorting.txt ├── DS_07_Recursion_And_Backtracking └── recursion.txt ├── DS_08_LinkedList_and_Questions └── linkedList.txt ├── DS_09_Stack_and_Queue_with_Question └── stackAndQueue.txt ├── DS_10_HashMap_and_Questions └── HashMap.txt ├── DS_11_Tree_and_Questions └── trees.txt ├── DS_12_Heap_and_Questions └── heaps.txt ├── DS_13_Graphs_and_Questions └── graphs.txt ├── DS_14_Trie_and_Questions └── tries.txt ├── DS_15_Sliding_Window_Questions └── slidingWindow.txt ├── DS_16_Dyanmic_Programming └── dynamicProgramming.txt ├── DS_17_Greedy_Algorithms └── Greedy.txt └── README.md /DSA-revision-guide-2022.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DSA-revision-guide-2022.pdf -------------------------------------------------------------------------------- /DS_01_Time Complexiety/timeComplexiety.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_01_Time Complexiety/timeComplexiety.txt -------------------------------------------------------------------------------- /DS_02_Pattern_Questions/Merged_Patterns/Pattern1.java: -------------------------------------------------------------------------------- 1 | package DS_02_Pattern_Questions.Merged_Patterns; 2 | 3 | public class Pattern1 { 4 | public static void main(String[] args) { 5 | pattern5(5); 6 | } 7 | 8 | // pattern Output 9 | // * 10 | // ** 11 | // *** 12 | // **** 13 | // ***** 14 | // ****** 15 | // ***** 16 | // **** 17 | // *** 18 | // ** 19 | // * 20 | 21 | // method 1 22 | static void pattern1(int n) { 23 | for (int i = 0; i < n; i++) { 24 | for (int j = 0; j < i; j++) { 25 | System.out.print("*"); 26 | } 27 | System.out.println(); 28 | } 29 | for (int i = 0; i < n; i++) { 30 | for (int j = i; j < n; j++) { 31 | System.out.print("*"); 32 | } 33 | System.out.println(); 34 | } 35 | } 36 | 37 | // method 2 38 | static void pattern1_1(int n) { 39 | for (int i = 0; i < 2 * n + 1; i++) { 40 | int colInRow = (i > n) ? 2 * n - i : i; 41 | for (int j = 0; j <= colInRow; j++) { 42 | System.out.print('*'); 43 | } 44 | System.out.println(); 45 | } 46 | } 47 | 48 | static void pattern2(int n){ 49 | for (int i = 0; i < n*2+1; i++) { 50 | int colInRow = (i > n) ? 2 * n - i : i; 51 | for (int j = colInRow; j < n; j++) { 52 | System.out.print(" "); 53 | } 54 | for (int j = 0; j <= colInRow; j++) { 55 | System.out.print("*"); 56 | } 57 | System.out.println(); 58 | } 59 | } 60 | // pattern Output 61 | // * 62 | // ** 63 | // *** 64 | // **** 65 | // ***** 66 | // ****** 67 | // ***** 68 | // **** 69 | // *** 70 | // ** 71 | // * 72 | static void pattern3(int n){ 73 | for (int i = 0; i < n*2+1; i++) { 74 | int colInRow = (i > n) ? 2 * n - i : i; 75 | for (int j = colInRow; j < n; j++) { 76 | System.out.print(" "); 77 | } 78 | for (int j = 0; j <= colInRow; j++) { 79 | System.out.print("* "); 80 | } 81 | System.out.println(); 82 | } 83 | } 84 | // Pattern output 85 | // * 86 | // * * 87 | // * * * 88 | // * * * * 89 | // * * * * * 90 | // * * * * * * 91 | // * * * * * 92 | // * * * * 93 | // * * * 94 | // * * 95 | // * 96 | 97 | static void pattern4(int n){ 98 | for (int i = 0; i < n*2+1; i++) { 99 | int colInRow = (i > n) ? 2 * n - i : i; 100 | for (int j = 0; j <= colInRow; j++) { 101 | System.out.print('*'); 102 | } 103 | for (int j = colInRow; j < n; j++) { 104 | System.out.print(" "); 105 | } 106 | for (int j = colInRow; j < n; j++) { 107 | System.out.print(" "); 108 | } 109 | for (int j = 0; j <= colInRow; j++) { 110 | System.out.print("*"); 111 | } 112 | System.out.println(); 113 | } 114 | } 115 | // pattern output 116 | // * * 117 | // ** ** 118 | // *** *** 119 | // **** **** 120 | // ***** ***** 121 | // ************ 122 | // ***** ***** 123 | // **** **** 124 | // *** *** 125 | // ** ** 126 | // * * 127 | 128 | static void pattern5(int n){ 129 | for (int i = 0; i < n*2+1; i++) { 130 | int colInRow = (i > n) ? 2 * n - i : i; 131 | for (int j = colInRow; j <= n; j++) { 132 | System.out.print("*"); 133 | } 134 | for (int j = 0; j < colInRow; j++) { 135 | System.out.print(" "); 136 | } 137 | for (int j = 0; j < colInRow; j++) { 138 | System.out.print(" "); 139 | } 140 | for (int j = colInRow; j <= n; j++) { 141 | System.out.print("*"); 142 | } 143 | System.out.println(); 144 | } 145 | } 146 | // pattern5 output 147 | // ************ 148 | // ***** ***** 149 | // **** **** 150 | // *** *** 151 | // ** ** 152 | // * * 153 | // ** ** 154 | // *** *** 155 | // **** **** 156 | // ***** ***** 157 | // ************ 158 | 159 | } 160 | -------------------------------------------------------------------------------- /DS_02_Pattern_Questions/Pattern_With_Spaces/Pattern.java: -------------------------------------------------------------------------------- 1 | package DS_02_Pattern_Questions.Pattern_With_Spaces; 2 | 3 | public class Pattern { 4 | public static void main(String[] args) { 5 | pattern5(5); 6 | } 7 | 8 | static void pattern1(int n) { 9 | for (int i = 0; i < n; i++) { 10 | for (int j = i + 1; j < n; j++) { 11 | System.out.print(" "); 12 | } 13 | for (int j = 0; j <= i; j++) { 14 | System.out.print("*"); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | // pattern1 output 20 | // * 21 | // ** 22 | // *** 23 | // **** 24 | // ***** 25 | 26 | static void pattern2(int n){ 27 | for (int i = 0; i < n; i++) { 28 | for (int j = 0; j < i; j++) { 29 | System.out.print(" "); 30 | } 31 | for (int j = i; j < n; j++) { 32 | System.out.print("*"); 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | // pattern2 output 38 | // ***** 39 | // **** 40 | // *** 41 | // ** 42 | // * 43 | 44 | static void pattern4(int n){ 45 | for (int i = 0; i < n; i++) { 46 | for (int j = i+1; j < n; j++) { 47 | System.out.print(" "); 48 | } 49 | for (int j = 0; j <= 2*i; j++) { 50 | System.out.print("*"); 51 | } 52 | System.out.println(); 53 | } 54 | } 55 | // pattern output 56 | // * 57 | // *** 58 | // ***** 59 | // ******* 60 | // ********* 61 | 62 | static void pattern5(int n){ 63 | for (int i = 0; i < n; i++) { 64 | for (int j = 0; j < i; j++) { 65 | System.out.print(" "); 66 | } 67 | for (int j = i*2+1; j <= n*2-1; j++) { 68 | System.out.print("*"); 69 | } 70 | System.out.println(); 71 | } 72 | } 73 | // pattern output 74 | // ********* 75 | // ******* 76 | // ***** 77 | // *** 78 | // * 79 | 80 | static void pattern6(int n){ 81 | for (int i = 0; i < n; i++) { 82 | for (int j = i + 1; j < n; j++) { 83 | System.out.print(" "); 84 | } 85 | for (int j = 0; j <= i; j++) { 86 | System.out.print("* "); 87 | } 88 | System.out.println(); 89 | } 90 | } 91 | // pattern output 92 | // * 93 | // * * 94 | // * * * 95 | // * * * * 96 | // * * * * * 97 | 98 | static void pattern7(int n){ 99 | for (int i = 0; i < n; i++) { 100 | for (int j = 0; j < i; j++) { 101 | System.out.print(" "); 102 | } 103 | for (int j = i; j < n; j++) { 104 | System.out.print("* "); 105 | } 106 | System.out.println(); 107 | } 108 | } 109 | // pattern output 110 | // * * * * * 111 | // * * * * 112 | // * * * 113 | // * * 114 | // * 115 | } 116 | -------------------------------------------------------------------------------- /DS_02_Pattern_Questions/Pattern_with_Alphabets/Pattern.java: -------------------------------------------------------------------------------- 1 | package DS_02_Pattern_Questions.Pattern_with_Alphabets; 2 | 3 | public class Pattern { 4 | public static void main(String[] args) { 5 | // System.out.println(); 6 | pattern2(5); 7 | } 8 | 9 | static void pattern1(int n) { 10 | for (int i = 0; i < n; i++) { 11 | for (int j = 0; j <= i; j++) { 12 | System.out.print((char) ('A' + n - i + j - 1) + " "); 13 | } 14 | System.out.println(); 15 | } 16 | } 17 | // pattern1 output 18 | // E 19 | // D E 20 | // C D E 21 | // B C D E 22 | // A B C D E 23 | 24 | static void pattern2(int n) { 25 | for (int i = 0; i < n; i++) { 26 | for (int j = i; j < n; j++) { 27 | System.out.print((char) ('A' + n - j - 1) + " "); 28 | } 29 | System.out.println(); 30 | } 31 | } 32 | // pattern output 33 | // E D C B A 34 | // D C B A 35 | // C B A 36 | // B A 37 | // A 38 | } 39 | -------------------------------------------------------------------------------- /DS_02_Pattern_Questions/Patterns_With_Numbers/Pattern.java: -------------------------------------------------------------------------------- 1 | package DS_02_Pattern_Questions.Patterns_With_Numbers; 2 | 3 | public class Pattern { 4 | public static void main(String[] args) { 5 | pattern5(5); 6 | } 7 | 8 | static void pattern1(int n) { 9 | for (int i = 0; i < n; i++) { 10 | for (int j = 0; j <= i; j++) { 11 | System.out.print(j + 1 + " "); 12 | } 13 | System.out.println(); 14 | } 15 | } 16 | // pattern output 17 | // 1 18 | // 1 2 19 | // 1 2 3 20 | // 1 2 3 4 21 | // 1 2 3 4 5 22 | 23 | static void pattern2(int n) { 24 | for (int i = 0; i < n; i++) { 25 | for (int j = i; j < n; j++) { 26 | System.out.print(i + 1 + " "); 27 | } 28 | System.out.println(); 29 | } 30 | } 31 | // pattern output 32 | // 1 1 1 1 1 33 | // 2 2 2 2 34 | // 3 3 3 35 | // 4 4 36 | // 5 37 | 38 | static void pattern3(int n) { 39 | int count = 1; 40 | for (int i = 0; i < n; i++) { 41 | for (int j = 0; j <= i; j++) { 42 | System.out.print((count < 10) ? "0" + count++ + " " : count++ + " "); 43 | } 44 | System.out.println(); 45 | } 46 | } 47 | // pattern output 48 | // 1 49 | // 2 3 50 | // 4 5 6 51 | // 7 8 9 10 52 | 53 | static void pattern4(int n) { 54 | for (int i = 0; i < n; i++) { 55 | for (int j = 0; j <= i; j++) { 56 | System.out.print(((i - j) % 2 == 1) ? "0 " : "1 "); 57 | } 58 | System.out.println(); 59 | } 60 | } 61 | // pattern output 62 | // 1 63 | // 0 1 64 | // 1 0 1 65 | // 0 1 0 1 66 | // 1 0 1 0 1 67 | 68 | static void pattern5(int n) { 69 | for (int i = 0; i < n; i++) { 70 | for (int j = i; j < n - 1; j++) { 71 | System.out.print(" "); 72 | } 73 | for (int j = i; j > 0; j--) { 74 | System.out.print(j + 1 + " "); 75 | } 76 | for (int j = 0; j <= i; j++) { 77 | System.out.print(j + 1 + " "); 78 | } 79 | System.out.println(); 80 | } 81 | } 82 | // pattern output 83 | // 1 84 | // 2 1 2 85 | // 3 2 1 2 3 86 | // 4 3 2 1 2 3 4 87 | // 5 4 3 2 1 2 3 4 5 88 | } 89 | -------------------------------------------------------------------------------- /DS_02_Pattern_Questions/README.md: -------------------------------------------------------------------------------- 1 | # Questions on Patterns 2 | 3 | This type of questions are usually asked in service based companies. 4 | These questions are the best to improve the programming logic 5 | 6 | 7 | Print these patterns using loops: 8 | 9 | ```text 10 | 11 | 1. ***** 12 | ***** 13 | ***** 14 | ***** 15 | ***** 16 | 17 | 18 | 2. * 19 | ** 20 | *** 21 | **** 22 | ***** 23 | 24 | 25 | 3. ***** 26 | **** 27 | *** 28 | ** 29 | * 30 | 31 | 32 | 4. 1 33 | 1 2 34 | 1 2 3 35 | 1 2 3 4 36 | 1 2 3 4 5 37 | 38 | 39 | 5. * 40 | ** 41 | *** 42 | **** 43 | ***** 44 | **** 45 | *** 46 | ** 47 | * 48 | 49 | 50 | 6. * 51 | ** 52 | *** 53 | **** 54 | ***** 55 | 56 | 57 | 7. ***** 58 | **** 59 | *** 60 | ** 61 | * 62 | 63 | 64 | 8. * 65 | *** 66 | ***** 67 | ******* 68 | ********* 69 | 70 | 71 | 9. ********* 72 | ******* 73 | ***** 74 | *** 75 | * 76 | 77 | 78 | 10. * 79 | * * 80 | * * * 81 | * * * * 82 | * * * * * 83 | 84 | 85 | 11. * * * * * 86 | * * * * 87 | * * * 88 | * * 89 | * 90 | 91 | 92 | 12. * * * * * 93 | * * * * 94 | * * * 95 | * * 96 | * 97 | * 98 | * * 99 | * * * 100 | * * * * 101 | * * * * * 102 | 103 | 104 | 13. * 105 | * * 106 | * * 107 | * * 108 | ********* 109 | 110 | 111 | 14. ********* 112 | * * 113 | * * 114 | * * 115 | * 116 | 117 | 118 | 15. * 119 | * * 120 | * * 121 | * * 122 | * * 123 | * * 124 | * * 125 | * * 126 | * 127 | 128 | 129 | 16. 1 130 | 1 1 131 | 1 2 1 132 | 1 3 3 1 133 | 1 4 6 4 1 134 | 135 | 136 | 17. 1 137 | 212 138 | 32123 139 | 4321234 140 | 32123 141 | 212 142 | 1 143 | 144 | 145 | 18. ********** 146 | **** **** 147 | *** *** 148 | ** ** 149 | * * 150 | * * 151 | ** ** 152 | *** *** 153 | **** **** 154 | ********** 155 | 156 | 157 | 19. * * 158 | ** ** 159 | *** *** 160 | **** **** 161 | ********** 162 | **** **** 163 | *** *** 164 | ** ** 165 | * * 166 | 167 | 168 | 20. **** 169 | * * 170 | * * 171 | * * 172 | **** 173 | 174 | 21. 1 175 | 2 3 176 | 4 5 6 177 | 7 8 9 10 178 | 11 12 13 14 15 179 | 180 | 22. 1 181 | 0 1 182 | 1 0 1 183 | 0 1 0 1 184 | 1 0 1 0 1 185 | 186 | 23. * * 187 | * * * * 188 | * * * 189 | 190 | 24. * * 191 | ** ** 192 | * * * * 193 | * * * * 194 | * ** * 195 | * ** * 196 | * * * * 197 | * * * * 198 | ** ** 199 | * * 200 | 201 | 25. ***** 202 | * * 203 | * * 204 | * * 205 | ***** 206 | 207 | 26. 1 1 1 1 1 1 208 | 2 2 2 2 2 209 | 3 3 3 3 210 | 4 4 4 211 | 5 5 212 | 6 213 | 214 | 27. 1 2 3 4 17 18 19 20 215 | 5 6 7 14 15 16 216 | 8 9 12 13 217 | 10 11 218 | 219 | 28. * 220 | * * 221 | * * * 222 | * * * * 223 | * * * * * 224 | * * * * 225 | * * * 226 | * * 227 | * 228 | 229 | 29. 230 | * * 231 | ** ** 232 | *** *** 233 | **** **** 234 | ********** 235 | **** **** 236 | *** *** 237 | ** ** 238 | * * 239 | 240 | 30. 1 241 | 2 1 2 242 | 3 2 1 2 3 243 | 4 3 2 1 2 3 4 244 | 5 4 3 2 1 2 3 4 5 245 | 246 | 247 | 31. 4 4 4 4 4 4 4 248 | 4 3 3 3 3 3 4 249 | 4 3 2 2 2 3 4 250 | 4 3 2 1 2 3 4 251 | 4 3 2 2 2 3 4 252 | 4 3 3 3 3 3 4 253 | 4 4 4 4 4 4 4 254 | 255 | 32. E 256 | D E 257 | C D E 258 | B C D E 259 | A B C D E 260 | 261 | 33. a 262 | B c 263 | D e F 264 | g H i J 265 | k L m N o 266 | 267 | 34. E D C B A 268 | D C B A 269 | C B A 270 | B A 271 | A 272 | 273 | 35. 1 1 274 | 12 21 275 | 123 321 276 | 12344321 277 | ``` -------------------------------------------------------------------------------- /DS_02_Pattern_Questions/Star_Patterns/Pattern.java: -------------------------------------------------------------------------------- 1 | package DS_02_Pattern_Questions.Star_Patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int n = sc.nextInt(); 9 | sc.close(); 10 | pattern2(n); 11 | } 12 | 13 | static void pattern1(int n) { 14 | for (int i = 0; i < n; i++) { 15 | for (int j = 0; j < n; j++) { 16 | System.out.print("*"); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | // pattern1 Output 22 | // ****** 23 | // ****** 24 | // ****** 25 | // ****** 26 | // ****** 27 | // ****** 28 | 29 | static void pattern2(int n) { 30 | for (int i = 0; i < n; i++) { 31 | for (int j = 0; j <= i; j++) { 32 | System.out.print("*"); 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | // pattern2 Output 38 | // * 39 | // ** 40 | // *** 41 | // **** 42 | // ***** 43 | 44 | static void pattern3(int n) { 45 | for (int i = 0; i < n; i++) { 46 | for (int j = i; j < n; j++) { 47 | System.out.print("*"); 48 | } 49 | System.out.println(); 50 | } 51 | } 52 | // pattern3 Output 53 | // ***** 54 | // **** 55 | // *** 56 | // ** 57 | // * 58 | 59 | } 60 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_1/BM01_Binary_to_Decimal.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_1; 2 | 3 | public class BM01_Binary_to_Decimal { 4 | public static void main(String[] args) { 5 | System.out.println(toDecimal(1101011001)); 6 | } 7 | 8 | static int toDecimal(int num) { 9 | int ans = 0; 10 | int i = 0; 11 | while (num != 0) { 12 | int digit = num % 10; 13 | num = num / 10; 14 | ans = ans + digit * (int) Math.pow(2, i++); 15 | } 16 | return ans; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_1/BM02_Decimal_to_Binary.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_1; 2 | 3 | public class BM02_Decimal_to_Binary { 4 | public static void main(String[] args) { 5 | System.out.println(toBinary(5)); 6 | } 7 | 8 | static int toBinary(int n) { 9 | int[] binary = new int[40]; 10 | int i = 0; 11 | while (n != 0) { 12 | int rem = n & 1; 13 | n = n / 2; 14 | binary[i++] = rem; 15 | } 16 | int ans = 0; 17 | for (int j = i; j >= 0; j--) { 18 | ans = ans * 10 + binary[j]; 19 | } 20 | 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_1/BM03_Not_Operator.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_1; 2 | 3 | public class BM03_Not_Operator { 4 | public static void main(String[] args) { 5 | System.out.println(notOperator(5)); 6 | } 7 | 8 | static int notOperator(int n){ 9 | return ~n; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_1/BM04_Shifting_Operator.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_1; 2 | 3 | public class BM04_Shifting_Operator { 4 | public static void main(String[] args) { 5 | System.out.println(13 >> 1); 6 | System.out.println(13 >> 2); 7 | System.out.println(13 >> 3); 8 | System.out.println(13 << 1); 9 | System.out.println(13 << 2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_1/BM05_Swap_Numbers.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_1; 2 | 3 | public class BM05_Swap_Numbers { 4 | public static void main(String[] args) { 5 | int a = 10; 6 | int b = 20; 7 | 8 | System.out.println("before swap : " + a + " " + b); 9 | a = a ^ b; 10 | b = a ^ b; 11 | a = a ^ b; 12 | 13 | System.out.println("after swap : " + a + " " + b); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_1/BM06_Find_EvenOdd.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BM06_Find_EvenOdd { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | System.out.println("Enter the number : "); 9 | int a = sc.nextInt(); 10 | if ((a & 1) == 0) 11 | System.out.println(a + " is Even Number"); 12 | else 13 | System.out.println(a + " is Odd Number"); 14 | 15 | sc.close(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_2/BM01_FindBit.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_2; 2 | 3 | public class BM01_FindBit { 4 | public static void main(String[] args) { 5 | System.out.println(findIthBit(3, 2)); 6 | System.out.println(findBit(3, 2)); 7 | } 8 | 9 | // method 1 10 | static int findBit(int n, int i) { 11 | n = n >> i; 12 | return (n & 1) == 1 ? 1 : 0; 13 | } 14 | 15 | // method 2 16 | static int findIthBit(int n, int i) { 17 | int mask = 1 << i; 18 | return ((mask & n) > 0) ? 1 : 0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_2/BM02_SetBit.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_2; 2 | 3 | public class BM02_SetBit { 4 | public static void main(String[] args) { 5 | System.out.println(setIthBit(3, 2)); 6 | } 7 | 8 | static int setIthBit(int n, int i) { 9 | int mask = 1 << i; 10 | return n | mask; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_2/BM03_ResetBit.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_2; 2 | 3 | public class BM03_ResetBit { 4 | public static void main(String[] args) { 5 | System.out.println(resetBit(15, 2)); 6 | } 7 | 8 | static int resetBit(int n, int i) { 9 | int mask = 1 << i; 10 | return ~mask & n; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_2/BM04_ClearBit.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_2; 2 | 3 | public class BM04_ClearBit { 4 | public static void main(String[] args) { 5 | System.out.println(clearBit(15, 2)); 6 | } 7 | 8 | static int clearBit(int n, int i) { 9 | int mask = 1 << i; 10 | return ~mask & n; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_2/BM05_RightmostSetBit.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_2; 2 | 3 | public class BM05_RightmostSetBit { 4 | public static void main(String[] args) { 5 | System.out.println(rightmostBit(13)); 6 | } 7 | 8 | static int rightmostBit(int n) { 9 | return n & -n; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_2/BM06_Number_of_SetBit.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_2; 2 | 3 | public class BM06_Number_of_SetBit { 4 | public static void main(String[] args) { 5 | System.out.println(no_of_setBits(16)); 6 | System.out.println(no_of_SetBits(16)); 7 | } 8 | 9 | // method 1 10 | static int no_of_setBits(int n) { 11 | int count = 0; 12 | while (n != 0) { 13 | if ((n & 1) == 1) 14 | count++; 15 | n = n >> 1; 16 | } 17 | return count; 18 | } 19 | 20 | // method 2 21 | static int no_of_SetBits(int n){ 22 | int count = 0; 23 | while(n != 0){ 24 | n = n & (n-1); 25 | count++; 26 | } 27 | return count; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/Part_2/BM07_ChangeNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.Part_2; 2 | 3 | public class BM07_ChangeNumber { 4 | public static void main(String[] args) { 5 | System.out.println(change(22, 27)); 6 | } 7 | 8 | static int change(int a, int b) { 9 | int ans = a ^ b; 10 | int count = 0; 11 | while (ans != 0) { 12 | if ((ans & 1) == 1) 13 | count++; 14 | ans = ans >> 1; 15 | } 16 | 17 | return count; 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/README.md: -------------------------------------------------------------------------------- 1 | # TOPIC COVERED -> BIT MANIPULATION 2 | 3 | ### bitwise operators are - & , | , ^ , ~ , >> , <<. 4 | 5 | | a | b | a and b | a or b | a xor b | 6 | | :-- | :-: | :-----: | :----: | ------: | 7 | | 0 | 0 | 0 | 0 | 0 | 8 | | 0 | 1 | 0 | 1 | 1 | 9 | | 1 | 0 | 0 | 1 | 1 | 10 | | 1 | 1 | 1 | 1 | 0 | 11 | 12 | ## Questions in part 1 13 | 14 | - Q. Convert a number from binary to decimal.✅ 15 | - Q. Convert a number from decimal to binary.✅ 16 | - Q. Not Operator Demo.✅ 17 | - Q. Shift Operators Demo.✅ 18 | - Q. Swap two numbers✅ 19 | - using bitwise operator. 20 | - without using third variable. 21 | - Q. Check wheather number is odd or even using bitwise operator.✅ 22 | 23 | --- 24 | 25 | ## Questions in part 2 26 | 27 | - Q. Find the ith bit of a number✅. 28 | - Q. Set the ith bit of a number✅. 29 | - Q. Reset the ith bit of a number✅. 30 | - Q. Clear the bit at specific position✅. 31 | - Q. Find the position of the rightmost set bit✅. 32 | - Q. Find the number of set bits✅. 33 | - Q. Find bits to change number from a to b✅. 34 | 35 | --- 36 | 37 | ## Additional Questions Asked in interviews. 38 | 39 | - Q. Find number of digits in base b✅ 40 | - Q. Find XOR of numbers from 0 till a✅ 41 | - Q. Find XOR of number between ranges✅ 42 | - Q. Find the unique element where every element appears twice✅ 43 | - Q. Find the two unique element where every element appears twice✅ 44 | - Q. Find the unique element where every element appears trice✅ 45 | - Q. Find nth magic number✅ 46 | - Q. Find wheather number is power of two or not✅ 47 | - Q. Calculate a to the power b using bitwise operator✅ 48 | - Q. Rotate bits of binary number✅ 49 | - Q. Find trailing zeros in binary number✅ 50 | - Q. Find duplicate in array where the array elements range from 1 to n-1 where n is size of the array✅ 51 | - Q. Google question -> flip image✅ 52 | - Q. Reverse bits of binary number✅ 53 | - Q. Find compliment of number with base 10✅ 54 | 55 | 56 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM01_DigitsInNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM01_DigitsInNumber { 4 | public static void main(String[] args) { 5 | System.out.println(no_digits(14654653, 10)); 6 | } 7 | 8 | static int no_digits(int num, int base) { 9 | 10 | int digits = (int) (Math.log(num) / Math.log(base)); 11 | 12 | return digits + 1; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM02_FindXOR.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM02_FindXOR { 4 | public static void main(String[] args) { 5 | System.out.println(findXOR(34)); 6 | System.out.println(findXOR2(34)); 7 | } 8 | 9 | static int findXOR(int num) { 10 | if (num % 4 == 0) 11 | return num; 12 | if (num % 4 == 1) 13 | return 1; 14 | if (num % 4 == 2) 15 | return num + 1; 16 | return 0; 17 | } 18 | 19 | static int findXOR2(int num){ 20 | int ans = 0; 21 | for (int i = 0; i <= num; i++) { 22 | ans = ans ^ i; 23 | } 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM03_FindXORinRange.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM03_FindXORinRange { 4 | public static void main(String[] args) { 5 | System.out.println(xorInRange(12, 24)); 6 | System.out.println(xorInRange2(12, 24)); 7 | } 8 | 9 | static int xorInRange(int a, int b) { 10 | return findXOR(b) ^ findXOR(a - 1); 11 | } 12 | 13 | static int xorInRange2(int a, int b) { 14 | int ans = 0; 15 | for (int i = a; i <= b; i++) { 16 | ans = ans ^ i; 17 | } 18 | return ans; 19 | } 20 | 21 | static int findXOR(int a) { 22 | if (a % 4 == 0) 23 | return a; 24 | if (a % 4 == 1) 25 | return 1; 26 | if (a % 4 == 2) 27 | return a + 1; 28 | return 0; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM04_FindUniqueElement.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM04_FindUniqueElement { 4 | public static void main(String[] args) { 5 | int[] arr = { 1, 2, 1, 2, 3, 4, 4, 5, 3, 5, 6 }; 6 | System.out.println(findUnique(arr)); 7 | } 8 | 9 | static int findUnique(int[] arr) { 10 | int ans = 0; 11 | for (int i = 0; i < arr.length; i++) { 12 | ans = ans ^ arr[i]; 13 | } 14 | return ans; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM05_FindTwoUnique.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM05_FindTwoUnique { 4 | public static void main(String[] args) { 5 | int[] arr = { 7, 12, 12, 34, 34, 56, 5, 34, 56, 7 }; 6 | UniqueElement(arr); 7 | } 8 | 9 | static void UniqueElement(int[] arr) { 10 | // what is the approach ? 11 | // step 1 - first find the whole XOR of the elements 12 | int sum = 0; 13 | for (int j : arr) { 14 | sum ^= j; 15 | } 16 | // step 2 - find the rightmost set bit of the sum 17 | sum = sum & (-sum); 18 | // step 3 - maintain sum1 for elements having same rightmost bit and 19 | int sum1 = 0, sum2 = 0; 20 | 21 | for (int j : arr) { 22 | if ((j & sum) > 0) 23 | sum1 ^= j; 24 | else 25 | sum2 ^= j; 26 | } 27 | System.out.println("the non repeating elements are " + sum1 + " and " + sum2); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM06_FindUniqueWithThreeElements.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM06_FindUniqueWithThreeElements { 4 | public static void main(String[] args) { 5 | int arr[] = { 2, 5, 2, 2, 5, 5, 8 }; 6 | int n = arr.length; 7 | int k = 3; 8 | System.out.println(findUnique(arr, n, k)); 9 | } 10 | 11 | static int findUnique(int[] arr, int n, int k) { 12 | byte size_of_int = 4; 13 | int int_size = 8 * size_of_int; 14 | int[] count = new int[int_size]; 15 | 16 | for (int i = 0; i < int_size; i++) { 17 | int mask = 1 << i; 18 | for (int j = 0; j < n; j++) { 19 | if ((arr[j] & mask) != 0) { 20 | count[i] += 1; 21 | } 22 | } 23 | } 24 | 25 | int result = 0; 26 | 27 | for (int i = 0; i < int_size; i++) { 28 | int mask1 = 1 << i; 29 | int remainder = count[i] % k; 30 | result = result + (remainder * mask1); 31 | } 32 | 33 | return result; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM07_Nth_MagicNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM07_Nth_MagicNumber { 4 | public static void main(String[] args) { 5 | int n = 5; 6 | int ans = 0; 7 | int cnt = 1; 8 | while (n != 0) { 9 | if ((n & 1) == 1) { 10 | ans = ans + (int) Math.pow(5, cnt); 11 | } 12 | n = n >> 1; 13 | cnt++; 14 | } 15 | System.out.println(ans); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM08_PowerOfTwo.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM08_PowerOfTwo { 4 | public static void main(String[] args) { 5 | System.out.println(isPowerOfTwoOrNot(31)); 6 | } 7 | 8 | static boolean isPowerOfTwoOrNot(int n) { 9 | if(n <= 0) return false; 10 | return (n & (n - 1)) == 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM09_Power.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM09_Power { 4 | public static void main(String[] args) { 5 | int base = 2; 6 | int power = 4; 7 | int ans = 1; 8 | while (power > 0) { 9 | if ((power & 1) == 1) { 10 | ans *= base; 11 | } 12 | base = base * base; 13 | power = power >> 1; 14 | } 15 | System.out.println(ans); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM10_RotateBitsInBinary.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM10_RotateBitsInBinary { 4 | static int INT_BITS = 32; 5 | 6 | static int leftRotate(int n, int d) { 7 | return (n << d) | (n >> (INT_BITS - d)); 8 | } 9 | 10 | static int rightRotate(int n, int d) { 11 | return (n >> d) | (n << (INT_BITS - d)); 12 | } 13 | 14 | public static void main(String[] args) { 15 | System.out.println(rotateBinary(10)); 16 | System.out.println(leftRotate(11, 5)); 17 | } 18 | 19 | static int rotateBinary(int num) { 20 | int result = 0; 21 | while (num > 0) { 22 | result = result << 1; 23 | result = result | (num & 1); 24 | num = num >> 1; 25 | } 26 | 27 | return result; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM11_TrailingZerosInBinary.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM11_TrailingZerosInBinary { 4 | public static void main(String[] args) { 5 | System.out.println(trailingZeros(17)); 6 | System.out.println(countTrailingZeroes(17)); 7 | } 8 | 9 | // method 1 10 | static int trailingZeros(int num) { 11 | int ans = 0; 12 | while (num != 0) { 13 | if ((num & 1) == 0) 14 | ans++; 15 | else 16 | break; 17 | num = num >> 1; 18 | } 19 | return ans; 20 | } 21 | 22 | // method 2 23 | public static int countTrailingZeroes(int num) { 24 | int res = num ^ (num - 1); 25 | return (int) (Math.log(res) / Math.log(2)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM12_FlipImage.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | import java.util.Arrays; 4 | 5 | public class BM12_FlipImage { 6 | public static void main(String[] args) { 7 | int arr[][] = { { 1, 1, 0 }, 8 | { 1, 0, 1 }, 9 | { 0, 0, 1 } }; 10 | 11 | int arr2[][] = { { 1, 1, 0, 0 }, 12 | { 1, 0, 0, 1 }, 13 | { 0, 1, 1, 1 }, 14 | { 1, 0, 1, 0 } }; 15 | 16 | for (int i = 0; i < arr.length; i++) { 17 | arr[i] = arrayReverse(arr[i]); 18 | } 19 | arr = change(arr); 20 | System.out.println(Arrays.deepToString(arr)); 21 | 22 | System.out.println(Arrays.deepToString(flipAndInvertImage2(arr2))); 23 | } 24 | 25 | public static int[][] flipAndInvertImage2(int[][] image) { 26 | for (int[] row : image) { 27 | for (int i = 0; i < (image[0].length + 1) / 2; i++) { 28 | int temp = row[i] ^ 1; 29 | row[i] = row[image[0].length - i - 1] ^ 1; 30 | row[image[0].length - i - 1] = temp; 31 | } 32 | } 33 | return image; 34 | } 35 | 36 | public static int[][] flipAndInvertImage(int[][] image) { 37 | for (int i = 0; i < image.length; i++) { 38 | image[i] = arrayReverse(image[i]); 39 | } 40 | return image; 41 | } 42 | 43 | static int[] arrayReverse(int[] arr) { 44 | int i = 0; 45 | int j = arr.length - 1; 46 | while (i < j) { 47 | int temp = arr[i]; 48 | arr[i] = arr[j]; 49 | arr[j] = temp; 50 | i++; 51 | j--; 52 | } 53 | return arr; 54 | } 55 | 56 | static int[][] change(int[][] arr) { 57 | for (int i = 0; i < arr.length; i++) { 58 | for (int j = 0; j < arr[0].length; j++) { 59 | if (arr[i][j] == 1) 60 | arr[i][j] = 0; 61 | else 62 | arr[i][j] = 1; 63 | } 64 | } 65 | return arr; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM13_FindDuplicate.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM13_FindDuplicate { 4 | public static void main(String[] args) { 5 | int arr[] = { 1, 3, 2, 4, 5, 3 }; 6 | System.out.println(findDuplicate(arr)); 7 | } 8 | 9 | static int findDuplicate(int[] arr) { 10 | int ans = 0; 11 | for (int i = 0; i < arr.length; i++) { 12 | ans = ans ^ i ^ arr[i]; 13 | } 14 | 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM14_ReverseBitsinBinary.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM14_ReverseBitsinBinary { 4 | public static void main(String[] args) { 5 | System.out.println(reverseBits2(11)); 6 | } 7 | 8 | static int reverseBits(int num) { 9 | int rev = 0; 10 | for (int i = 0; i < 32; i++) { 11 | rev = rev << 1; 12 | int mask = 1 << i; 13 | if ((num & mask) != 0) { 14 | rev = rev | 1; 15 | } 16 | 17 | } 18 | return rev; 19 | } 20 | 21 | static int reverseBits2(int num) { 22 | int rev = 0; 23 | while (num != 0) { 24 | rev = rev << 1; 25 | if ((num & 1) == 1) { 26 | rev = rev ^ 1; 27 | } 28 | num = num >> 1; 29 | } 30 | return rev; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/TestQuestion/BM15_ComplimentOfNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Bit_Manipulation.TestQuestion; 2 | 3 | public class BM15_ComplimentOfNumber { 4 | 5 | public static void main(String[] args) { 6 | System.out.println(bitwiseComplement(12)); 7 | } 8 | 9 | static int bitwiseComplement(int n) { 10 | if (n == 0) 11 | return 1; 12 | int temp = n; 13 | int mask = 0; 14 | while (n != 0) { 15 | mask = (mask << 1) | 1; 16 | n = n >> 1; 17 | } 18 | return ~temp & mask; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DS_03_Number_System/Bit_Manipulation/bitManipulation.txt: -------------------------------------------------------------------------------- 1 | bitwise operatos - &, |, ^, ~, >>, <<, >>> -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_1/MD01_ReverseNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_1; 2 | 3 | public class MD01_ReverseNumber { 4 | public static void main(String[] args) { 5 | System.out.println(reverse(1234)); 6 | } 7 | 8 | static int reverse(int num) { 9 | int rev = 0; 10 | while (num != 0) { 11 | int digit = num % 10; 12 | num = num / 10; 13 | rev = rev * 10 + digit; 14 | } 15 | return rev; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_1/MD02_PalindromeNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_1; 2 | 3 | public class MD02_PalindromeNumber { 4 | public static void main(String[] args) { 5 | System.out.println(isPalindrome(12332)); 6 | } 7 | 8 | static boolean isPalindrome(int num) { 9 | int temp = num; 10 | int rev = 0; 11 | while (num != 0) { 12 | int digit = num % 10; 13 | num = num / 10; 14 | rev = rev * 10 + digit; 15 | } 16 | return temp == rev; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_1/MD03_ArmstrongNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_1; 2 | 3 | public class MD03_ArmstrongNumber { 4 | public static void main(String[] args) { 5 | for (int i = 0; i < 10000000; i++) { 6 | System.out.print((isArmStrong(i)) ? i + " " : ""); 7 | } 8 | } 9 | 10 | static boolean isArmStrong(int num) { 11 | int sum = 0; 12 | int temp = num; 13 | while (num != 0) { 14 | int digit = num % 10; 15 | num = num / 10; 16 | sum = sum + (int) Math.pow(digit, 3); 17 | } 18 | return sum == temp; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_1/MD04_PerfectNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_1; 2 | 3 | public class MD04_PerfectNumber { 4 | public static void main(String[] args) { 5 | for (int i = 0; i < 1000000; i++) { 6 | 7 | System.out.print(isPerfect(i) ? i + " " : ""); 8 | } 9 | } 10 | 11 | static boolean isPerfect(int num) { 12 | int sum = 0; 13 | for (int i = 1; i < num; i++) { 14 | if (num % i == 0) { 15 | sum += i; 16 | } 17 | } 18 | return sum == num; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_1/MD05_HappyNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_1; 2 | 3 | public class MD05_HappyNumber { 4 | public static void main(String[] args) { 5 | for (int i = 0; i < 10000; i++) { 6 | System.out.print(isHappy(i) ? i + " " : ""); 7 | } 8 | } 9 | 10 | static int square(int num) { 11 | int sum = 0; 12 | if (num < 10) 13 | return num; 14 | while (num != 0) { 15 | int digit = num % 10; 16 | num = num / 10; 17 | sum += digit * digit; 18 | } 19 | return sum; 20 | } 21 | 22 | static boolean isHappy(int num) { 23 | int fast = num; 24 | int slow = num; 25 | do { 26 | slow = square(slow); 27 | fast = square(square(fast)); 28 | } while (slow != fast); 29 | return slow == 1; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_1/MD06_FibonacciNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_1; 2 | 3 | public class MD06_FibonacciNumber { 4 | public static void main(String[] args) { 5 | for (int i = 0; i < 15; i++) { 6 | System.out.print(fibo(i) + " "); 7 | } 8 | } 9 | 10 | static int fibo(int num) { 11 | if (num == 1 || num == 2) 12 | return 1; 13 | int a = 0; 14 | int b = 1; 15 | int sum = 0; 16 | for (int i = 1; i < num; i++) { 17 | sum = a + b; 18 | a = b; 19 | b = sum; 20 | } 21 | return sum; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_1/MD07_CatalanNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_1; 2 | 3 | public class MD07_CatalanNumber { 4 | public static void main(String[] args) { 5 | System.out.println(catalan(4)); 6 | } 7 | 8 | static int catalan(int n) { 9 | if (n <= 1) { 10 | return 1; 11 | } 12 | int res = 0; 13 | for (int i = 0; i <= n - 1; i++) { 14 | res += catalan(i) * catalan(n - i - 1); 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_1/MD08_SmithNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_1; 2 | 3 | import java.util.*; 4 | 5 | public class MD08_SmithNumber { 6 | public static void main(String args[]) { 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter a number: "); 9 | 10 | int n = sc.nextInt(); 11 | 12 | int a = findSumOfDigit(n); 13 | int b = findSumPrimeFactors(n); 14 | 15 | System.out.println("Sum of Digits of the given number is = " + a); 16 | System.out.println("Sum of digits of its prime factors is = " + b); 17 | 18 | if (a == b) 19 | System.out.print("The given number is a smith number."); 20 | else 21 | System.out.print("The given number is not a smith number."); 22 | } 23 | 24 | static int findSumPrimeFactors(int n) { 25 | int i = 2, sum = 0; 26 | while (n > 1) { 27 | if (n % i == 0) { 28 | sum = sum + findSumOfDigit(i); 29 | n = n / i; 30 | } else { 31 | do { 32 | i++; 33 | } while (!isPrime(i)); 34 | } 35 | } 36 | return sum; 37 | } 38 | 39 | static int findSumOfDigit(int n) { 40 | int s = 0; 41 | while (n > 0) { 42 | s = s + n % 10; 43 | n = n / 10; 44 | } 45 | return s; 46 | } 47 | 48 | static boolean isPrime(int k) { 49 | boolean b = true; 50 | int d = 2; 51 | while (d < Math.sqrt(k)) { 52 | if (k % d == 0) { 53 | b = false; 54 | } 55 | d++; 56 | } 57 | return b; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD01_Factorial.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD01_Factorial { 4 | public static void main(String[] args) { 5 | System.out.println(factorial(10)); 6 | } 7 | 8 | static int factorial(int n){ 9 | int ans = 1; 10 | for (int i = 1; i <= n ; i++) { 11 | ans = ans* i; 12 | } 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD02_TrailingZerosInFactorial.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD02_TrailingZerosInFactorial { 4 | public static void main(String[] args) { 5 | System.out.println(trailingZeros(30)); 6 | } 7 | 8 | static int trailingZeros(int n) { 9 | int result = 0; 10 | for (int i = 5; i <= n; i *= 5) { 11 | result = result + n / i; 12 | } 13 | return result; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD03_FastPower.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD03_FastPower { 4 | public static void main(String[] args) { 5 | System.out.println(fastPower(3, 4)); 6 | } 7 | 8 | static int fastPower(int a, int b) { 9 | int res = 1; 10 | while (b > 0) { 11 | if ((b & 1) != 0) { 12 | res = res * a; 13 | } 14 | a = a * a; 15 | b = b >> 1; 16 | } 17 | return res; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD04_SquareRoot.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD04_SquareRoot { 4 | public static void main(String[] args) { 5 | 6 | } 7 | 8 | static double sqrt(int n, int p) { 9 | int start = 0; 10 | int end = n; 11 | double root = 0.0; 12 | 13 | while (start <= end) { 14 | int mid = start + (end - start) / 2; 15 | if (mid * mid == n) { 16 | // root = m; 17 | return mid; 18 | } 19 | if (mid * mid > n) { 20 | end = mid - 1; 21 | } else { 22 | start = mid + 1; 23 | } 24 | } 25 | 26 | double incr = 0.1; 27 | for (int i = 0; i < p; i++) { 28 | while (root * root <= n) { 29 | root += incr; 30 | } 31 | root -= incr; 32 | incr /= 10; 33 | } 34 | return root; 35 | } 36 | 37 | static double sqrt(double n) { 38 | double x = n; 39 | double root; 40 | while (true) { 41 | root = 0.5 * (x + (n / x)); 42 | if (Math.abs(root - x) < 0.5) { 43 | break; 44 | } 45 | x = root; 46 | } 47 | return root; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD05_FactorsOfNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD05_FactorsOfNumber { 4 | public static void main(String[] args) { 5 | 6 | } 7 | 8 | static void factors(int n) { 9 | for (int i = 1; i <= n; i++) { 10 | if (n % i == 0) 11 | System.out.print(i + " "); 12 | } 13 | } 14 | 15 | static void factors2(int n) { 16 | for (int i = 1; i * i <= n; i++) { 17 | if (n % i == 0) { 18 | if (n / i == i) { 19 | System.out.print(i + " "); 20 | } else { 21 | 22 | System.out.print(i + " " + n / i + " "); 23 | } 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD06_EuclidGCD.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD06_EuclidGCD { 4 | public static void main(String[] args) { 5 | System.out.println(gcd(24, 42)); 6 | } 7 | 8 | static int gcd(int a, int b){ 9 | while(b != 0){ 10 | int rem = a % b; 11 | a = b; 12 | b = rem; 13 | } 14 | return a ; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD07_PrimeNumber.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD07_PrimeNumber { 4 | public static void main(String[] args) { 5 | int num = 130; 6 | 7 | for (int i = 0; i < num; i++) { 8 | System.out.print(isPrime(i)? i+" ":""); 9 | } 10 | } 11 | 12 | static boolean isPrime(int n) { 13 | if(n <= 1) return false; 14 | for (int i = 2; i * i <= n; i++) { 15 | if (n % i == 0) 16 | return false; 17 | } 18 | return true; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD08_SieveOfEratosthenes.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD08_SieveOfEratosthenes { 4 | public static void main(String[] args) { 5 | int n = 37; 6 | boolean[] isPrime = new boolean[n + 1]; 7 | sieve(n, isPrime); 8 | } 9 | 10 | static void sieve(int n, boolean[] prime) { 11 | for (int i = 2; i * i <= n; i++) { 12 | if (!prime[i]) { 13 | for (int j = 2 * i; j <= n; j += i) { 14 | prime[j] = true; 15 | } 16 | } 17 | } 18 | 19 | for (int i = 2; i <= n; i++) { 20 | if (!prime[i]) 21 | System.out.print(i + " "); 22 | } 23 | } 24 | 25 | // code to submit on leetcode 26 | // https://leetcode.com/problems/count-primes/ 27 | public static int countPrimes(int n) { 28 | if (n <= 1) 29 | return 0; 30 | int cnt = 0; 31 | boolean[] prime = new boolean[n + 1]; 32 | prime[0] = prime[1] = true; 33 | 34 | for (int i = 2; i * i <= n; i++) { 35 | for (int j = 2 * i; j < n; j += i) { 36 | prime[j] = true; 37 | } 38 | 39 | } 40 | for (int i = 0; i < prime.length; i++) { 41 | if (!prime[i]) 42 | cnt++; 43 | } 44 | return cnt - 1; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/MD09_ModuloArithematics.java: -------------------------------------------------------------------------------- 1 | package DS_03_Number_System.Math_for_DSA.Part_2; 2 | 3 | public class MD09_ModuloArithematics { 4 | public static void main(String[] args) { 5 | System.out.println(fastPower(3978432, 5, 1000000007)); 6 | } 7 | 8 | static long fastPower(long a, long b, int n) { 9 | long res = 1; 10 | while (b > 0) { 11 | if ((b & 1) != 0) { 12 | res = (res * a % n) % n; 13 | } 14 | a = (a % n * a % n) % n; 15 | b = b >> 1; 16 | } 17 | return res; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/Part_2/part2.txt: -------------------------------------------------------------------------------- 1 | part 2 -------------------------------------------------------------------------------- /DS_03_Number_System/Math_for_DSA/README.md: -------------------------------------------------------------------------------- 1 | # TOPIC COVERED -> MATH FOR DSA 2 | 3 | 4 | ## Questions in part 1 5 | 6 | - Q. Reverse a number.✅ 7 | - Q. Check wheather the number is Palindrome number or not.✅ 8 | - Q. Check wheather the number is Armstrong number or not.✅ 9 | - Q. Check wheather the number is Perfect number or not.✅ 10 | - Q. Check wheather the number is Happy number or not.✅ 11 | - Q. Find the nth fibonacci Number.✅ 12 | - Q. Find the nth Catalan Number.✅ 13 | - Q. Find wheather the number is Smith number or not.✅ 14 | 15 | 16 | --- 17 | 18 | ## Questions in part 2 19 | 20 | - Q. Find the Factorial of a number✅. 21 | - Q. Find trailing zeroes in Factorial of a number✅. 22 | - Q. Calculate power using fast power method✅. 23 | - Q. Calculate Squareroot of a number✅. 24 | - Q. Calculate Factors of given of a number✅. 25 | - Q. Calculate GCD/HCF using Euclid Algorithm✅. 26 | - Q. Check wheather the number is Prime number or not.✅ 27 | - Q. Calculate prime numbers within a range using Sieve Algorithm.✅ 28 | - Q. Modular Arithematics for competitive coding.✅ 29 | 30 | 31 | 32 | --- 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/MajorityElement.java: -------------------------------------------------------------------------------- 1 | package DS_04_Array_and_Strings.Arrays_and_Questions; 2 | 3 | public class MajorityElement { 4 | 5 | // gfg link - https://practice.geeksforgeeks.org/problems/majority-element-1587115620/1 6 | // gfg solution 7 | static int majorityElement(int arr[], int size){ 8 | int ansIndex = 0; 9 | int count = 1; 10 | for(int i = 1 ; i < arr.length ; i++){ 11 | if(arr[i] == arr[ansIndex]){ 12 | count ++; 13 | }else{ 14 | count--; 15 | } 16 | if(count == 0){ 17 | count = 1; 18 | ansIndex = i; 19 | } 20 | } 21 | 22 | int major = arr[ansIndex]; 23 | int ans = 0; 24 | for(int i = 0 ; i < arr.length ; i++){ 25 | if(arr[i] == major) ans++; 26 | } 27 | 28 | return ans > size/2 ? major: -1; 29 | } 30 | 31 | // leetcode link - https://leetcode.com/problems/majority-element/ 32 | // leetcode solution 33 | public int majorityElement(int[] arr) { 34 | int ansIndex = 0, cnt = 1; 35 | for(int i = 1 ; i < arr.length ; i++){ 36 | if(arr[i]== arr[ansIndex]) cnt++; 37 | else cnt--; 38 | if(cnt == 0) { 39 | cnt = 1; 40 | ansIndex = i; 41 | } 42 | } 43 | return arr[ansIndex]; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/Max_Till_i.java: -------------------------------------------------------------------------------- 1 | package DS_04_Array_and_Strings.Arrays_and_Questions; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Max_Till_i { 6 | public static void main(String[] args) { 7 | int arr[] = { 1, 0, 5, 4, 6, 8 }; 8 | 9 | for (int i = 1; i < arr.length; i++) { 10 | arr[i] = Math.max(arr[i-1], arr[i]); 11 | } 12 | System.out.println(Arrays.toString(arr)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/MaximunSumSubArray.java: -------------------------------------------------------------------------------- 1 | package DS_04_Array_and_Strings.Arrays_and_Questions; 2 | 3 | public class MaximunSumSubArray { 4 | 5 | // kadane's algorithm 6 | static int maxSumSubArray(int[] arr){ 7 | int maxSum = 0; 8 | int currentSum = 0; 9 | for(int i = 0 ; i < arr.length ; i++){ 10 | currentSum = currentSum + arr[i]; 11 | if(currentSum > maxSum){ 12 | maxSum = currentSum; 13 | } 14 | if(currentSum < 0){ 15 | currentSum = 0; 16 | } 17 | } 18 | return maxSum; 19 | 20 | } 21 | 22 | // gfg link : https://practice.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1 23 | long maxSubarraySum(int arr[], int n){ 24 | long maxSum = 0; 25 | long currentSum = 0; 26 | for(int i = 0 ; i < arr.length ; i++){ 27 | currentSum = currentSum + arr[i]; 28 | if(currentSum > maxSum){ 29 | maxSum = currentSum; 30 | } 31 | if(currentSum < 0){ 32 | currentSum = 0; 33 | } 34 | } 35 | return maxSum > 0 ? maxSum : findMax(arr); 36 | } 37 | 38 | long findMax(int[] arr){ 39 | long max = arr[0]; 40 | for(int i = 0 ; i < arr.length ; i++){ 41 | if(arr[i] > max) max = arr[i]; 42 | } 43 | return max; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/MultiDimensionalArray/SpiralPrint.java: -------------------------------------------------------------------------------- 1 | package DS_04_Array_and_Strings.Arrays_and_Questions.MultiDimensionalArray; 2 | 3 | public class SpiralPrint { 4 | public static void main(String[] args) { 5 | int[][] arr = { 6 | { 1, 2, 3 }, 7 | { 4, 5, 6 }, 8 | { 7, 8, 9 } 9 | }; 10 | spiralPrint(arr); 11 | } 12 | 13 | static void spiralPrint(int[][] arr) { 14 | int row = arr.length; 15 | int col = arr[0].length; 16 | int total = row * col; 17 | int count = 0; 18 | 19 | int startCol = 0; 20 | int startRow = 0; 21 | int endCol = col - 1; 22 | int endRow = row - 1; 23 | 24 | while (count < total) { 25 | // print first row 26 | for (int i = startCol; i <= endCol; i++) { 27 | System.out.print(arr[startRow][i] + " "); 28 | count++; 29 | } 30 | startRow++; 31 | 32 | // print last column 33 | for (int i = startRow; i <= endRow; i++) { 34 | System.out.print(arr[i][endCol] + " "); 35 | count++; 36 | } 37 | endCol--; 38 | 39 | // print last row 40 | for (int i = endCol; i >= startCol; i--) { 41 | System.out.print(arr[endRow][i] + " "); 42 | count++; 43 | } 44 | endRow--; 45 | // print first column 46 | for (int i = endRow; i >= startRow; i--) { 47 | System.out.print(arr[i][startCol] + " "); 48 | count++; 49 | } 50 | startCol++; 51 | } 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/RainwaterTrapping.java: -------------------------------------------------------------------------------- 1 | package DS_04_Array_and_Strings.Arrays_and_Questions; 2 | 3 | public class RainwaterTrapping { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/StockBuyAndSell1.java: -------------------------------------------------------------------------------- 1 | package DS_04_Array_and_Strings.Arrays_and_Questions; 2 | 3 | public class StockBuyAndSell1 { 4 | // Question 3 : Stock Buy and Sell (part - 1 : one stock at a time) 5 | // leetcode link : 6 | // https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 7 | 8 | public int maxProfit(int[] arr) { 9 | int max = 0; 10 | int minSoFar = arr[0]; 11 | for (int i = 0; i < arr.length; i++) { 12 | minSoFar = Math.min(minSoFar, arr[i]); 13 | int profit = arr[i] - minSoFar; 14 | max = Math.max(max, profit); 15 | } 16 | return max; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/StockBuyAndSell2.java: -------------------------------------------------------------------------------- 1 | package DS_04_Array_and_Strings.Arrays_and_Questions; 2 | 3 | public class StockBuyAndSell2 { 4 | // Question 4 : Stock Buy and Sell (part - 2 : buy any number of stock) 5 | // leetcode link : 6 | // https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 7 | 8 | public int maxProfit(int[] arr) { 9 | int profit = 0; 10 | for (int i = 1; i < arr.length; i++) { 11 | if (arr[i] > arr[i - 1]) { 12 | profit = profit + arr[i] - arr[i - 1]; 13 | } 14 | } 15 | return profit; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/SumOfAllSubArrays.java: -------------------------------------------------------------------------------- 1 | package DS_04_Array_and_Strings.Arrays_and_Questions; 2 | 3 | public class SumOfAllSubArrays { 4 | public static void main(String[] args) { 5 | int[] arr = { 1, 2, 0, 7, 2 }; 6 | sum(arr); 7 | } 8 | 9 | static void sum(int[] arr) { 10 | for (int i = 0; i < arr.length; i++) { 11 | int ans = 0; 12 | for (int j = i; j < arr.length; j++) { 13 | ans = ans + arr[j]; 14 | System.out.println(ans); 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DS_04_Array_and_Strings/Arrays_and_Questions/array.txt: -------------------------------------------------------------------------------- 1 | Majority Elements 2 | Maximum Sum Subarray 3 | Stock Buy And Sell - 1 4 | Stock Buy And Sell - 2 5 | Rainwater trapping problems 6 | 7 | 8 | maximum till i 9 | 10 | sum of all subarrays -------------------------------------------------------------------------------- /DS_05_Binary_Search/binarySearch.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_05_Binary_Search/binarySearch.txt -------------------------------------------------------------------------------- /DS_06_Sorting_Algorithm/sorting.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_06_Sorting_Algorithm/sorting.txt -------------------------------------------------------------------------------- /DS_07_Recursion_And_Backtracking/recursion.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_07_Recursion_And_Backtracking/recursion.txt -------------------------------------------------------------------------------- /DS_08_LinkedList_and_Questions/linkedList.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_08_LinkedList_and_Questions/linkedList.txt -------------------------------------------------------------------------------- /DS_09_Stack_and_Queue_with_Question/stackAndQueue.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_09_Stack_and_Queue_with_Question/stackAndQueue.txt -------------------------------------------------------------------------------- /DS_10_HashMap_and_Questions/HashMap.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_10_HashMap_and_Questions/HashMap.txt -------------------------------------------------------------------------------- /DS_11_Tree_and_Questions/trees.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_11_Tree_and_Questions/trees.txt -------------------------------------------------------------------------------- /DS_12_Heap_and_Questions/heaps.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_12_Heap_and_Questions/heaps.txt -------------------------------------------------------------------------------- /DS_13_Graphs_and_Questions/graphs.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_13_Graphs_and_Questions/graphs.txt -------------------------------------------------------------------------------- /DS_14_Trie_and_Questions/tries.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_14_Trie_and_Questions/tries.txt -------------------------------------------------------------------------------- /DS_15_Sliding_Window_Questions/slidingWindow.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_15_Sliding_Window_Questions/slidingWindow.txt -------------------------------------------------------------------------------- /DS_16_Dyanmic_Programming/dynamicProgramming.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_16_Dyanmic_Programming/dynamicProgramming.txt -------------------------------------------------------------------------------- /DS_17_Greedy_Algorithms/Greedy.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Data-Structues-and-Algorithm-using-Java/f687c298a28c30d7fd13f339d42d099d4ec53554/DS_17_Greedy_Algorithms/Greedy.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structues-and-Algorithm-using-Java 2 | The only repository you need to master Data structure and algorithm. This repo contains questions that are asked in big product based giants. Be ready to land on your dream job. 3 | --------------------------------------------------------------------------------