├── .gitignore ├── 0069-sqrtx ├── NOTES.md └── README.md ├── 0136-single-number ├── NOTES.md ├── 0136-single-number.java └── README.md ├── 0231-power-of-two ├── NOTES.md ├── 0231-power-of-two.java └── README.md ├── 0342-power-of-four ├── NOTES.md ├── 0342-power-of-four.java └── README.md ├── 0412-fizz-buzz ├── NOTES.md ├── 0412-fizz-buzz.java └── README.md ├── 0645-set-mismatch ├── NOTES.md ├── 0645-set-mismatch.java └── README.md ├── 0704-binary-search ├── NOTES.md └── 0704-binary-search.java ├── 0709-to-lower-case ├── NOTES.md └── README.md ├── 0125-valid-palindrome ├── NOTES.md ├── 0125-valid-palindrome.java └── README.md ├── 0162-find-peak-element ├── NOTES.md ├── 0162-find-peak-element.java └── README.md ├── 0169-majority-element ├── NOTES.md ├── 0169-majority-element.java └── README.md ├── 0268-missing-number ├── NOTES.md ├── 0268-missing-number.java └── README.md ├── 0326-power-of-three ├── NOTES.md └── README.md ├── 0344-reverse-string ├── NOTES.md ├── 0344-reverse-string.java └── README.md ├── 0509-fibonacci-number ├── NOTES.md ├── 0509-fibonacci-number.java └── README.md ├── 1470-shuffle-the-array ├── NOTES.md ├── 1470-shuffle-the-array.java └── README.md ├── 2235-add-two-integers ├── NOTES.md └── README.md ├── 0035-search-insert-position ├── NOTES.md └── README.md ├── 0041-first-missing-positive ├── NOTES.md ├── 0041-first-missing-positive.java └── README.md ├── 0058-length-of-last-word ├── NOTES.md └── README.md ├── 0365-water-and-jug-problem ├── NOTES.md ├── 0365-water-and-jug-problem.java └── README.md ├── 1095-find-in-mountain-array ├── NOTES.md ├── 1095-find-in-mountain-array.java └── README.md ├── 1480-running-sum-of-1d-array ├── NOTES.md ├── 1480-running-sum-of-1d-array.java └── README.md ├── 1492-the-kth-factor-of-n ├── NOTES.md ├── 1492-the-kth-factor-of-n.java └── README.md ├── 1512-number-of-good-pairs ├── NOTES.md └── README.md ├── 1572-matrix-diagonal-sum ├── NOTES.md ├── 1572-matrix-diagonal-sum.java └── README.md ├── 1672-richest-customer-wealth ├── NOTES.md ├── 1672-richest-customer-wealth.java └── README.md ├── 1929-concatenation-of-array ├── NOTES.md └── README.md ├── 0374-guess-number-higher-or-lower ├── NOTES.md ├── 0374-guess-number-higher-or-lower.java └── README.md ├── Questions ├── Test.java ├── LeetCode │ └── 1281-subtract-the-product-and-sum-of-digits-of-an-integer │ │ ├── NOTES.md │ │ ├── 1281-subtract-the-product-and-sum-of-digits-of-an-integer.java │ │ └── README.md ├── PrimeNum.java ├── FirstJava │ ├── Currency.java │ ├── Name.java │ ├── EvenOdd.java │ ├── SimpleInterest.java │ ├── Armstrong.java │ └── Palindrome.java ├── conditionsandloops │ ├── AreaCircle.java │ └── AreaTriangle.java ├── AlphabetCaseCheck.java ├── Temperature.java ├── Reverse.java ├── Largest.java ├── Fibonacci.java ├── PrimeNumbers.java ├── BinarySearch │ ├── NthRootOfInteger.cpp │ ├── MedianOfTwoSortedArrays.cpp │ ├── AllocatedMinimumPages.cpp │ └── KthElementOfSortedArrays.cpp ├── Arrays │ ├── nextPermutation.cpp │ └── setMatrixZeroes.cpp ├── Calculator.java ├── CountingOccurences.java └── Backtracking │ ├── MColoringProblem.cpp │ ├── sodukoSolver.cpp │ ├── ratInAMaze.cpp │ └── NQueenProblem.cpp ├── 0033-search-in-rotated-sorted-array ├── NOTES.md └── 0033-search-in-rotated-sorted-array.java ├── 0442-find-all-duplicates-in-an-array ├── NOTES.md ├── 0442-find-all-duplicates-in-an-array.java └── README.md ├── 0852-peak-index-in-a-mountain-array ├── NOTES.md ├── 0852-peak-index-in-a-mountain-array.java └── README.md ├── 1832-check-if-the-sentence-is-pangram ├── NOTES.md ├── 1832-check-if-the-sentence-is-pangram.java └── README.md ├── 0153-find-minimum-in-rotated-sorted-array ├── NOTES.md ├── 0153-find-minimum-in-rotated-sorted-array.java └── README.md ├── 1389-create-target-array-in-the-given-order ├── NOTES.md ├── 1389-create-target-array-in-the-given-order.java └── README.md ├── 0448-find-all-numbers-disappeared-in-an-array ├── NOTES.md ├── 0448-find-all-numbers-disappeared-in-an-array.java └── README.md ├── 0744-find-smallest-letter-greater-than-target ├── NOTES.md └── README.md ├── 1342-number-of-steps-to-reduce-a-number-to-zero ├── NOTES.md ├── 1342-number-of-steps-to-reduce-a-number-to-zero.java └── README.md ├── 1431-kids-with-the-greatest-number-of-candies ├── NOTES.md ├── 1431-kids-with-the-greatest-number-of-candies.java └── README.md ├── 1662-check-if-two-string-arrays-are-equivalent ├── NOTES.md └── README.md ├── 1365-how-many-numbers-are-smaller-than-the-current-number ├── NOTES.md ├── 1365-how-many-numbers-are-smaller-than-the-current-number.java └── README.md ├── 0034-find-first-and-last-position-of-element-in-sorted-array ├── NOTES.md └── README.md ├── 0189-rotate-array ├── NOTES.md └── 0189-rotate-array.java ├── 1920-build-array-from-permutation ├── 1920-build-array-from-permutation.java └── README.md ├── 1732-find-the-highest-altitude ├── 1732-find-the-highest-altitude.java └── README.md ├── 0066-plus-one ├── 0066-plus-one.java └── README.md ├── 0832-flipping-an-image ├── 0832-flipping-an-image.java └── README.md ├── CONTRIBUTING.md ├── 1295-find-numbers-with-even-number-of-digits ├── 1295-find-numbers-with-even-number-of-digits.java └── README.md ├── Algorithms ├── Searching Algorithms │ ├── Linear Search │ │ ├── Linear Search Algorithm.txt │ │ ├── LinearSearch.cpp │ │ └── LinearSearch.java │ ├── Depth-First Search │ │ └── depthfirstsearch.py │ └── Binary Search │ │ └── BinarySearch.java ├── Sorting Algorithms │ ├── Insertion Sort │ │ ├── insertionSort.cpp │ │ ├── insertion_sort.py │ │ └── InsertionSort.java │ ├── Shell Sort │ │ └── shellsort.py │ ├── Cyclic Sort │ │ └── CyclicSort.java │ ├── Selection Sort │ │ └── SelectionSort.java │ ├── Bubble Sort │ │ └── BubbleSort.java │ ├── Merge Sort │ │ └── mergesort.java │ ├── Bucket Sort │ │ └── bucketsort.java │ ├── Heap Sort │ │ └── heapsort.java │ ├── Quick Sort │ │ └── QuickSort.cpp │ └── Radix sort │ │ └── Radix_sort.cpp └── Greedy Algorithms │ └── kruskal-algorithm ├── 1773-count-items-matching-a-rule ├── 1773-count-items-matching-a-rule.java └── README.md ├── 0190-reverse-bits ├── 0190-reverse-bits.java └── README.md ├── README.md ├── 0278-first-bad-version ├── 0278-first-bad-version.java └── README.md ├── 1539-kth-missing-positive-number ├── 1539-kth-missing-positive-number.java └── README.md ├── 0367-valid-perfect-square └── README.md ├── Print 1 To N Without Loop - GFG ├── print-1-to-n-without-loop.java └── README.md ├── 0154-find-minimum-in-rotated-sorted-array-ii ├── 0154-find-minimum-in-rotated-sorted-array-ii.java └── README.md ├── 0410-split-array-largest-sum ├── 0410-split-array-largest-sum.java └── README.md └── 0081-search-in-rotated-sorted-array-ii ├── README.md └── 0081-search-in-rotated-sorted-array-ii.java /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0069-sqrtx/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0136-single-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0231-power-of-two/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0342-power-of-four/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0412-fizz-buzz/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0645-set-mismatch/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0704-binary-search/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0709-to-lower-case/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0125-valid-palindrome/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0162-find-peak-element/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0169-majority-element/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0268-missing-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0326-power-of-three/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0344-reverse-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0509-fibonacci-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1470-shuffle-the-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2235-add-two-integers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0035-search-insert-position/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0041-first-missing-positive/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0058-length-of-last-word/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0365-water-and-jug-problem/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1095-find-in-mountain-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1480-running-sum-of-1d-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1492-the-kth-factor-of-n/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1512-number-of-good-pairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1572-matrix-diagonal-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1672-richest-customer-wealth/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1929-concatenation-of-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0374-guess-number-higher-or-lower/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Questions/Test.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0442-find-all-duplicates-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0852-peak-index-in-a-mountain-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1832-check-if-the-sentence-is-pangram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0153-find-minimum-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1389-create-target-array-in-the-given-order/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0744-find-smallest-letter-greater-than-target/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1342-number-of-steps-to-reduce-a-number-to-zero/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1431-kids-with-the-greatest-number-of-candies/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1662-check-if-two-string-arrays-are-equivalent/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1365-how-many-numbers-are-smaller-than-the-current-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0034-find-first-and-last-position-of-element-in-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Questions/LeetCode/1281-subtract-the-product-and-sum-of-digits-of-an-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Questions/PrimeNum.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | public class PrimeNum { 3 | public static void main(String[] args) { 4 | 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /0189-rotate-array/NOTES.md: -------------------------------------------------------------------------------- 1 | 1,2,3,4,5,6,7 2 | k = 3 3 | after first swap 4 | 7,6,5,4,3,2,1 5 | think as 6 | 7,6,5   4,3,2,1 7 | swap 8 | first part 9 | then second part 10 | 5,6,7,4,3,2,1 11 | 5,6,7,1,2,3,4 -------------------------------------------------------------------------------- /0136-single-number/0136-single-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int num = 0; 4 | for (int elem: nums){ 5 | num = num ^ elem; 6 | } 7 | return num; 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /0509-fibonacci-number/0509-fibonacci-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int fib(int n) { 3 | // this can be solved using dynamic programming but solving now 4 | if (n < 2){ 5 | return n; 6 | } 7 | return fib(n-1) + fib(n-2); 8 | } 9 | } -------------------------------------------------------------------------------- /1920-build-array-from-permutation/1920-build-array-from-permutation.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] buildArray(int[] nums) { 3 | int[] perms = new int[nums.length]; 4 | for(int i = 0; i < nums.length; i++){ 5 | perms[i] = nums[nums[i]]; 6 | 7 | } 8 | return perms; 9 | 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /1480-running-sum-of-1d-array/1480-running-sum-of-1d-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] runningSum(int[] nums) { 3 | int[] resSum = new int[nums.length]; 4 | int sum = 0; 5 | for (int i = 0; i < nums.length; i++){ 6 | sum = sum + nums[i]; 7 | resSum[i] = sum; 8 | } 9 | return resSum; 10 | } 11 | } -------------------------------------------------------------------------------- /1470-shuffle-the-array/1470-shuffle-the-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] shuffle(int[] nums, int n) { 3 | int[] shuffledArray = new int[2*n]; 4 | for(int i = 1; i < 2*n; i = i+2){ 5 | shuffledArray[i-1] = nums[i/2]; 6 | shuffledArray[i] = nums[(2*n+i)/2]; 7 | } 8 | 9 | return shuffledArray; 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /Questions/FirstJava/Currency.java: -------------------------------------------------------------------------------- 1 | package Questions.FirstJava; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Currency { 6 | public static void main(String[] args) { 7 | System.out.println("Enter in rupees"); 8 | Scanner in = new Scanner(System.in); 9 | float rs = in.nextFloat(); 10 | System.out.printf("USD: %.2f\n", (float)(rs/81.97)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /1732-find-the-highest-altitude/1732-find-the-highest-altitude.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int largestAltitude(int[] gain) { 3 | int ground = 0; 4 | int mxm = 0; 5 | for(int i = 0; i < gain.length; i++){ 6 | ground += gain[i]; 7 | 8 | mxm = Math.max(mxm, ground); 9 | 10 | 11 | } 12 | return mxm; 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /Questions/FirstJava/Name.java: -------------------------------------------------------------------------------- 1 | package Questions.FirstJava; 2 | import java.util.Scanner; 3 | 4 | public class Name { 5 | public static void main(String[] args) { 6 | // Take name as input and print a greeting message for that particular name. 7 | Scanner in = new Scanner(System.in); 8 | String name = in.next(); 9 | System.out.println("Hello "+name ); 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Questions/conditionsandloops/AreaCircle.java: -------------------------------------------------------------------------------- 1 | package Questions.conditionsandloops; 2 | 3 | import java.util.Scanner; 4 | 5 | public class AreaCircle { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | float radius = in.nextFloat(); 9 | float area = (float)(Math.PI * radius * radius); 10 | System.out.printf("%.2f\n", area); 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /1572-matrix-diagonal-sum/1572-matrix-diagonal-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int diagonalSum(int[][] mat) { 3 | int sum = 0; 4 | int n = mat.length - 1; 5 | for(int i = 0; i < mat.length; i++){ 6 | sum += mat[i][i]; 7 | if (n != 0){ 8 | sum += mat[i][n + i]; 9 | } 10 | n -= 2; 11 | 12 | 13 | } 14 | return sum; 15 | } 16 | } -------------------------------------------------------------------------------- /0231-power-of-two/0231-power-of-two.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfTwo(int n) { 3 | // there exists an integer x such that x == 2^x 4 | //use recursion 5 | if (n <= 0){ 6 | return false; 7 | } 8 | if (n == 1){ 9 | return true; 10 | 11 | } 12 | if(n % 2 != 0){ 13 | return false; 14 | } 15 | return isPowerOfTwo(n/2); 16 | } 17 | } -------------------------------------------------------------------------------- /1492-the-kth-factor-of-n/1492-the-kth-factor-of-n.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int kthFactor(int n, int k) { 3 | if (k < 1 || k > n) { 4 | return -1; 5 | } 6 | 7 | int count = 0; 8 | 9 | for (int i = 1; i <= n; i++) { 10 | if (n % i == 0) { 11 | count++; 12 | if (count == k) { 13 | return i; 14 | } 15 | } 16 | } 17 | 18 | return -1; 19 | }} 20 | -------------------------------------------------------------------------------- /0066-plus-one/0066-plus-one.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] plusOne(int[] digits) { 3 | for (int i = digits.length - 1; i >= 0; i--) { 4 | digits[i]++; 5 | if(digits[i] < 10){ 6 | return digits; 7 | } 8 | digits[i] = 0; 9 | 10 | } 11 | int[] result = new int[digits.length + 1]; 12 | result[0] = 1; 13 | return result; 14 | 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /0342-power-of-four/0342-power-of-four.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfFour(int n) { 3 | // there exists an integer x such that x == 2^x 4 | //use recursion 5 | if (n <= 0){ 6 | return false; 7 | } 8 | if (n == 1){ 9 | return true; 10 | 11 | } 12 | if(n % 4 != 0){ 13 | return false; 14 | } 15 | return isPowerOfFour(n/4); 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /Questions/AlphabetCaseCheck.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | import java.util.Scanner; 3 | 4 | public class AlphabetCaseCheck { 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | char ch = in.next().trim().charAt(0); 8 | 9 | if(ch >= 'a' && ch <= 'z'){ 10 | System.out.println("Lowercase"); 11 | } 12 | else{ 13 | System.out.println("Uppercase"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Questions/Temperature.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | import java.util.Scanner; 3 | 4 | public class Temperature { 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | System.out.println("Enter number in celsius"); 8 | double celsius = in.nextDouble(); 9 | double fahrenheit = (celsius * 9/5) + 32; 10 | System.out.println("The equivalent value in fahrenheit is "+ (float)fahrenheit); 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /1832-check-if-the-sentence-is-pangram/1832-check-if-the-sentence-is-pangram.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkIfPangram(String sentence) { 3 | String checker = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; 4 | 5 | List list = new ArrayList<>(Arrays.asList(sentence.split(""))); 6 | List arr = new ArrayList<>(Arrays.asList(checker.split(","))); 7 | return list.containsAll(arr); 8 | 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /Questions/Reverse.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | import java.util.Scanner; 3 | 4 | public class Reverse { 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | long num = in.nextInt(); 8 | long rev = 0; 9 | while(num > 0){ 10 | int dig = (int)num % 10; 11 | rev = rev * 10 + dig; 12 | 13 | num = num / 10; 14 | } 15 | System.out.println(rev); 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0162-find-peak-element/0162-find-peak-element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findPeakElement(int[] arr) { 3 | int start = 0; 4 | int end = arr.length -1; 5 | while(start < end){ 6 | int mid = start + (end - start)/2; 7 | if (arr[mid] > arr[mid+1]){ 8 | end = mid; 9 | } 10 | else{ 11 | start = mid+1; 12 | } 13 | } 14 | return start; 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0832-flipping-an-image/0832-flipping-an-image.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] flipAndInvertImage(int[][] image) { 3 | 4 | int n = image.length; 5 | 6 | for(int i = 0; i < n; i++){ 7 | for(int j = 0; j < (n+1)/2; j++){ 8 | 9 | int temp = image[i][j]^1; 10 | image[i][j] = image[i][n - 1 - j] ^1; 11 | image[i][n-1-j]=temp; 12 | 13 | } 14 | } 15 | return image; 16 | } 17 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | #How to contribute to this Repo? 2 | 3 | 4 | ## Step No. 1 5 | 6 | 1. Fork and Star this repo. 7 | 8 | ## Step No. 2 9 | 10 | 2. Clone the repo to your local machine or you can create on gitpod too, and create another branch for it. 11 | 12 | ## Step No. 3 13 | 14 | 3. Write code, review and commit your changes. 15 | 16 | ## Step No. 4 17 | 18 | 4. Push it in your repo and create a pull request. If everything is alright, I will approve or I will let you know. 19 | 20 | Thanks. 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Questions/Largest.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | import java.util.Scanner; 3 | 4 | public class Largest { 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | int a = in.nextInt(); 8 | int b = in.nextInt(); 9 | int c = in.nextInt(); 10 | int max = a; 11 | if ( b > a ){ 12 | max = b; 13 | } 14 | if (c > b) { 15 | max = c; 16 | } 17 | System.out.println(max); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Questions/LeetCode/1281-subtract-the-product-and-sum-of-digits-of-an-integer/1281-subtract-the-product-and-sum-of-digits-of-an-integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subtractProductAndSum(int n) { 3 | int sum = 0; 4 | int dig = 0; 5 | int prod = 1; 6 | while(n>0){ 7 | dig = n%10; 8 | prod = prod * dig; 9 | sum = sum + dig; 10 | n = n/10; 11 | } 12 | int res = prod - sum; 13 | return res; 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int peakIndexInMountainArray(int[] arr) { 3 | int start = 0; 4 | int end = arr.length -1; 5 | while(start < end){ 6 | int mid = start + (end - start)/2; 7 | if (arr[mid] > arr[mid+1]){ 8 | end = mid; 9 | } 10 | else{ 11 | start = mid+1; 12 | } 13 | } 14 | return start; 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /1295-find-numbers-with-even-number-of-digits/1295-find-numbers-with-even-number-of-digits.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | class Solution { 3 | public int findNumbers(int[] nums) { 4 | int dig = 0; 5 | int count = 0; 6 | 7 | for (int i = 0; i< nums.length; i++){ 8 | int counter =(int)Math.log10(nums[i])+1; 9 | if(counter % 2 == 0){ 10 | count ++; 11 | } 12 | } 13 | 14 | 15 | return count; 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /Questions/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | import java.util.Scanner; 3 | 4 | public class Fibonacci{ 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | int n = in.nextInt(); 8 | int a = 0; 9 | int b = 1; 10 | System.out.print(a + " " + b + " "); 11 | for (int i = 2; i < n; i++) { 12 | int c = a + b; 13 | a = b; 14 | b = c; 15 | System.out.print(c + " "); 16 | 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /0412-fizz-buzz/0412-fizz-buzz.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List fizzBuzz(int n) { 3 | List list = new ArrayList<>(10); 4 | for(int i = 1; i <= n; i++){ 5 | String element = ""; 6 | if(i % 3 == 0 && i % 5 == 0){element+="FizzBuzz";} 7 | else if(i % 3 ==0){element+="Fizz";} 8 | else if(i % 5 == 0){element+="Buzz";} 9 | else{element += i;} 10 | list.add(element); 11 | } 12 | return list; 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /1672-richest-customer-wealth/1672-richest-customer-wealth.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumWealth(int[][] accounts) { 3 | int max = Integer.MIN_VALUE; 4 | for(int i = 0; i max){ 10 | max = sum; 11 | } 12 | } 13 | 14 | return max; 15 | 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /0125-valid-palindrome/0125-valid-palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(String s) { 3 | StringBuilder string = new StringBuilder(); 4 | s = s.toLowerCase(); 5 | 6 | String[] arrayString = s.strip().split(" "); 7 | for(int i = 0; i < arrayString.length; i++){ 8 | String cleaned = arrayString[i].replaceAll("[^a-zA-Z0-9]", ""); 9 | string.append(cleaned); 10 | } 11 | return string.toString().equals(string.reverse().toString()); 12 | 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /1389-create-target-array-in-the-given-order/1389-create-target-array-in-the-given-order.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] createTargetArray(int[] nums, int[] index) { 3 | int[] targetArray = new int[nums.length]; 4 | List list = new ArrayList<>(10); 5 | for(int i = 0; i < index.length; i++){ 6 | list.add(index[i], nums[i]); 7 | } 8 | for(int i = 0; i < nums.length; i++){ 9 | targetArray[i] = list.get(i); 10 | } 11 | 12 | return targetArray; 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/Searching Algorithms/Linear Search/Linear Search Algorithm.txt: -------------------------------------------------------------------------------- 1 | For Linear Search we will take an array K 2 | Suppose we wish to search for element a 3 | We will use looping structure to get our output. 4 | 5 | Thus, 6 | 7 | LinearSearch(K,a) 8 | 9 | Step 1: Set i to 1 10 | Step 2: if i > n 11 | move to STEP 7 12 | Step 3: if K[i] = a 13 | go to STEP 6 14 | Step 4: Increment i/i++ 15 | Step 5: Repeat from STEP 2 16 | Step 6: Print a is found at index i 17 | move to STEP 8 18 | Step 7: ELSE 19 | Print element is not found 20 | Step 8: Exit -------------------------------------------------------------------------------- /0365-water-and-jug-problem/0365-water-and-jug-problem.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) { 3 | if (jug1Capacity + jug2Capacity < targetCapacity){ 4 | return false; 5 | } 6 | if (targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0){ 7 | return true; 8 | } 9 | return false; 10 | } 11 | static int gcd(int a, int b){ 12 | if (a == 0){ 13 | return b; 14 | } 15 | return gcd(b%a, a); 16 | } 17 | } -------------------------------------------------------------------------------- /Questions/FirstJava/EvenOdd.java: -------------------------------------------------------------------------------- 1 | package Questions.FirstJava; 2 | 3 | import java.util.Scanner; 4 | 5 | public class EvenOdd { 6 | public static void main(String[] args) { 7 | // Write a program to print whether a number is even or odd, also take input from the user. 8 | Scanner in = new Scanner(System.in); 9 | System.out.println("Enter a number: "); 10 | int num = in.nextInt(); 11 | if (num % 2 != 0){ 12 | System.out.println("Odd"); 13 | } 14 | else{ 15 | System.out.println("Even"); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Algorithms/Searching Algorithms/Depth-First Search/depthfirstsearch.py: -------------------------------------------------------------------------------- 1 | graph = { 2 | '5' : ['3','7'], 3 | '3' : ['2', '4'], 4 | '7' : ['8'], 5 | '2' : [], 6 | '4' : ['8'], 7 | '8' : [] 8 | } 9 | 10 | visited = set() # Set to keep track of visited nodes of graph. 11 | 12 | def dfs(visited, graph, node): #function for dfs 13 | if node not in visited: 14 | print (node) 15 | visited.add(node) 16 | for neighbour in graph[node]: 17 | dfs(visited, graph, neighbour) 18 | 19 | # Driver Code 20 | print("Following is the Depth-First Search") 21 | dfs(visited, graph, '5') 22 | -------------------------------------------------------------------------------- /1365-how-many-numbers-are-smaller-than-the-current-number/1365-how-many-numbers-are-smaller-than-the-current-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] smallerNumbersThanCurrent(int[] nums) { 3 | int[] res = new int[nums.length]; 4 | for (int i = 0; i < nums.length; i++){ 5 | int count = 0; 6 | for(int j = 0; j< nums.length; j++){ 7 | if(nums[j] < nums[i]){ 8 | count++; 9 | } 10 | } 11 | 12 | res[i] = count; 13 | } 14 | 15 | return res; 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /1773-count-items-matching-a-rule/1773-count-items-matching-a-rule.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countMatches(List> items, String ruleKey, String ruleValue) { 3 | int iter = 0; 4 | if (ruleKey.equals("color")){ 5 | iter = 1; 6 | } 7 | else if(ruleKey.equals("name")){ 8 | iter = 2; 9 | } 10 | int count = 0; 11 | for(int i = 0; i < items.size();i++){ 12 | if (items.get(i).get(iter).equals(ruleValue)){ 13 | count++; 14 | } 15 | 16 | } 17 | return count; 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /0344-reverse-string/0344-reverse-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void reverseString(char[] s) { 3 | // As the question mentioned O(1) space complexity, so it couldn't be achieved via recursion, 4 | // just doing for practice. 5 | //solving by two pointer method 6 | recursion(s, 0, s.length-1); 7 | 8 | 9 | } 10 | public void recursion(char[] str, int s, int e){ 11 | if (s > e){ 12 | return; 13 | } 14 | char temp = str[s]; 15 | str[s] = str[e]; 16 | str[e] = temp; 17 | recursion(str, s+1, e-1); 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /0704-binary-search/0704-binary-search.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] arr, int target) { 3 | return recursive(arr, target, 0, arr.length-1); 4 | } 5 | public int recursive(int[] arr, int target, int s, int e){ 6 | //Using recursion 7 | if(s > e){ 8 | return -1; 9 | } 10 | 11 | int m = s + (e-s)/2; 12 | 13 | if (arr[m] == target){ 14 | return m; 15 | } 16 | if(target < arr[m]){ 17 | return recursive(arr, target, s, m-1); 18 | } 19 | return recursive(arr, target, m+1, e); 20 | 21 | } 22 | 23 | 24 | } -------------------------------------------------------------------------------- /0190-reverse-bits/0190-reverse-bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need treat n as an unsigned value 3 | public int reverseBits(int n) { 4 | if (n == 0){ 5 | return 0; 6 | } 7 | int result = 0; 8 | for(int i = 0; i < 32; i++){ 9 | //left shift 10 | result <<= 1; 11 | 12 | //drop 13 | if ((n & 1) == 1){ 14 | 15 | //add 16 | result++; 17 | 18 | } 19 | 20 | //rightshift 21 | n >>= 1; 22 | 23 | } 24 | return result; 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Insertion Sort/insertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void insertionSort(int n, vector &arr){ 5 | 6 | for(int i = 1; i=0; j--) { 10 | 11 | if(arr[j] > temp) { 12 | //shift 13 | arr[j+1] = arr[j]; 14 | } 15 | else { // ruk jao 16 | break; 17 | } 18 | 19 | } 20 | //copy temp value 21 | arr[j+1] = temp; 22 | } 23 | } 24 | 25 | 26 | 27 | 28 | // Contributed by: Jayesh Kumavat -------------------------------------------------------------------------------- /Questions/PrimeNumbers.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | import java.util.Scanner; 3 | 4 | public class PrimeNumbers { 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | int num = in.nextInt(); 8 | System.out.println(IsPrime(num)); 9 | 10 | } 11 | static boolean IsPrime(int number){ 12 | if (number <= 1){ 13 | return false; 14 | } 15 | int i = 2; 16 | while (i * i <= number){ 17 | if (number % i == 0){ 18 | return false; 19 | } 20 | i++; 21 | } 22 | return i * i > number; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DSA-algorithms 2 | 3 | ## Data Structures 4 | 5 | 1. Arrays 6 | 2. Linked Lists 7 | 3. Stacks 8 | 4. Queues 9 | 5. Trees 10 | 6. Graphs 11 | 7. Heaps 12 | 8. Hash Tables 13 | 14 | ## Algorithms 15 | 16 | 1. Sorting Algorithms 17 | 2. Searching Algorithms 18 | 19 | 20 | ## Some Guidelines to this particular repository 👇👇👇 21 | 22 | 23 | 1. You are only allowed to contribute in C, Java, C++, Python. 24 | 2. Your pull request should contain two files, i.e. one for code and another for algorithm/pseudocode. 25 | 3. However in guidline no. 2, you can also contribute algorithms. 26 | 4. See at the repo, don't submit multiple files. 27 | 28 | 29 | LET'S CONTRIBUTE 30 | 31 | THANK YOU 32 | -------------------------------------------------------------------------------- /Questions/conditionsandloops/AreaTriangle.java: -------------------------------------------------------------------------------- 1 | package Questions.conditionsandloops; 2 | 3 | import java.util.Scanner; 4 | 5 | public class AreaTriangle { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | System.out.println("Enter first side: "); 9 | int a = in.nextInt(); 10 | System.out.println("Enter second side: "); 11 | int b = in.nextInt(); 12 | System.out.println("Enter third side: "); 13 | int c = in.nextInt(); 14 | 15 | float s = (a + b + c)/2; 16 | float area = (float)Math.sqrt((double)(s*(s-a)*(s-b)*(s-c))); 17 | System.out.println(area); 18 | 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Shell Sort/shellsort.py: -------------------------------------------------------------------------------- 1 | # Shell sort in python 2 | 3 | 4 | def shellSort(array, n): 5 | 6 | # Rearrange elements at each n/2, n/4, n/8, ... intervals 7 | interval = n // 2 8 | while interval > 0: 9 | for i in range(interval, n): 10 | temp = array[i] 11 | j = i 12 | while j >= interval and array[j - interval] > temp: 13 | array[j] = array[j - interval] 14 | j -= interval 15 | 16 | array[j] = temp 17 | interval //= 2 18 | 19 | 20 | data = [9, 8, 3, 7, 5, 6, 4, 1] 21 | size = len(data) 22 | shellSort(data, size) 23 | print('Sorted Array in Ascending Order:') 24 | print(data) 25 | -------------------------------------------------------------------------------- /Questions/FirstJava/SimpleInterest.java: -------------------------------------------------------------------------------- 1 | package Questions.FirstJava; 2 | import java.util.Scanner; 3 | public class SimpleInterest { 4 | public static void main(String[] args) { 5 | // Write a program to input principal, time, and rate (P, T, R) from the user and find Simple Interest. 6 | Scanner in = new Scanner(System.in); 7 | System.out.println("Enter principal: "); 8 | float principal = in.nextFloat(); 9 | System.out.println("Enter time in years: "); 10 | float time = in.nextFloat(); 11 | System.out.println("Enter rate: "); 12 | float rate = in.nextFloat(); 13 | System.out.println("Interest: " + (principal * rate * time) / 100); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Questions/FirstJava/Armstrong.java: -------------------------------------------------------------------------------- 1 | package Questions.FirstJava; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Armstrong { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int num = in.nextInt(); 9 | int noOfdigits = (int)(Math.log10((double)(num)))+1; 10 | boolean isArmstrong = false; 11 | int sum = 0; 12 | int temp = num; 13 | while (num > 0){ 14 | int dig = num % 10; 15 | sum += Math.pow(dig, noOfdigits); 16 | num = num / 10; 17 | } 18 | if (sum == temp){ 19 | isArmstrong = true; 20 | } 21 | System.out.println(isArmstrong); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0278-first-bad-version/0278-first-bad-version.java: -------------------------------------------------------------------------------- 1 | /* The isBadVersion API is defined in the parent class VersionControl. 2 | boolean isBadVersion(int version); */ 3 | 4 | public class Solution extends VersionControl { 5 | public int firstBadVersion(int n) { 6 | if(n==0) 7 | return 0; 8 | return BinarySearch(n); 9 | 10 | 11 | 12 | } 13 | public int BinarySearch(int n){ 14 | int start = 1; 15 | int end = n; 16 | while(start < end){ 17 | int mid = start + (end - start)/2; 18 | if (isBadVersion(mid)){ 19 | end = mid; 20 | } 21 | else{ 22 | start = mid + 1; 23 | } 24 | } 25 | return start; 26 | } 27 | } -------------------------------------------------------------------------------- /Questions/FirstJava/Palindrome.java: -------------------------------------------------------------------------------- 1 | package Questions.FirstJava; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Palindrome { 6 | public static void main(String[] args) { 7 | System.out.println("Enter a string: "); 8 | Scanner in = new Scanner(System.in); 9 | String str = in.next(); 10 | //solve palindrome by two pointer method in java 11 | boolean isPalindrome = true; 12 | int i = 0; 13 | int j = str.length() - 1; 14 | while(i < j){ 15 | if(str.charAt(i) != str.charAt(j)){ 16 | isPalindrome = false; 17 | break; 18 | } 19 | i++; 20 | j--; 21 | } 22 | System.out.println(isPalindrome); 23 | 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /1539-kth-missing-positive-number/1539-kth-missing-positive-number.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public int findKthPositive(int[] arr, int k) { 3 | //brute force approach 4 | // Adjust k for any missing elements before the first element 5 | int nom = arr[0] - 1; 6 | if (nom >= k) { 7 | return k; 8 | } 9 | 10 | // Traverse the array to find the kth missing element 11 | for (int i = 1; i < arr.length; i++) { 12 | nom += arr[i] - arr[i - 1] - 1; 13 | if (nom >= k) { 14 | // The kth missing element is between arr[i-1] and arr[i] 15 | return arr[i - 1] + (k - (nom - (arr[i] - arr[i - 1] - 1))); 16 | } 17 | } 18 | 19 | // The kth missing element is beyond the last element of the array 20 | return arr[arr.length - 1] + (k - nom); 21 | } 22 | } -------------------------------------------------------------------------------- /Questions/BinarySearch/NthRootOfInteger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | double multiply(double number, int n) { 4 | double ans = 1.0; 5 | for(int i = 1;i<=n;i++) { 6 | ans = ans * number; 7 | } 8 | return ans; 9 | } 10 | 11 | void getNthRoot(int n, int m) { 12 | double low = 1; 13 | double high = m; 14 | double eps = 1e-7; 15 | 16 | while((high - low) > eps) { 17 | double mid = (low + high) / 2.0; 18 | if(multiply(mid, n) < m) { 19 | low = mid; 20 | } 21 | else { 22 | high = mid; 23 | } 24 | } 25 | 26 | cout < findDuplicates(int[] arr) { 3 | int i = 0; 4 | while (i < arr.length) { 5 | int correct = arr[i] - 1; 6 | if (arr[i] != arr[correct]) { 7 | swap(arr, i , correct); 8 | } else { 9 | i++; 10 | } 11 | } 12 | 13 | List ans = new ArrayList<>(); 14 | for (int index = 0; index < arr.length; index++) { 15 | if (arr[index] != index+1) { 16 | ans.add(arr[index]); 17 | } 18 | } 19 | 20 | return ans; 21 | } 22 | 23 | static void swap(int[] arr, int first, int second) { 24 | int temp = arr[first]; 25 | arr[first] = arr[second]; 26 | arr[second] = temp; 27 | } 28 | } -------------------------------------------------------------------------------- /0374-guess-number-higher-or-lower/0374-guess-number-higher-or-lower.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Forward declaration of guess API. 3 | * @param num your guess 4 | * @return -1 if num is higher than the picked number 5 | * 1 if num is lower than the picked number 6 | * otherwise return 0 7 | * int guess(int num); 8 | */ 9 | 10 | public class Solution extends GuessGame { 11 | public int guessNumber(int n) { 12 | int start = 1; 13 | int end = n; 14 | while (start <= end){ 15 | int mid = start + (end - start)/2; 16 | if (guess(mid) == -1){ 17 | end = mid-1; 18 | 19 | } 20 | else if(guess(mid) == 1){ 21 | start = mid +1; 22 | } 23 | else{ 24 | return mid; 25 | } 26 | } 27 | return -2; 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /0645-set-mismatch/0645-set-mismatch.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | class Solution { 3 | 4 | public int[] findErrorNums(int[] arr) { 5 | int i = 0; 6 | while (i < arr.length) { 7 | int correct = arr[i] - 1; 8 | if (arr[i] != arr[correct]) { 9 | swap(arr, i , correct); 10 | } else { 11 | i++; 12 | } 13 | } 14 | 15 | // search for first missing number 16 | for (int index = 0; index < arr.length; index++) { 17 | if (arr[index] != index + 1) { 18 | return new int[] {arr[index], index+1}; 19 | } 20 | } 21 | return new int[] {-1, -1}; 22 | } 23 | 24 | static void swap(int[] arr, int first, int second) { 25 | int temp = arr[first]; 26 | arr[first] = arr[second]; 27 | arr[second] = temp; 28 | } 29 | } -------------------------------------------------------------------------------- /Algorithms/Searching Algorithms/Linear Search/LinearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void linearSearch(int arr[], int size, int search_element) 5 | { 6 | 7 | bool found_element = false; 8 | int index = 0; 9 | 10 | for (int i = 0; i < size; i++) 11 | { 12 | if (arr[i] == search_element) 13 | { 14 | found_element = true; 15 | break; 16 | } 17 | index++; 18 | } 19 | 20 | if (found_element == true) 21 | { 22 | cout << "Number is found at index " << index << endl; 23 | } 24 | else 25 | { 26 | cout << "Number not found" << endl; 27 | } 28 | } 29 | 30 | 31 | int main() 32 | { 33 | 34 | int arr[] = {1, 4, 52, 63, 78, 221, 442}; 35 | int size = sizeof(arr) / sizeof(int); 36 | int search_element = 78; 37 | 38 | linearSearch(arr, size, search_element); 39 | } -------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Insertion Sort/insertion_sort.py: -------------------------------------------------------------------------------- 1 | def insertionSort(a): 2 | # storing the length of the given array or list in a variable. 3 | n = len(a) 4 | 5 | for i in range(1 , n): 6 | # current is storing the i-th element of the list and 7 | # j is pointing to the index before the current index 8 | current = a[i] 9 | j = i-1 10 | 11 | # checking j's boundary condition and 12 | # while the current elemet is smaller than a[j], keep shifting j 13 | # at last place the current element to its correct positon 14 | while j > -1 and current < a[j]: 15 | a[j + 1] = a[j] 16 | j = j - 1 17 | 18 | a[j + 1] = current 19 | 20 | 21 | a = [64, 34, 25, 12, 22, 11, 90] 22 | insertionSort(a) 23 | print("Sorted array : ") 24 | for i in a: 25 | print(i) 26 | 27 | 28 | 29 | # Contributed by: Jayesh Kumavat 30 | -------------------------------------------------------------------------------- /2235-add-two-integers/README.md: -------------------------------------------------------------------------------- 1 |

2235. Add Two Integers

Easy


Given two integers num1 and num2, return the sum of the two integers. 2 |

 

3 |

Example 1:

4 | 5 |
Input: num1 = 12, num2 = 5
 6 | Output: 17
 7 | Explanation: num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: num1 = -10, num2 = 4
13 | Output: -6
14 | Explanation: num1 + num2 = -6, so -6 is returned.
15 | 
16 | 17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • -100 <= num1, num2 <= 100
  • 22 |
23 |
-------------------------------------------------------------------------------- /1342-number-of-steps-to-reduce-a-number-to-zero/1342-number-of-steps-to-reduce-a-number-to-zero.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfSteps(int num) { 3 | // //if num is even divide by 2 4 | // //else subtract by 1 5 | // int count = 0; 6 | // while(num > 0){ 7 | // if(num % 2 == 0){ 8 | // num = num/2; 9 | // } 10 | // else{ 11 | // num--; 12 | // } 13 | // count++; 14 | // } 15 | 16 | // return count; 17 | //previous soln 18 | // recursion soln 19 | // at last 0 + 1 + 1 + 1 +... count 20 | // base condition 21 | if(num == 0){ 22 | return 0; 23 | } 24 | if (num % 2 == 0){ 25 | return 1 + numberOfSteps(num/2); 26 | 27 | } 28 | return 1 + numberOfSteps(num - 1); 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /Questions/Arrays/nextPermutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void nextPermutation(vector& nums) { 4 | int n = nums.size(), k, l; 5 | for (k = n - 2; k >= 0; k--) { 6 | if (nums[k] < nums[k + 1]) { 7 | break; 8 | } 9 | } 10 | if (k < 0) { 11 | reverse(nums.begin(), nums.end()); 12 | } else { 13 | for (l = n - 1; l > k; l--) { 14 | if (nums[l] > nums[k]) { 15 | break; 16 | } 17 | } 18 | swap(nums[k], nums[l]); 19 | reverse(nums.begin() + k + 1, nums.end()); 20 | } 21 | } 22 | 23 | int main() { 24 | vector nums = {1,2,3}; 25 | 26 | nextPermutation(nums);//using in-built function of C++ 27 | 28 | cout<nums.length){ 8 | k = k%nums.length; 9 | } 10 | if (nums.length > 1){swap(nums, 0, nums.length-1); 11 | swap(nums,0, k-1); 12 | swap(nums,k, nums.length-1);} 13 | 14 | } 15 | public void swap(int[] arr, int start, int end){ 16 | 17 | while(start < end){ 18 | int temp = arr[start]; 19 | arr[start] = arr[end]; 20 | arr[end] = temp; 21 | start++; 22 | end--; 23 | } 24 | 25 | 26 | 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /1431-kids-with-the-greatest-number-of-candies/1431-kids-with-the-greatest-number-of-candies.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static List kidsWithCandies(int[] candies, int extraCandies) { 3 | int maximum = max(candies); 4 | List list = new ArrayList(10); 5 | for(int i = 0; i < candies.length; i++){ 6 | candies[i] = candies[i] + extraCandies; 7 | if(candies[i] >= maximum){ 8 | list.add(true); 9 | 10 | } 11 | else{ 12 | list.add(false); 13 | } 14 | } 15 | 16 | return list; 17 | 18 | } 19 | public static int max(int[] candies){ 20 | int max = Integer.MIN_VALUE; 21 | for(int i = 0; i < candies.length; i++){ 22 | if(candies[i] >= max){ 23 | max = candies[i]; 24 | } 25 | } 26 | return max; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0041-first-missing-positive/0041-first-missing-positive.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | class Solution { 3 | public int firstMissingPositive(int[] arr) { 4 | int i = 0; 5 | while (i < arr.length) { 6 | int correct = arr[i] - 1; 7 | if (arr[i] > 0 && arr[i] <= arr.length && arr[i] != arr[correct]) { 8 | swap(arr, i , correct); 9 | } else { 10 | i++; 11 | } 12 | } 13 | // System.out.println(Arrays.toString(arr)); 14 | for (int index = 0; index < arr.length; index++) { 15 | if (arr[index] != index + 1) { 16 | return index+1; 17 | } 18 | 19 | } 20 | 21 | 22 | return arr.length+1; 23 | } 24 | static void swap(int[] arr, int first, int second) { 25 | int temp = arr[first]; 26 | arr[first] = arr[second]; 27 | arr[second] = temp; 28 | } 29 | } -------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/0448-find-all-numbers-disappeared-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findDisappearedNumbers(int[] nums) { 3 | 4 | 5 | int i = 0; 6 | while (i < nums.length) { 7 | int correct = nums[i] - 1; 8 | if (nums[i] != nums[correct]) { 9 | swap(nums, i , correct); 10 | } else { 11 | i++; 12 | } 13 | } 14 | 15 | // just find missing numbers 16 | List ans = new ArrayList<>(); 17 | for (int index = 0; index < nums.length; index++) { 18 | if (nums[index] != index+1) { 19 | ans.add(index + 1); 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | 26 | static void swap(int[] arr, int first, int second) { 27 | int temp = arr[first]; 28 | arr[first] = arr[second]; 29 | arr[second] = temp; 30 | } 31 | } -------------------------------------------------------------------------------- /0709-to-lower-case/README.md: -------------------------------------------------------------------------------- 1 |

709. To Lower Case

Easy


Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "Hello"
 7 | Output: "hello"
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: s = "here"
13 | Output: "here"
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: s = "LOVELY"
19 | Output: "lovely"
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= s.length <= 100
  • 27 |
  • s consists of printable ASCII characters.
  • 28 |
29 |
-------------------------------------------------------------------------------- /0268-missing-number/0268-missing-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int missingNumber(int[] nums) { 3 | //search for the element and go for it. 4 | int index = 0; 5 | sort(nums); 6 | 7 | for (int i = 0; i < nums.length;i++){ 8 | if (nums[i] == 0){ 9 | index = i + 1; 10 | 11 | } 12 | } 13 | return index; 14 | } 15 | static void sort(int[] nums){ 16 | 17 | int i = 0; 18 | while (i < nums.length) { 19 | 20 | int correct = nums[i] - 1; 21 | if(correct == -1){ 22 | i++; 23 | continue; 24 | 25 | } 26 | if (nums[i] != nums[correct]){ 27 | int temp = nums[i]; 28 | nums[i] = nums[correct]; 29 | nums[correct] = temp; 30 | 31 | } 32 | else { 33 | i++; 34 | } 35 | 36 | 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /0169-majority-element/0169-majority-element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int majorityElement(int[] nums) { 3 | quickSort(nums, 0, nums.length-1); 4 | return nums[(nums.length-1)/2]; 5 | } 6 | public void quickSort(int[] nums, int low, int hi){ 7 | if (low >= hi){ 8 | return; 9 | } 10 | int s = low; 11 | int e = hi; 12 | int mid = s + (e-s)/2; 13 | int pivot = nums[mid]; 14 | //putting the pivot at correct index 15 | while(s <= e){ 16 | while (nums[s] < pivot){ 17 | s++; 18 | } 19 | while(nums[e] > pivot){ 20 | e--; 21 | } 22 | if (s <= e){ 23 | int temp = nums[s]; 24 | nums[s] = nums[e]; 25 | nums[e] = temp; 26 | s++; 27 | e--; 28 | } 29 | } 30 | quickSort(nums, low, e); 31 | quickSort(nums, s, hi); 32 | 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Questions/Calculator.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | import java.util.Scanner; 3 | 4 | public class Calculator { 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | char choice = 'x'; 8 | do{ 9 | System.out.println("Enter first number: "); 10 | int a = in.nextInt(); 11 | System.out.println("Enter second number: "); 12 | int b = in.nextInt(); 13 | System.out.println("Additon: " + (a+b)); 14 | System.out.println("Subtraction: "+ (a-b)); 15 | System.out.println("Multiplication: "+ (a*b)); 16 | if (b != 0){ 17 | System.out.println("Division: "+ (a/b)); 18 | System.out.println("Remainder"+ (a%b)); 19 | } 20 | else{ 21 | System.out.println("Cannot divide by zero"); 22 | } 23 | System.out.println("Enter choice: "); 24 | choice = in.next().trim().charAt(0); 25 | 26 | }while(choice != 'x'); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Algorithms/Searching Algorithms/Linear Search/LinearSearch.java: -------------------------------------------------------------------------------- 1 | // package com.company; 2 | 3 | public class LinearSearch { 4 | public static void main(String[] args) { 5 | int[] nums = {23, 45, 1,2,3,5, 8, 7,34,63}; 6 | int target = 8; 7 | int ans = linearsearch(nums, target); 8 | System.out.println(ans); 9 | 10 | 11 | } 12 | 13 | // search in the array: the return the index if item found 14 | //otherwise if item not found return -1 15 | static int linearsearch(int[] arr, int target) { 16 | if (arr.length == 0){ 17 | return -1; 18 | 19 | } 20 | for(int index = 0; index< arr.length; index++){ 21 | //check for element at every index if it is target 22 | int element = arr[index]; 23 | if(element == target){ 24 | return index; 25 | } 26 | } 27 | //this line will execute if none of the return statements above have executed, target not found 28 | return -1; 29 | } 30 | 31 | } 32 | 33 | // Credit to Kunal Kushwaha 34 | -------------------------------------------------------------------------------- /Questions/CountingOccurences.java: -------------------------------------------------------------------------------- 1 | package Questions; 2 | import java.util.Scanner; 3 | 4 | public class CountingOccurences { 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | System.out.println("Enter a big number: "); 8 | long num = in.nextInt(); 9 | System.out.println(); 10 | System.out.println("Enter target: "); 11 | int target = in.nextInt(); 12 | int count = 0; 13 | while (num > 0){ 14 | int digit = (int)num % 10; 15 | if (digit == target){ 16 | count++; 17 | } 18 | num = num / 10; 19 | } 20 | System.out.println(count); 21 | //convert long to string 22 | // String str = Long.toString(num); 23 | // int count = 0; 24 | // for (int i = 0; i < str.length(); i++) { 25 | // if(Character.getNumericValue(str.charAt(i)) == target){ 26 | // count++; 27 | // } 28 | // } 29 | // System.out.println(count); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0342-power-of-four/README.md: -------------------------------------------------------------------------------- 1 |

342. Power of Four

Easy


Given an integer n, return true if it is a power of four. Otherwise, return false.

2 | 3 |

An integer n is a power of four, if there exists an integer x such that n == 4x.

4 | 5 |

 

6 |

Example 1:

7 |
Input: n = 16
 8 | Output: true
 9 | 

Example 2:

10 |
Input: n = 5
11 | Output: false
12 | 

Example 3:

13 |
Input: n = 1
14 | Output: true
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • -231 <= n <= 231 - 1
  • 21 |
22 | 23 |

 

24 | Follow up: Could you solve it without loops/recursion?
-------------------------------------------------------------------------------- /Questions/LeetCode/1281-subtract-the-product-and-sum-of-digits-of-an-integer/README.md: -------------------------------------------------------------------------------- 1 |

1281. Subtract the Product and Sum of Digits of an Integer

Easy


Given an integer number n, return the difference between the product of its digits and the sum of its digits. 2 |

 

3 |

Example 1:

4 | 5 |
Input: n = 234
 6 | Output: 15 
 7 | Explanation: 
 8 | Product of digits = 2 * 3 * 4 = 24 
 9 | Sum of digits = 2 + 3 + 4 = 9 
10 | Result = 24 - 9 = 15
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: n = 4421
16 | Output: 21
17 | Explanation: 
18 | Product of digits = 4 * 4 * 2 * 1 = 32 
19 | Sum of digits = 4 + 4 + 2 + 1 = 11 
20 | Result = 32 - 11 = 21
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • 1 <= n <= 10^5
  • 28 |
29 |
-------------------------------------------------------------------------------- /0344-reverse-string/README.md: -------------------------------------------------------------------------------- 1 |

344. Reverse String

Easy


Write a function that reverses a string. The input string is given as an array of characters s.

2 | 3 |

You must do this by modifying the input array in-place with O(1) extra memory.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = ["h","e","l","l","o"]
 8 | Output: ["o","l","l","e","h"]
 9 | 

Example 2:

10 |
Input: s = ["H","a","n","n","a","h"]
11 | Output: ["h","a","n","n","a","H"]
12 | 
13 |

 

14 |

Constraints:

15 | 16 | 20 |
-------------------------------------------------------------------------------- /0169-majority-element/README.md: -------------------------------------------------------------------------------- 1 |

169. Majority Element

Easy


Given an array nums of size n, return the majority element.

2 | 3 |

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [3,2,3]
 8 | Output: 3
 9 | 

Example 2:

10 |
Input: nums = [2,2,1,1,1,2,2]
11 | Output: 2
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • n == nums.length
  • 18 |
  • 1 <= n <= 5 * 104
  • 19 |
  • -109 <= nums[i] <= 109
  • 20 |
21 | 22 |

 

23 | Follow-up: Could you solve the problem in linear time and in O(1) space?
-------------------------------------------------------------------------------- /Questions/BinarySearch/MedianOfTwoSortedArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | float median(int nums1[],int nums2[],int m,int n) { 5 | int finalArray[n+m]; 6 | int i=0,j=0,k=0; 7 | while(i arr[mid]){ 26 | start = mid +1; 27 | }else{ 28 | //ans found 29 | return mid; 30 | 31 | } 32 | 33 | } 34 | return -1; 35 | } 36 | } 37 | 38 | // Credit to Kunal Kushwaha 39 | -------------------------------------------------------------------------------- /0153-find-minimum-in-rotated-sorted-array/0153-find-minimum-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMin(int[] nums) { 3 | // 4 cases 4 | // 1st: left of pivot 5 | // 2nd: right of target 6 | // 3rd: target 7 | // 4th: pivot 8 | int start = 0; 9 | int end = nums.length-1; 10 | // int tempstart = start; 11 | // int tempend = end; 12 | while(start < end){ 13 | int mid = start + (end - start) /2; 14 | int b = mid-1; 15 | int a = mid +1; 16 | b = b < 0? mid:b; 17 | a = a > nums.length-1? mid: a; 18 | 19 | 20 | if (nums[a] < nums[mid]){ 21 | return nums[a]; 22 | } 23 | else if(nums[b] > nums[mid]){ 24 | return nums[mid]; 25 | } 26 | else if(nums[mid] >= nums[0] && !(nums[0] < nums[nums.length-1])){ 27 | start = mid + 1; 28 | } 29 | else{ 30 | end = mid -1; 31 | } 32 | } 33 | return nums[start]; 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /Questions/Arrays/setMatrixZeroes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void setZeroes(vector < vector < int >> & matrix) { 4 | int col0 = 1, rows = matrix.size(), cols = matrix[0].size(); 5 | for (int i = 0; i < rows; i++) { 6 | 7 | if (matrix[i][0] == 0) col0 = 0; 8 | for (int j = 1; j < cols; j++) { 9 | if (matrix[i][j] == 0) { 10 | matrix[i][0] = 0; 11 | matrix[0][j] = 0; 12 | } 13 | } 14 | } 15 | 16 | for (int i = rows - 1; i >= 0; i--) { 17 | for (int j = cols - 1; j >= 1; j--) { 18 | if (matrix[i][0] == 0 || matrix[0][j] == 0) { 19 | matrix[i][j] = 0; 20 | } 21 | } 22 | if (col0 == 0) { 23 | matrix[i][0] = 0; 24 | } 25 | 26 | } 27 | 28 | } 29 | 30 | int main() { 31 | vector < vector < int >> arr; 32 | arr = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}}; 33 | setZeroes(arr); 34 | cout<<"The Final Matrix is "<136. Single Number

Easy


Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

2 | 3 |

You must implement a solution with a linear runtime complexity and use only constant extra space.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [2,2,1]
 8 | Output: 1
 9 | 

Example 2:

10 |
Input: nums = [4,1,2,1,2]
11 | Output: 4
12 | 

Example 3:

13 |
Input: nums = [1]
14 | Output: 1
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= nums.length <= 3 * 104
  • 21 |
  • -3 * 104 <= nums[i] <= 3 * 104
  • 22 |
  • Each element in the array appears twice except for one element which appears only once.
  • 23 |
24 |
-------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/README.md: -------------------------------------------------------------------------------- 1 |

448. Find All Numbers Disappeared in an Array

Easy


Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

2 | 3 |

 

4 |

Example 1:

5 |
Input: nums = [4,3,2,7,8,2,3,1]
 6 | Output: [5,6]
 7 | 

Example 2:

8 |
Input: nums = [1,1]
 9 | Output: [2]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • n == nums.length
  • 16 |
  • 1 <= n <= 105
  • 17 |
  • 1 <= nums[i] <= n
  • 18 |
19 | 20 |

 

21 |

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

22 |
-------------------------------------------------------------------------------- /0069-sqrtx/README.md: -------------------------------------------------------------------------------- 1 |

69. Sqrt(x)

Easy


Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

2 | 3 |

You must not use any built-in exponent function or operator.

4 | 5 |
    6 |
  • For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
  • 7 |
8 | 9 |

 

10 |

Example 1:

11 | 12 |
Input: x = 4
13 | Output: 2
14 | Explanation: The square root of 4 is 2, so we return 2.
15 | 
16 | 17 |

Example 2:

18 | 19 |
Input: x = 8
20 | Output: 2
21 | Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 0 <= x <= 231 - 1
  • 29 |
30 |
-------------------------------------------------------------------------------- /0367-valid-perfect-square/README.md: -------------------------------------------------------------------------------- 1 |

367. Valid Perfect Square

Easy


Given a positive integer num, return true if num is a perfect square or false otherwise.

2 | 3 |

A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.

4 | 5 |

You must not use any built-in library function, such as sqrt.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: num = 16
11 | Output: true
12 | Explanation: We return true because 4 * 4 = 16 and 4 is an integer.
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: num = 14
18 | Output: false
19 | Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= num <= 231 - 1
  • 27 |
28 |
-------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Selection Sort/SelectionSort.java: -------------------------------------------------------------------------------- 1 | // package com.company; 2 | 3 | import java.util.Arrays; 4 | 5 | public class SelectionSort { 6 | public static void main(String[] args) { 7 | int[] arr = {-23, -32, 0, 78, -3, 45, 0, 67}; 8 | selectionSort(arr); 9 | System.out.println(Arrays.toString(arr)); 10 | 11 | } 12 | static void selectionSort(int[] arr){ 13 | for (int i = 0; i < arr.length; i++) { 14 | //find the max item in the remaining array and swap with correct index 15 | int last = arr.length-i-1; 16 | int maxIndex = getMaxIndex(arr, 0, last); 17 | swap(arr, maxIndex, last); 18 | } 19 | 20 | 21 | } 22 | static void swap(int[] arr, int first, int second){ 23 | int temp = arr[first]; 24 | arr[first] = arr[second]; 25 | arr[second] = temp; 26 | 27 | } 28 | 29 | static int getMaxIndex(int[] arr, int start, int end) { 30 | int max = start; 31 | for (int i = start; i <= end ; i++) { 32 | if(arr[max] < arr[i]){ 33 | max = i; 34 | } 35 | 36 | } 37 | return max; 38 | } 39 | } 40 | 41 | // Credit to Kunal Kushwaha -------------------------------------------------------------------------------- /0231-power-of-two/README.md: -------------------------------------------------------------------------------- 1 |

231. Power of Two

Easy


Given an integer n, return true if it is a power of two. Otherwise, return false.

2 | 3 |

An integer n is a power of two, if there exists an integer x such that n == 2x.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: n = 1
 9 | Output: true
10 | Explanation: 20 = 1
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: n = 16
16 | Output: true
17 | Explanation: 24 = 16
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: n = 3
23 | Output: false
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • -231 <= n <= 231 - 1
  • 31 |
32 | 33 |

 

34 | Follow up: Could you solve it without loops/recursion?
-------------------------------------------------------------------------------- /1832-check-if-the-sentence-is-pangram/README.md: -------------------------------------------------------------------------------- 1 |

1832. Check if the Sentence Is Pangram

Easy


A pangram is a sentence where every letter of the English alphabet appears at least once.

2 | 3 |

Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
 9 | Output: true
10 | Explanation: sentence contains at least one of every letter of the English alphabet.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: sentence = "leetcode"
16 | Output: false
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= sentence.length <= 1000
  • 24 |
  • sentence consists of lowercase English letters.
  • 25 |
26 |
-------------------------------------------------------------------------------- /0645-set-mismatch/README.md: -------------------------------------------------------------------------------- 1 |

645. Set Mismatch

Easy


You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.

2 | 3 |

You are given an integer array nums representing the data status of this set after the error.

4 | 5 |

Find the number that occurs twice and the number that is missing and return them in the form of an array.

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [1,2,2,4]
10 | Output: [2,3]
11 | 

Example 2:

12 |
Input: nums = [1,1]
13 | Output: [1,2]
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 2 <= nums.length <= 104
  • 20 |
  • 1 <= nums[i] <= 104
  • 21 |
22 |
-------------------------------------------------------------------------------- /1512-number-of-good-pairs/README.md: -------------------------------------------------------------------------------- 1 |

1512. Number of Good Pairs

Easy


Given an array of integers nums, return the number of good pairs.

2 | 3 |

A pair (i, j) is called good if nums[i] == nums[j] and i < j.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,2,3,1,1,3]
 9 | Output: 4
10 | Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [1,1,1,1]
16 | Output: 6
17 | Explanation: Each pair in the array are good.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: nums = [1,2,3]
23 | Output: 0
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 1 <= nums.length <= 100
  • 31 |
  • 1 <= nums[i] <= 100
  • 32 |
33 |
-------------------------------------------------------------------------------- /1480-running-sum-of-1d-array/README.md: -------------------------------------------------------------------------------- 1 |

1480. Running Sum of 1d Array

Easy


Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

2 | 3 |

Return the running sum of nums.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,2,3,4]
 9 | Output: [1,3,6,10]
10 | Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
11 | 12 |

Example 2:

13 | 14 |
Input: nums = [1,1,1,1,1]
15 | Output: [1,2,3,4,5]
16 | Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
17 | 18 |

Example 3:

19 | 20 |
Input: nums = [3,1,2,10,1]
21 | Output: [3,4,6,16,17]
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 1000
  • 29 |
  • -10^6 <= nums[i] <= 10^6
  • 30 |
-------------------------------------------------------------------------------- /Print 1 To N Without Loop - GFG/print-1-to-n-without-loop.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for Java 3 | 4 | /*package whatever //do not write package name here */ 5 | 6 | import java.io.*; 7 | import java.util.*; 8 | class Main { 9 | public static void main (String[] args) { 10 | 11 | //taking input using Scanner class 12 | Scanner sc=new Scanner(System.in); 13 | 14 | //taking total testcases 15 | int T=sc.nextInt(); 16 | while(T-->0) 17 | { 18 | //creating an object of class Print 19 | Solution obj=new Solution(); 20 | int N; 21 | 22 | //input N 23 | N=sc.nextInt(); 24 | 25 | //calling printNos() methdo 26 | //of class Print 27 | obj.printNos(N); 28 | System.out.println(); 29 | 30 | } 31 | 32 | } 33 | } 34 | 35 | 36 | // } Driver Code Ends 37 | 38 | 39 | //User function Template for Java 40 | 41 | 42 | 43 | class Solution 44 | { 45 | 46 | public void printNos(int N) 47 | { 48 | // if (N > 0){ 49 | // printNos(N-1); 50 | // System.out.print(N + " "); 51 | // } 52 | if (N == 0){ 53 | return; 54 | } 55 | printNos(N -1); 56 | System.out.print(N + " "); 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Questions/BinarySearch/AllocatedMinimumPages.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace std; 5 | //to check if allocation of books among given students is possible. 6 | int isPossible(vector < int > & A, int pages, int students) { 7 | int cnt = 0; 8 | int sumAllocated = 0; 9 | for (int i = 0; i < A.size(); i++) { 10 | if (sumAllocated + A[i] > pages) { 11 | cnt++; 12 | sumAllocated = A[i]; 13 | if (sumAllocated > pages) return false; 14 | } else { 15 | sumAllocated += A[i]; 16 | } 17 | } 18 | if (cnt < students) return true; 19 | return false; 20 | } 21 | int books(vector < int > & A, int B) { 22 | if (B > A.size()) return -1; 23 | int low = A[0]; 24 | int high = 0; 25 | //to find minimum value and sum of all pages 26 | for (int i = 0; i < A.size(); i++) { 27 | high = high + A[i]; 28 | low = min(low, A[i]); 29 | } 30 | //binary search 31 | while (low <= high) { 32 | int mid = (low + high) >> 1; 33 | if (isPossible(A, mid, B)) { 34 | high = mid - 1; 35 | } else { 36 | low = mid + 1; 37 | } 38 | } 39 | return low; 40 | } 41 | int main() { 42 | vector A = {12,34,67,90}; 43 | int B = 2; 44 | cout << "Minimum Possible Number is " << books(A, B); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Bubble Sort/BubbleSort.java: -------------------------------------------------------------------------------- 1 | // package com.company; 2 | 3 | import java.util.Arrays; 4 | 5 | public class BubbleSort { 6 | public static void main(String[] args) { 7 | int[] arr = {-23, -32, 0, 78, -3, 45, 0, 67}; 8 | BubbleSort(arr); 9 | System.out.println(Arrays.toString(arr)); 10 | 11 | 12 | } 13 | static void BubbleSort(int[] arr){ 14 | boolean swapped; 15 | // run the steps n - 1 times 16 | for (int i = 0; i < arr.length; i++) { 17 | swapped =false; 18 | 19 | // for each step, max item will come at the last respective index 20 | for (int j = 1; j < arr.length - i; j++) { 21 | // swap if the item is smaller than the previous item 22 | if (arr[j] < arr[j-1]){ 23 | //swap 24 | int temp = arr[j]; 25 | arr[j] = arr[j-1]; 26 | arr[j-1] = temp; 27 | swapped = true; 28 | } 29 | 30 | } 31 | // if you did not swap for a particular value of i, it means the array is sorted hence stop the program 32 | if(!swapped){ 33 | break; 34 | } 35 | } 36 | } 37 | } 38 | 39 | // Credit to Kunal Kushwaha 40 | -------------------------------------------------------------------------------- /Questions/BinarySearch/KthElementOfSortedArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int kthelement(int arr1[], int arr2[], int m, int n, int k) { 4 | if(m > n) { 5 | return kthelement(arr2, arr1, n, m, k); 6 | } 7 | 8 | int low = max(0,k-m), high = min(k,n); 9 | 10 | while(low <= high) { 11 | int cut1 = (low + high) >> 1; 12 | int cut2 = k - cut1; 13 | int l1 = cut1 == 0 ? INT_MIN : arr1[cut1 - 1]; 14 | int l2 = cut2 == 0 ? INT_MIN : arr2[cut2 - 1]; 15 | int r1 = cut1 == n ? INT_MAX : arr1[cut1]; 16 | int r2 = cut2 == m ? INT_MAX : arr2[cut2]; 17 | 18 | if(l1 <= r2 && l2 <= r1) { 19 | return max(l1, l2); 20 | } 21 | else if (l1 > r2) { 22 | high = cut1 - 1; 23 | } 24 | else { 25 | low = cut1 + 1; 26 | } 27 | } 28 | return 1; 29 | } 30 | int main() { 31 | int array1[] = {2,3,6,7,9}; 32 | int array2[] = {1,4,8,10}; 33 | int m = sizeof(array1)/sizeof(array1[0]); 34 | int n = sizeof(array2)/sizeof(array2[0]); 35 | int k = 5; 36 | cout<<"The element at the kth position in the final sorted array is " 37 | < nums.length-1? mid: a; 18 | 19 | 20 | if (nums[a] < nums[mid]){ 21 | return nums[a]; 22 | } 23 | else if(nums[b] > nums[mid]){ 24 | return nums[mid]; 25 | } 26 | else if(nums[mid] == nums[start] && nums[mid] == nums[end]){ 27 | start++; 28 | end--; 29 | 30 | } 31 | else if(nums[mid] >= nums[start] && !(nums[start] < nums[end])){ 32 | start = mid + 1; 33 | } 34 | else{ 35 | end = mid -1; 36 | } 37 | } 38 | return nums[start]; 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /0509-fibonacci-number/README.md: -------------------------------------------------------------------------------- 1 |

509. Fibonacci Number

Easy


The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

2 | 3 |
F(0) = 0, F(1) = 1
 4 | F(n) = F(n - 1) + F(n - 2), for n > 1.
 5 | 
6 | 7 |

Given n, calculate F(n).

8 | 9 |

 

10 |

Example 1:

11 | 12 |
Input: n = 2
13 | Output: 1
14 | Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
15 | 
16 | 17 |

Example 2:

18 | 19 |
Input: n = 3
20 | Output: 2
21 | Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
22 | 
23 | 24 |

Example 3:

25 | 26 |
Input: n = 4
27 | Output: 3
28 | Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
29 | 
30 | 31 |

 

32 |

Constraints:

33 | 34 |
    35 |
  • 0 <= n <= 30
  • 36 |
37 |
-------------------------------------------------------------------------------- /0041-first-missing-positive/README.md: -------------------------------------------------------------------------------- 1 |

41. First Missing Positive

Hard


Given an unsorted integer array nums, return the smallest missing positive integer.

2 | 3 |

You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,2,0]
 9 | Output: 3
10 | Explanation: The numbers in the range [1,2] are all in the array.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [3,4,-1,1]
16 | Output: 2
17 | Explanation: 1 is in the array but 2 is missing.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: nums = [7,8,9,11,12]
23 | Output: 1
24 | Explanation: The smallest positive integer 1 is missing.
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= nums.length <= 105
  • 32 |
  • -231 <= nums[i] <= 231 - 1
  • 33 |
34 |
-------------------------------------------------------------------------------- /0326-power-of-three/README.md: -------------------------------------------------------------------------------- 1 |

326. Power of Three

Easy


Given an integer n, return true if it is a power of three. Otherwise, return false.

2 | 3 |

An integer n is a power of three, if there exists an integer x such that n == 3x.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: n = 27
 9 | Output: true
10 | Explanation: 27 = 33
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: n = 0
16 | Output: false
17 | Explanation: There is no x where 3x = 0.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: n = -1
23 | Output: false
24 | Explanation: There is no x where 3x = (-1).
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • -231 <= n <= 231 - 1
  • 32 |
33 | 34 |

 

35 | Follow up: Could you solve it without loops/recursion?
-------------------------------------------------------------------------------- /1295-find-numbers-with-even-number-of-digits/README.md: -------------------------------------------------------------------------------- 1 |

1295. Find Numbers with Even Number of Digits

Easy


Given an array nums of integers, return how many of them contain an even number of digits.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums = [12,345,2,6,7896]
 7 | Output: 2
 8 | Explanation: 
 9 | 12 contains 2 digits (even number of digits). 
10 | 345 contains 3 digits (odd number of digits). 
11 | 2 contains 1 digit (odd number of digits). 
12 | 6 contains 1 digit (odd number of digits). 
13 | 7896 contains 4 digits (even number of digits). 
14 | Therefore only 12 and 7896 contain an even number of digits.
15 | 
16 | 17 |

Example 2:

18 | 19 |
Input: nums = [555,901,482,1771]
20 | Output: 1 
21 | Explanation: 
22 | Only 1771 contains an even number of digits.
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= nums.length <= 500
  • 30 |
  • 1 <= nums[i] <= 105
  • 31 |
32 |
-------------------------------------------------------------------------------- /0412-fizz-buzz/README.md: -------------------------------------------------------------------------------- 1 |

412. Fizz Buzz

Easy


Given an integer n, return a string array answer (1-indexed) where:

2 | 3 |
    4 |
  • answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
  • 5 |
  • answer[i] == "Fizz" if i is divisible by 3.
  • 6 |
  • answer[i] == "Buzz" if i is divisible by 5.
  • 7 |
  • answer[i] == i (as a string) if none of the above conditions are true.
  • 8 |
9 | 10 |

 

11 |

Example 1:

12 |
Input: n = 3
13 | Output: ["1","2","Fizz"]
14 | 

Example 2:

15 |
Input: n = 5
16 | Output: ["1","2","Fizz","4","Buzz"]
17 | 

Example 3:

18 |
Input: n = 15
19 | Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
20 | 
21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= n <= 104
  • 26 |
27 |
-------------------------------------------------------------------------------- /0035-search-insert-position/README.md: -------------------------------------------------------------------------------- 1 |

35. Search Insert Position

Easy


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.

2 | 3 |

You must write an algorithm with O(log n) runtime complexity.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,3,5,6], target = 5
 9 | Output: 2
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: nums = [1,3,5,6], target = 2
15 | Output: 1
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: nums = [1,3,5,6], target = 7
21 | Output: 4
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 104
  • 29 |
  • -104 <= nums[i] <= 104
  • 30 |
  • nums contains distinct values sorted in ascending order.
  • 31 |
  • -104 <= target <= 104
  • 32 |
33 |
-------------------------------------------------------------------------------- /Print 1 To N Without Loop - GFG/README.md: -------------------------------------------------------------------------------- 1 | # Print 1 To N Without Loop 2 | ## Easy 3 |

Print numbers from 1 to N without the help of loops.

4 | 5 |

Example 1:

6 | 7 |
Input:
 8 | N = 10
 9 | Output: 1 2 3 4 5 6 7 8 9 10
10 | 
11 | 12 |


13 | Example 2:

14 | 15 |
Input:
16 | N = 5
17 | Output: 1 2 3 4 5
18 | 19 |

 

20 | 21 |

Your Task:
22 | This is a function problem. You only need to complete the function printNos() that takes N as parameter and prints number from 1 to N recursively. Don't print newline, it will be added by the driver code.

23 | 24 |


25 | Expected Time Complexity: O(N).
26 | Expected Auxiliary Space: O(N) (Recursive).

27 | 28 |


29 | Constraints:
30 | 1 <= N <= 1000

31 |
-------------------------------------------------------------------------------- /0442-find-all-duplicates-in-an-array/README.md: -------------------------------------------------------------------------------- 1 |

442. Find All Duplicates in an Array

Medium


Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.

2 | 3 |

You must write an algorithm that runs in O(n) time and uses only constant extra space.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [4,3,2,7,8,2,3,1]
 8 | Output: [2,3]
 9 | 

Example 2:

10 |
Input: nums = [1,1,2]
11 | Output: [1]
12 | 

Example 3:

13 |
Input: nums = [1]
14 | Output: []
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • n == nums.length
  • 21 |
  • 1 <= n <= 105
  • 22 |
  • 1 <= nums[i] <= n
  • 23 |
  • Each element in nums appears once or twice.
  • 24 |
25 |
-------------------------------------------------------------------------------- /Questions/Backtracking/MColoringProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | bool check(int node, int color[], bool graph[101][101], int n, int col) { 5 | for (int k = 0; k < n; k++) { 6 | if (k != node && graph[k][node] == 1 && color[k] == col) { 7 | return false; 8 | } 9 | } 10 | return true; 11 | } 12 | bool solve(int node, int color[], int m, int N, bool graph[101][101]) { 13 | if (node == N) { 14 | return true; 15 | } 16 | 17 | for (int i = 1; i <= m; i++) { 18 | if (check(node, color, graph, N, i)) { 19 | color[node] = i; 20 | if (solve(node + 1, color, m, N, graph)) return true; 21 | color[node] = 0; 22 | } 23 | 24 | } 25 | return false; 26 | } 27 | 28 | bool graphColoring(bool graph[101][101], int m, int N) { 29 | int color[N] = { 30 | 0 31 | }; 32 | if (solve(0, color, m, N, graph)) return true; 33 | return false; 34 | } 35 | 36 | int main() { 37 | int N = 4; 38 | int m = 3; 39 | 40 | bool graph[101][101]; 41 | memset(graph, false, sizeof graph); 42 | 43 | // Edges are (0, 1), (1, 2), (2, 3), (3, 0), (0, 2) 44 | graph[0][1] = 1; graph[1][0] = 1; 45 | graph[1][2] = 1; graph[2][1] = 1; 46 | graph[2][3] = 1; graph[3][2] = 1; 47 | graph[3][0] = 1; graph[0][3] = 1; 48 | graph[0][2] = 1; graph[2][0] = 1; 49 | 50 | cout << graphColoring(graph, m, N); 51 | 52 | } -------------------------------------------------------------------------------- /0410-split-array-largest-sum/0410-split-array-largest-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int splitArray(int[] nums, int m) { 3 | 4 | int start = 0; 5 | int end = 0; 6 | for (int i = 0; i < nums.length; i++) { 7 | start = Math.max(start, nums[i]); // in the end of the loop this will contain the max item from the array 8 | end += nums[i]; 9 | 10 | } 11 | //binary search 12 | while(start < end){ 13 | // try for the middle as potential ans 14 | int mid = start + (end - start) / 2; 15 | // calculate how many pieces you can divide this in with this max sum 16 | int sum = 0; 17 | int pieces = 1; 18 | for (int num : nums) { 19 | if (sum + num > mid) { 20 | // you cannot add this in this subarray, make new one 21 | // say you add this num in new subarray, then sum = num 22 | sum = num; 23 | pieces++; 24 | 25 | } else { 26 | sum += num; 27 | } 28 | 29 | } 30 | if(pieces > m){ 31 | start = mid + 1; 32 | 33 | } 34 | else { 35 | end = mid; 36 | } 37 | 38 | } 39 | 40 | return end; //here start == end 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /1470-shuffle-the-array/README.md: -------------------------------------------------------------------------------- 1 |

1470. Shuffle the Array

Easy


Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

2 | 3 |

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [2,5,1,3,4,7], n = 3
 9 | Output: [2,3,5,4,1,7] 
10 | Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [1,2,3,4,4,3,2,1], n = 4
16 | Output: [1,4,2,3,3,2,4,1]
17 | 
18 | 19 |

Example 3:

20 | 21 |
Input: nums = [1,1,2,2], n = 2
22 | Output: [1,2,1,2]
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= n <= 500
  • 30 |
  • nums.length == 2n
  • 31 |
  • 1 <= nums[i] <= 10^3
  • 32 |
-------------------------------------------------------------------------------- /1732-find-the-highest-altitude/README.md: -------------------------------------------------------------------------------- 1 |

1732. Find the Highest Altitude

Easy


There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.

2 | 3 |

You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i​​​​​​ and i + 1 for all (0 <= i < n). Return the highest altitude of a point.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: gain = [-5,1,5,0,-7]
 9 | Output: 1
10 | Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: gain = [-4,-3,-2,-1,4,3,2]
16 | Output: 0
17 | Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • n == gain.length
  • 25 |
  • 1 <= n <= 100
  • 26 |
  • -100 <= gain[i] <= 100
  • 27 |
28 |
-------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Merge Sort/mergesort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class mergesort{ 4 | 5 | public static void merge(int a[],int l,int m,int r,int n) { 6 | int i=l; 7 | int j=m+1; 8 | int k=0; 9 | int b[]=new int[n]; 10 | 11 | // Comparing the values in the 2 splitted arrays (array(l,m) and array(m+1,r)) 12 | while(i<=m && j<=r) { 13 | if(a[i]>a[j]) { 14 | b[k]=a[j]; 15 | j++; 16 | } 17 | else { 18 | b[k]=a[i]; 19 | i++; 20 | } 21 | k++; 22 | } 23 | 24 | // if any one of the array is reached the end we have to join the remaining array as it is in the main array 25 | while(i<=m) { 26 | b[k]=a[i]; 27 | k++; 28 | i++; 29 | } 30 | while(j<=r) { 31 | b[k]=a[j]; 32 | k++; 33 | j++; 34 | } 35 | for(i=l;i<=r;i++) { 36 | a[i]=b[i-l]; 37 | } 38 | 39 | } 40 | 41 | public static void mergesplit(int a[],int l,int r,int n) { 42 | if(l58. Length of Last Word

Easy


Given a string s consisting of words and spaces, return the length of the last word in the string.

2 | 3 |

A word is a maximal substring consisting of non-space characters only.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: s = "Hello World"
 9 | Output: 5
10 | Explanation: The last word is "World" with length 5.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: s = "   fly me   to   the moon  "
16 | Output: 4
17 | Explanation: The last word is "moon" with length 4.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: s = "luffy is still joyboy"
23 | Output: 6
24 | Explanation: The last word is "joyboy" with length 6.
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= s.length <= 104
  • 32 |
  • s consists of only English letters and spaces ' '.
  • 33 |
  • There will be at least one word in s.
  • 34 |
35 |
-------------------------------------------------------------------------------- /0034-find-first-and-last-position-of-element-in-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

34. Find First and Last Position of Element in Sorted Array

Medium


Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

2 | 3 |

If target is not found in the array, return [-1, -1].

4 | 5 |

You must write an algorithm with O(log n) runtime complexity.

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [5,7,7,8,8,10], target = 8
10 | Output: [3,4]
11 | 

Example 2:

12 |
Input: nums = [5,7,7,8,8,10], target = 6
13 | Output: [-1,-1]
14 | 

Example 3:

15 |
Input: nums = [], target = 0
16 | Output: [-1,-1]
17 | 
18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 0 <= nums.length <= 105
  • 23 |
  • -109 <= nums[i] <= 109
  • 24 |
  • nums is a non-decreasing array.
  • 25 |
  • -109 <= target <= 109
  • 26 |
27 |
-------------------------------------------------------------------------------- /0278-first-bad-version/README.md: -------------------------------------------------------------------------------- 1 |

278. First Bad Version

Easy


You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

2 | 3 |

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

4 | 5 |

You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: n = 5, bad = 4
11 | Output: 4
12 | Explanation:
13 | call isBadVersion(3) -> false
14 | call isBadVersion(5) -> true
15 | call isBadVersion(4) -> true
16 | Then 4 is the first bad version.
17 | 
18 | 19 |

Example 2:

20 | 21 |
Input: n = 1, bad = 1
22 | Output: 1
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= bad <= n <= 231 - 1
  • 30 |
31 |
-------------------------------------------------------------------------------- /1929-concatenation-of-array/README.md: -------------------------------------------------------------------------------- 1 |

1929. Concatenation of Array

Easy


Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).

2 | 3 |

Specifically, ans is the concatenation of two nums arrays.

4 | 5 |

Return the array ans.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: nums = [1,2,1]
11 | Output: [1,2,1,1,2,1]
12 | Explanation: The array ans is formed as follows:
13 | - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
14 | - ans = [1,2,1,1,2,1]
15 | 16 |

Example 2:

17 | 18 |
Input: nums = [1,3,2,1]
19 | Output: [1,3,2,1,1,3,2,1]
20 | Explanation: The array ans is formed as follows:
21 | - ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
22 | - ans = [1,3,2,1,1,3,2,1]
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • n == nums.length
  • 30 |
  • 1 <= n <= 1000
  • 31 |
  • 1 <= nums[i] <= 1000
  • 32 |
33 |
-------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Bucket Sort/bucketsort.java: -------------------------------------------------------------------------------- 1 | // Java program to sort an array 2 | // using bucket sort 3 | import java.util.*; 4 | import java.util.Collections; 5 | 6 | class GFG { 7 | 8 | // Function to sort arr[] of size n 9 | // using bucket sort 10 | static void bucketSort(float arr[], int n) 11 | { 12 | if (n <= 0) 13 | return; 14 | 15 | // 1) Create n empty buckets 16 | @SuppressWarnings("unchecked") 17 | Vector[] buckets = new Vector[n]; 18 | 19 | for (int i = 0; i < n; i++) { 20 | buckets[i] = new Vector(); 21 | } 22 | 23 | // 2) Put array elements in different buckets 24 | for (int i = 0; i < n; i++) { 25 | float idx = arr[i] * n; 26 | buckets[(int)idx].add(arr[i]); 27 | } 28 | 29 | // 3) Sort individual buckets 30 | for (int i = 0; i < n; i++) { 31 | Collections.sort(buckets[i]); 32 | } 33 | 34 | // 4) Concatenate all buckets into arr[] 35 | int index = 0; 36 | for (int i = 0; i < n; i++) { 37 | for (int j = 0; j < buckets[i].size(); j++) { 38 | arr[index++] = buckets[i].get(j); 39 | } 40 | } 41 | } 42 | 43 | // Driver code 44 | public static void main(String args[]) 45 | { 46 | float arr[] = { (float)0.897, (float)0.565, 47 | (float)0.656, (float)0.1234, 48 | (float)0.665, (float)0.3434 }; 49 | 50 | int n = arr.length; 51 | bucketSort(arr, n); 52 | 53 | System.out.println("Sorted array is "); 54 | for (float el : arr) { 55 | System.out.print(el + " "); 56 | } 57 | } 58 | } 59 | 60 | // Credit: Himangshu Shekhar Jha 61 | -------------------------------------------------------------------------------- /0410-split-array-largest-sum/README.md: -------------------------------------------------------------------------------- 1 |

410. Split Array Largest Sum

Hard


Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized.

2 | 3 |

Return the minimized largest sum of the split.

4 | 5 |

A subarray is a contiguous part of the array.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: nums = [7,2,5,10,8], k = 2
11 | Output: 18
12 | Explanation: There are four ways to split nums into two subarrays.
13 | The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
14 | 
15 | 16 |

Example 2:

17 | 18 |
Input: nums = [1,2,3,4,5], k = 2
19 | Output: 9
20 | Explanation: There are four ways to split nums into two subarrays.
21 | The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 1000
  • 29 |
  • 0 <= nums[i] <= 106
  • 30 |
  • 1 <= k <= min(50, nums.length)
  • 31 |
32 |
-------------------------------------------------------------------------------- /1539-kth-missing-positive-number/README.md: -------------------------------------------------------------------------------- 1 |

1539. Kth Missing Positive Number

Easy


Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.

2 | 3 |

Return the kth positive integer that is missing from this array.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: arr = [2,3,4,7,11], k = 5
 9 | Output: 9
10 | Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: arr = [1,2,3,4], k = 2
16 | Output: 6
17 | Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= arr.length <= 1000
  • 25 |
  • 1 <= arr[i] <= 1000
  • 26 |
  • 1 <= k <= 1000
  • 27 |
  • arr[i] < arr[j] for 1 <= i < j <= arr.length
  • 28 |
29 | 30 |

 

31 |

Follow up:

32 | 33 |

Could you solve this problem in less than O(n) complexity?

34 |
-------------------------------------------------------------------------------- /0162-find-peak-element/README.md: -------------------------------------------------------------------------------- 1 |

162. Find Peak Element

Medium


A peak element is an element that is strictly greater than its neighbors.

2 | 3 |

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

4 | 5 |

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

6 | 7 |

You must write an algorithm that runs in O(log n) time.

8 | 9 |

 

10 |

Example 1:

11 | 12 |
Input: nums = [1,2,3,1]
13 | Output: 2
14 | Explanation: 3 is a peak element and your function should return the index number 2.
15 | 16 |

Example 2:

17 | 18 |
Input: nums = [1,2,1,3,5,6,4]
19 | Output: 5
20 | Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 1000
  • 27 |
  • -231 <= nums[i] <= 231 - 1
  • 28 |
  • nums[i] != nums[i + 1] for all valid i.
  • 29 |
30 |
-------------------------------------------------------------------------------- /0125-valid-palindrome/README.md: -------------------------------------------------------------------------------- 1 |

125. Valid Palindrome

Easy


A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

2 | 3 |

Given a string s, return true if it is a palindrome, or false otherwise.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: s = "A man, a plan, a canal: Panama"
 9 | Output: true
10 | Explanation: "amanaplanacanalpanama" is a palindrome.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: s = "race a car"
16 | Output: false
17 | Explanation: "raceacar" is not a palindrome.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: s = " "
23 | Output: true
24 | Explanation: s is an empty string "" after removing non-alphanumeric characters.
25 | Since an empty string reads the same forward and backward, it is a palindrome.
26 | 
27 | 28 |

 

29 |

Constraints:

30 | 31 |
    32 |
  • 1 <= s.length <= 2 * 105
  • 33 |
  • s consists only of printable ASCII characters.
  • 34 |
35 |
-------------------------------------------------------------------------------- /1492-the-kth-factor-of-n/README.md: -------------------------------------------------------------------------------- 1 |

1492. The kth Factor of n

Medium


You are given two positive integers n and k. A factor of an integer n is defined as an integer i where n % i == 0.

2 | 3 |

Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: n = 12, k = 3
 9 | Output: 3
10 | Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: n = 7, k = 2
16 | Output: 7
17 | Explanation: Factors list is [1, 7], the 2nd factor is 7.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: n = 4, k = 4
23 | Output: -1
24 | Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1.
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= k <= n <= 1000
  • 32 |
33 | 34 |

 

35 |

Follow up:

36 | 37 |

Could you solve this problem in less than O(n) complexity?

38 |
-------------------------------------------------------------------------------- /0374-guess-number-higher-or-lower/README.md: -------------------------------------------------------------------------------- 1 |

374. Guess Number Higher or Lower

Easy


We are playing the Guess Game. The game is as follows:

2 | 3 |

I pick a number from 1 to n. You have to guess which number I picked.

4 | 5 |

Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.

6 | 7 |

You call a pre-defined API int guess(int num), which returns three possible results:

8 | 9 |
    10 |
  • -1: Your guess is higher than the number I picked (i.e. num > pick).
  • 11 |
  • 1: Your guess is lower than the number I picked (i.e. num < pick).
  • 12 |
  • 0: your guess is equal to the number I picked (i.e. num == pick).
  • 13 |
14 | 15 |

Return the number that I picked.

16 | 17 |

 

18 |

Example 1:

19 | 20 |
Input: n = 10, pick = 6
21 | Output: 6
22 | 
23 | 24 |

Example 2:

25 | 26 |
Input: n = 1, pick = 1
27 | Output: 1
28 | 
29 | 30 |

Example 3:

31 | 32 |
Input: n = 2, pick = 1
33 | Output: 1
34 | 
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • 1 <= n <= 231 - 1
  • 41 |
  • 1 <= pick <= n
  • 42 |
43 |
-------------------------------------------------------------------------------- /1342-number-of-steps-to-reduce-a-number-to-zero/README.md: -------------------------------------------------------------------------------- 1 |

1342. Number of Steps to Reduce a Number to Zero

Easy


Given an integer num, return the number of steps to reduce it to zero.

2 | 3 |

In one step, if the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: num = 14
 9 | Output: 6
10 | Explanation: 
11 | Step 1) 14 is even; divide by 2 and obtain 7. 
12 | Step 2) 7 is odd; subtract 1 and obtain 6.
13 | Step 3) 6 is even; divide by 2 and obtain 3. 
14 | Step 4) 3 is odd; subtract 1 and obtain 2. 
15 | Step 5) 2 is even; divide by 2 and obtain 1. 
16 | Step 6) 1 is odd; subtract 1 and obtain 0.
17 | 
18 | 19 |

Example 2:

20 | 21 |
Input: num = 8
22 | Output: 4
23 | Explanation: 
24 | Step 1) 8 is even; divide by 2 and obtain 4. 
25 | Step 2) 4 is even; divide by 2 and obtain 2. 
26 | Step 3) 2 is even; divide by 2 and obtain 1. 
27 | Step 4) 1 is odd; subtract 1 and obtain 0.
28 | 
29 | 30 |

Example 3:

31 | 32 |
Input: num = 123
33 | Output: 12
34 | 
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • 0 <= num <= 106
  • 41 |
42 |
-------------------------------------------------------------------------------- /1365-how-many-numbers-are-smaller-than-the-current-number/README.md: -------------------------------------------------------------------------------- 1 |

1365. How Many Numbers Are Smaller Than the Current Number

Easy


Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].

2 | 3 |

Return the answer in an array.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [8,1,2,2,3]
 9 | Output: [4,0,1,1,3]
10 | Explanation: 
11 | For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). 
12 | For nums[1]=1 does not exist any smaller number than it.
13 | For nums[2]=2 there exist one smaller number than it (1). 
14 | For nums[3]=2 there exist one smaller number than it (1). 
15 | For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
16 | 
17 | 18 |

Example 2:

19 | 20 |
Input: nums = [6,5,4,8]
21 | Output: [2,1,0,3]
22 | 
23 | 24 |

Example 3:

25 | 26 |
Input: nums = [7,7,7,7]
27 | Output: [0,0,0,0]
28 | 
29 | 30 |

 

31 |

Constraints:

32 | 33 |
    34 |
  • 2 <= nums.length <= 500
  • 35 |
  • 0 <= nums[i] <= 100
  • 36 |
37 |
-------------------------------------------------------------------------------- /0852-peak-index-in-a-mountain-array/README.md: -------------------------------------------------------------------------------- 1 |

852. Peak Index in a Mountain Array

Medium


An array arr a mountain if the following properties hold:

2 | 3 |
    4 |
  • arr.length >= 3
  • 5 |
  • There exists some i with 0 < i < arr.length - 1 such that: 6 |
      7 |
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • 8 |
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
    • 9 |
    10 |
  • 11 |
12 | 13 |

Given a mountain array arr, return the index i such that arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].

14 | 15 |

You must solve it in O(log(arr.length)) time complexity.

16 | 17 |

 

18 |

Example 1:

19 | 20 |
Input: arr = [0,1,0]
21 | Output: 1
22 | 
23 | 24 |

Example 2:

25 | 26 |
Input: arr = [0,2,1,0]
27 | Output: 1
28 | 
29 | 30 |

Example 3:

31 | 32 |
Input: arr = [0,10,5,2]
33 | Output: 1
34 | 
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • 3 <= arr.length <= 105
  • 41 |
  • 0 <= arr[i] <= 106
  • 42 |
  • arr is guaranteed to be a mountain array.
  • 43 |
44 |
-------------------------------------------------------------------------------- /1662-check-if-two-string-arrays-are-equivalent/README.md: -------------------------------------------------------------------------------- 1 |

1662. Check If Two String Arrays are Equivalent

Easy


Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.

2 | 3 |

A string is represented by an array if the array elements concatenated in order forms the string.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
 9 | Output: true
10 | Explanation:
11 | word1 represents string "ab" + "c" -> "abc"
12 | word2 represents string "a" + "bc" -> "abc"
13 | The strings are the same, so return true.
14 | 15 |

Example 2:

16 | 17 |
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
18 | Output: false
19 | 
20 | 21 |

Example 3:

22 | 23 |
Input: word1  = ["abc", "d", "defg"], word2 = ["abcddefg"]
24 | Output: true
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= word1.length, word2.length <= 103
  • 32 |
  • 1 <= word1[i].length, word2[i].length <= 103
  • 33 |
  • 1 <= sum(word1[i].length), sum(word2[i].length) <= 103
  • 34 |
  • word1[i] and word2[i] consist of lowercase letters.
  • 35 |
36 |
-------------------------------------------------------------------------------- /1572-matrix-diagonal-sum/README.md: -------------------------------------------------------------------------------- 1 |

1572. Matrix Diagonal Sum

Easy


Given a square matrix mat, return the sum of the matrix diagonals.

2 | 3 |

Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: mat = [[1,2,3],
 9 |               [4,5,6],
10 |               [7,8,9]]
11 | Output: 25
12 | Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
13 | Notice that element mat[1][1] = 5 is counted only once.
14 | 
15 | 16 |

Example 2:

17 | 18 |
Input: mat = [[1,1,1,1],
19 |               [1,1,1,1],
20 |               [1,1,1,1],
21 |               [1,1,1,1]]
22 | Output: 8
23 | 
24 | 25 |

Example 3:

26 | 27 |
Input: mat = [[5]]
28 | Output: 5
29 | 
30 | 31 |

 

32 |

Constraints:

33 | 34 |
    35 |
  • n == mat.length == mat[i].length
  • 36 |
  • 1 <= n <= 100
  • 37 |
  • 1 <= mat[i][j] <= 100
  • 38 |
39 |
-------------------------------------------------------------------------------- /0066-plus-one/README.md: -------------------------------------------------------------------------------- 1 |

66. Plus One

Easy


You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

2 | 3 |

Increment the large integer by one and return the resulting array of digits.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: digits = [1,2,3]
 9 | Output: [1,2,4]
10 | Explanation: The array represents the integer 123.
11 | Incrementing by one gives 123 + 1 = 124.
12 | Thus, the result should be [1,2,4].
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: digits = [4,3,2,1]
18 | Output: [4,3,2,2]
19 | Explanation: The array represents the integer 4321.
20 | Incrementing by one gives 4321 + 1 = 4322.
21 | Thus, the result should be [4,3,2,2].
22 | 
23 | 24 |

Example 3:

25 | 26 |
Input: digits = [9]
27 | Output: [1,0]
28 | Explanation: The array represents the integer 9.
29 | Incrementing by one gives 9 + 1 = 10.
30 | Thus, the result should be [1,0].
31 | 
32 | 33 |

 

34 |

Constraints:

35 | 36 |
    37 |
  • 1 <= digits.length <= 100
  • 38 |
  • 0 <= digits[i] <= 9
  • 39 |
  • digits does not contain any leading 0's.
  • 40 |
41 |
-------------------------------------------------------------------------------- /0268-missing-number/README.md: -------------------------------------------------------------------------------- 1 |

268. Missing Number

Easy


Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums = [3,0,1]
 7 | Output: 2
 8 | Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: nums = [0,1]
14 | Output: 2
15 | Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: nums = [9,6,4,2,3,5,7,0,1]
21 | Output: 8
22 | Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • n == nums.length
  • 30 |
  • 1 <= n <= 104
  • 31 |
  • 0 <= nums[i] <= n
  • 32 |
  • All the numbers of nums are unique.
  • 33 |
34 | 35 |

 

36 |

Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?

37 |
-------------------------------------------------------------------------------- /0365-water-and-jug-problem/README.md: -------------------------------------------------------------------------------- 1 |

365. Water and Jug Problem

Medium


You are given two jugs with capacities jug1Capacity and jug2Capacity liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly targetCapacity liters using these two jugs.

2 | 3 |

If targetCapacity liters of water are measurable, you must have targetCapacity liters of water contained within one or both buckets by the end.

4 | 5 |

Operations allowed:

6 | 7 |
    8 |
  • Fill any of the jugs with water.
  • 9 |
  • Empty any of the jugs.
  • 10 |
  • Pour water from one jug into another till the other jug is completely full, or the first jug itself is empty.
  • 11 |
12 | 13 |

 

14 |

Example 1:

15 | 16 |
Input: jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4
17 | Output: true
18 | Explanation: The famous Die Hard example 
19 | 
20 | 21 |

Example 2:

22 | 23 |
Input: jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5
24 | Output: false
25 | 
26 | 27 |

Example 3:

28 | 29 |
Input: jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3
30 | Output: true
31 | 
32 | 33 |

 

34 |

Constraints:

35 | 36 |
    37 |
  • 1 <= jug1Capacity, jug2Capacity, targetCapacity <= 106
  • 38 |
39 |
-------------------------------------------------------------------------------- /0832-flipping-an-image/README.md: -------------------------------------------------------------------------------- 1 |

832. Flipping an Image

Easy


Given an n x n binary matrix image, flip the image horizontally, then invert it, and return the resulting image.

2 | 3 |

To flip an image horizontally means that each row of the image is reversed.

4 | 5 |
    6 |
  • For example, flipping [1,1,0] horizontally results in [0,1,1].
  • 7 |
8 | 9 |

To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0.

10 | 11 |
    12 |
  • For example, inverting [0,1,1] results in [1,0,0].
  • 13 |
14 | 15 |

 

16 |

Example 1:

17 | 18 |
Input: image = [[1,1,0],[1,0,1],[0,0,0]]
19 | Output: [[1,0,0],[0,1,0],[1,1,1]]
20 | Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
21 | Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
22 | 
23 | 24 |

Example 2:

25 | 26 |
Input: image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
27 | Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
28 | Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
29 | Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
30 | 
31 | 32 |

 

33 |

Constraints:

34 | 35 |
    36 |
  • n == image.length
  • 37 |
  • n == image[i].length
  • 38 |
  • 1 <= n <= 20
  • 39 |
  • images[i][j] is either 0 or 1.
  • 40 |
41 |
-------------------------------------------------------------------------------- /Questions/Backtracking/sodukoSolver.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isValid(vector < vector < char >> & board, int row, int col, char c) { 5 | for (int i = 0; i < 9; i++) { 6 | if (board[i][col] == c) 7 | return false; 8 | 9 | if (board[row][i] == c) 10 | return false; 11 | 12 | if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) 13 | return false; 14 | } 15 | return true; 16 | } 17 | 18 | bool solveSudoku(vector < vector < char >> & board) { 19 | for (int i = 0; i < board.size(); i++) { 20 | for (int j = 0; j < board[0].size(); j++) { 21 | if (board[i][j] == '.') { 22 | for (char c = '1'; c <= '9'; c++) { 23 | if (isValid(board, i, j, c)) { 24 | board[i][j] = c; 25 | 26 | if (solveSudoku(board)) 27 | return true; 28 | else 29 | board[i][j] = '.'; 30 | } 31 | } 32 | 33 | return false; 34 | } 35 | } 36 | } 37 | return true; 38 | } 39 | int main() { 40 | vector>board{ 41 | {'9', '5', '7', '.', '1', '3', '.', '8', '4'}, 42 | {'4', '8', '3', '.', '5', '7', '1', '.', '6'}, 43 | {'.', '1', '2', '.', '4', '9', '5', '3', '7'}, 44 | {'1', '7', '.', '3', '.', '4', '9', '.', '2'}, 45 | {'5', '.', '4', '9', '7', '.', '3', '6', '.'}, 46 | {'3', '.', '9', '5', '.', '8', '7', '.', '1'}, 47 | {'8', '4', '5', '7', '9', '.', '6', '1', '3'}, 48 | {'.', '9', '1', '.', '3', '6', '.', '7', '5'}, 49 | {'7', '.', '6', '1', '8', '5', '4', '.', '9'} 50 | }; 51 | 52 | solveSudoku(board); 53 | 54 | for(int i= 0; i< 9; i++){ 55 | for(int j= 0; j< 9; j++) 56 | cout<1920. Build Array from Permutation

Easy


Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.

2 | 3 |

A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [0,2,1,5,3,4]
 9 | Output: [0,1,2,4,5,3]
10 | Explanation: The array ans is built as follows: 
11 | ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
12 |     = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
13 |     = [0,1,2,4,5,3]
14 | 15 |

Example 2:

16 | 17 |
Input: nums = [5,0,1,2,3,4]
18 | Output: [4,5,0,1,2,3]
19 | Explanation: The array ans is built as follows:
20 | ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
21 |     = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
22 |     = [4,5,0,1,2,3]
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 1000
  • 29 |
  • 0 <= nums[i] < nums.length
  • 30 |
  • The elements in nums are distinct.
  • 31 |
32 | 33 |

 

34 |

Follow-up: Can you solve it without using an extra space (i.e., O(1) memory)?

35 |
-------------------------------------------------------------------------------- /1672-richest-customer-wealth/README.md: -------------------------------------------------------------------------------- 1 |

1672. Richest Customer Wealth

Easy


You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. Return the wealth that the richest customer has.

2 | 3 |

A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: accounts = [[1,2,3],[3,2,1]]
 9 | Output: 6
10 | Explanation:
11 | 1st customer has wealth = 1 + 2 + 3 = 6
12 | 2nd customer has wealth = 3 + 2 + 1 = 6
13 | Both customers are considered the richest with a wealth of 6 each, so return 6.
14 | 
15 | 16 |

Example 2:

17 | 18 |
Input: accounts = [[1,5],[7,3],[3,5]]
19 | Output: 10
20 | Explanation: 
21 | 1st customer has wealth = 6
22 | 2nd customer has wealth = 10 
23 | 3rd customer has wealth = 8
24 | The 2nd customer is the richest with a wealth of 10.
25 | 26 |

Example 3:

27 | 28 |
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
29 | Output: 17
30 | 
31 | 32 |

 

33 |

Constraints:

34 | 35 |
    36 |
  • m == accounts.length
  • 37 |
  • n == accounts[i].length
  • 38 |
  • 1 <= m, n <= 50
  • 39 |
  • 1 <= accounts[i][j] <= 100
  • 40 |
41 |
-------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Heap Sort/heapsort.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Heap Sort 2 | 3 | public class HeapSort { 4 | public void sort(int arr[]) 5 | { 6 | int N = arr.length; 7 | 8 | // Build heap (rearrange array) 9 | for (int i = N / 2 - 1; i >= 0; i--) 10 | heapify(arr, N, i); 11 | 12 | // One by one extract an element from heap 13 | for (int i = N - 1; i > 0; i--) { 14 | // Move current root to end 15 | int temp = arr[0]; 16 | arr[0] = arr[i]; 17 | arr[i] = temp; 18 | 19 | // call max heapify on the reduced heap 20 | heapify(arr, i, 0); 21 | } 22 | } 23 | 24 | // To heapify a subtree rooted with node i which is 25 | // an index in arr[]. n is size of heap 26 | void heapify(int arr[], int N, int i) 27 | { 28 | int largest = i; // Initialize largest as root 29 | int l = 2 * i + 1; // left = 2*i + 1 30 | int r = 2 * i + 2; // right = 2*i + 2 31 | 32 | // If left child is larger than root 33 | if (l < N && arr[l] > arr[largest]) 34 | largest = l; 35 | 36 | // If right child is larger than largest so far 37 | if (r < N && arr[r] > arr[largest]) 38 | largest = r; 39 | 40 | // If largest is not root 41 | if (largest != i) { 42 | int swap = arr[i]; 43 | arr[i] = arr[largest]; 44 | arr[largest] = swap; 45 | 46 | // Recursively heapify the affected sub-tree 47 | heapify(arr, N, largest); 48 | } 49 | } 50 | 51 | /* A utility function to print array of size n */ 52 | static void printArray(int arr[]) 53 | { 54 | int N = arr.length; 55 | 56 | for (int i = 0; i < N; ++i) 57 | System.out.print(arr[i] + " "); 58 | System.out.println(); 59 | } 60 | 61 | // Driver's code 62 | public static void main(String args[]) 63 | { 64 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 65 | int N = arr.length; 66 | 67 | // Function call 68 | HeapSort ob = new HeapSort(); 69 | ob.sort(arr); 70 | 71 | System.out.println("Sorted array is"); 72 | printArray(arr); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Questions/Backtracking/ratInAMaze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | void findPathHelper(int i, int j, vector < vector < int >> & a, int n, vector < string > & ans, string move, 7 | vector < vector < int >> & vis) { 8 | if (i == n - 1 && j == n - 1) { 9 | ans.push_back(move); 10 | return; 11 | } 12 | 13 | // downward 14 | if (i + 1 < n && !vis[i + 1][j] && a[i + 1][j] == 1) { 15 | vis[i][j] = 1; 16 | findPathHelper(i + 1, j, a, n, ans, move + 'D', vis); 17 | vis[i][j] = 0; 18 | } 19 | 20 | // left 21 | if (j - 1 >= 0 && !vis[i][j - 1] && a[i][j - 1] == 1) { 22 | vis[i][j] = 1; 23 | findPathHelper(i, j - 1, a, n, ans, move + 'L', vis); 24 | vis[i][j] = 0; 25 | } 26 | 27 | // right 28 | if (j + 1 < n && !vis[i][j + 1] && a[i][j + 1] == 1) { 29 | vis[i][j] = 1; 30 | findPathHelper(i, j + 1, a, n, ans, move + 'R', vis); 31 | vis[i][j] = 0; 32 | } 33 | 34 | // upward 35 | if (i - 1 >= 0 && !vis[i - 1][j] && a[i - 1][j] == 1) { 36 | vis[i][j] = 1; 37 | findPathHelper(i - 1, j, a, n, ans, move + 'U', vis); 38 | vis[i][j] = 0; 39 | } 40 | 41 | } 42 | public: 43 | vector < string > findPath(vector < vector < int >> & m, int n) { 44 | vector < string > ans; 45 | vector < vector < int >> vis(n, vector < int > (n, 0)); 46 | 47 | if (m[0][0] == 1) findPathHelper(0, 0, m, n, ans, "", vis); 48 | return ans; 49 | } 50 | }; 51 | 52 | int main() { 53 | int n = 4; 54 | 55 | vector < vector < int >> m = {{1,0,0,0},{1,1,0,1},{1,1,0,0},{0,1,1,1}}; 56 | 57 | Solution obj; 58 | vector < string > result = obj.findPath(m, n); 59 | if (result.size() == 0) 60 | cout << -1; 61 | else 62 | for (int i = 0; i < result.size(); i++) cout << result[i] << " "; 63 | cout << endl; 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int pivot = findPivot(nums); 4 | 5 | if (pivot == -1){ 6 | //just do normal binary search 7 | return binarySearch(nums, target, 0, nums.length-1); 8 | 9 | } 10 | // if pivot is found, we have found 2 asc arrays 11 | 12 | if(nums[pivot] == target){ 13 | return pivot; 14 | } 15 | if(target >= nums[0]){ 16 | return binarySearch(nums, target, 0, pivot - 1); 17 | 18 | } 19 | return binarySearch(nums, target, pivot + 1, nums.length -1); 20 | } 21 | public int binarySearch(int[] arr , int target, int start, int end){ 22 | 23 | while(start <= end){ 24 | //find the middle element 25 | // int mid = (start + end)/2 ; might be possible that (start+ end) may exceed 26 | int mid = start + (end - start)/2; 27 | if(target < arr[mid]){ 28 | end = mid - 1; 29 | 30 | } else if (target > arr[mid]){ 31 | start = mid +1; 32 | }else{ 33 | //ans found 34 | return mid; 35 | 36 | } 37 | 38 | } 39 | return -1; 40 | } 41 | public int findPivot(int[] arr){ 42 | int start = 0; 43 | int end = arr.length - 1; 44 | while(start <= end){ 45 | int mid = start + (end - start) /2; 46 | // 4 cases over here 47 | if (mid < end &&arr[mid] > arr [mid+1]){ 48 | return mid; 49 | } 50 | if(mid > start && arr[mid] < arr[mid -1]){ 51 | return mid; 52 | } 53 | if (arr[mid] <= arr[start]){ 54 | end = mid-1; 55 | } 56 | else{ 57 | start = mid + 1; 58 | } 59 | } 60 | return -1; 61 | } 62 | } -------------------------------------------------------------------------------- /0744-find-smallest-letter-greater-than-target/README.md: -------------------------------------------------------------------------------- 1 |

744. Find Smallest Letter Greater Than Target

Easy


You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.

2 | 3 |

Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: letters = ["c","f","j"], target = "a"
 9 | Output: "c"
10 | Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: letters = ["c","f","j"], target = "c"
16 | Output: "f"
17 | Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: letters = ["x","x","y","y"], target = "z"
23 | Output: "x"
24 | Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 2 <= letters.length <= 104
  • 32 |
  • letters[i] is a lowercase English letter.
  • 33 |
  • letters is sorted in non-decreasing order.
  • 34 |
  • letters contains at least two different characters.
  • 35 |
  • target is a lowercase English letter.
  • 36 |
37 |
-------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Quick Sort/QuickSort.cpp: -------------------------------------------------------------------------------- 1 | /*Problem of Quick Sort algorithm*/ 2 | 3 | #include 4 | using namespace std; 5 | // Funtio to Swap two elements 6 | void swap(int* a, int* b) 7 | { 8 | int t = *a; 9 | *a = *b; 10 | *b = t; 11 | } 12 | 13 | // partition of the array using last element as pivot 14 | int partition (int arr[], int low, int high) 15 | { 16 | int pivot = arr[high]; // pivot 17 | int i = (low - 1); 18 | 19 | for (int j = low; j <= high- 1; j++) 20 | { 21 | //if current element is smaller than pivot, increment the low element 22 | //swap elements at i and j 23 | if (arr[j] <= pivot) 24 | { 25 | i++; // increment index of smaller element 26 | swap(&arr[i], &arr[j]); 27 | } 28 | } 29 | swap(&arr[i + 1], &arr[high]); 30 | return (i + 1); 31 | } 32 | 33 | // quicksort algorithm 34 | void quickSort(int arr[], int low, int high) 35 | { 36 | if (low < high) 37 | { 38 | //partition the array 39 | int pivot = partition(arr, low, high); 40 | 41 | //sort the sub arrays independently 42 | quickSort(arr, low, pivot - 1); 43 | quickSort(arr, pivot + 1, high); 44 | } 45 | } 46 | //This funtion for display arrays 47 | void displayArray(int arr[], int size) 48 | { 49 | int i; 50 | for (i=0; i < size; i++) 51 | cout<>size; //Input size of array 61 | int arr[size]; 62 | int i; 63 | for(i=0;i>arr[i]; //Take input array element 65 | displayArray(arr,size); 66 | cout<154. Find Minimum in Rotated Sorted Array II

Hard


Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become:

2 | 3 |
    4 |
  • [4,5,6,7,0,1,4] if it was rotated 4 times.
  • 5 |
  • [0,1,4,4,5,6,7] if it was rotated 7 times.
  • 6 |
7 | 8 |

Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

9 | 10 |

Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array.

11 | 12 |

You must decrease the overall operation steps as much as possible.

13 | 14 |

 

15 |

Example 1:

16 |
Input: nums = [1,3,5]
17 | Output: 1
18 | 

Example 2:

19 |
Input: nums = [2,2,2,0,1]
20 | Output: 0
21 | 
22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • n == nums.length
  • 27 |
  • 1 <= n <= 5000
  • 28 |
  • -5000 <= nums[i] <= 5000
  • 29 |
  • nums is sorted and rotated between 1 and n times.
  • 30 |
31 | 32 |

 

33 |

Follow up: This problem is similar to Find Minimum in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why?

34 | 35 |

 

36 |
-------------------------------------------------------------------------------- /Questions/Backtracking/NQueenProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | class Solution { 5 | public: 6 | bool check(int row, int col, vector < string > board, int n) { 7 | // check upper element 8 | int duprow = row; 9 | int dupcol = col; 10 | 11 | while (row >= 0 && col >= 0) { 12 | if (board[row][col] == 'Q') 13 | return false; 14 | row--; 15 | col--; 16 | } 17 | 18 | col = dupcol; 19 | row = duprow; 20 | while (col >= 0) { 21 | if (board[row][col] == 'Q') 22 | return false; 23 | col--; 24 | } 25 | 26 | row = duprow; 27 | col = dupcol; 28 | while (row < n && col >= 0) { 29 | if (board[row][col] == 'Q') 30 | return false; 31 | row++; 32 | col--; 33 | } 34 | return true; 35 | } 36 | 37 | public: 38 | void solve(int col, vector < string > & board, vector < vector < string >> & ans, int n) { 39 | if (col == n) { 40 | ans.push_back(board); 41 | return; 42 | } 43 | for (int row = 0; row < n; row++) { 44 | if (check(row, col, board, n)) { 45 | board[row][col] = 'Q'; 46 | solve(col + 1, board, ans, n); 47 | board[row][col] = '.'; 48 | } 49 | } 50 | } 51 | 52 | public: 53 | vector < vector < string >> solveNQueens(int n) { 54 | vector < vector < string >> ans; 55 | vector < string > board(n); 56 | string s(n, '.'); 57 | for (int i = 0; i < n; i++) { 58 | board[i] = s; 59 | } 60 | solve(0, board, ans, n); 61 | return ans; 62 | } 63 | }; 64 | int main() { 65 | int n = 4; // we are taking 4*4 grid and 4 queens 66 | Solution obj; 67 | vector < vector < string >> ans = obj.solveNQueens(n); 68 | for (int i = 0; i < ans.size(); i++) { 69 | cout << "Arrangement " << i + 1 << "\n"; 70 | for (int j = 0; j < ans[0].size(); j++) { 71 | cout << ans[i][j]; 72 | cout << endl; 73 | } 74 | cout << endl; 75 | } 76 | return 0; 77 | } -------------------------------------------------------------------------------- /0190-reverse-bits/README.md: -------------------------------------------------------------------------------- 1 |

190. Reverse Bits

Easy


Reverse bits of a given 32 bits unsigned integer.

2 | 3 |

Note:

4 | 5 |
    6 |
  • Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
  • 7 |
  • In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.
  • 8 |
9 | 10 |

 

11 |

Example 1:

12 | 13 |
Input: n = 00000010100101000001111010011100
14 | Output:    964176192 (00111001011110000010100101000000)
15 | Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
16 | 
17 | 18 |

Example 2:

19 | 20 |
Input: n = 11111111111111111111111111111101
21 | Output:   3221225471 (10111111111111111111111111111111)
22 | Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The input must be a binary string of length 32
  • 30 |
31 | 32 |

 

33 |

Follow up: If this function is called many times, how would you optimize it?

34 |
-------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Insertion Sort/InsertionSort.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import java.util.Scanner; 4 | import java.util.Arrays; 5 | 6 | public class InsertionSort { 7 | // Insertion Sort 8 | public static void main(String[] args) { 9 | // Creating Scanner object for taking Inputs from the USER 10 | Scanner sc = new Scanner(System.in); 11 | // Take SIZE of 1D Array from the USER 12 | System.out.print(" Enter SIZE of 1D Array: "); 13 | int n = sc.nextInt(); 14 | // Creating Array of Required SIZE 15 | int[] arr = new int[n]; 16 | // Fill the Array by taking Inputs from the USER 17 | System.out.print(" Enter " + n + " Elements: "); 18 | for (int i = 0; i < arr.length; i++) 19 | arr[i] = sc.nextInt(); 20 | // Display the Array Before Sorting 21 | System.out.println(" Array Before Sorting is: " + Arrays.toString(arr)); 22 | // Applying Insertion Sort 23 | insertionSort(arr); 24 | // Display Array After Sorting 25 | System.out.println(" Array After Sorting is: " + Arrays.toString(arr)); 26 | } 27 | 28 | // Method of Insertion Sort 29 | static void insertionSort(int[] arr) { 30 | // Outer Loop for Number of Passes 31 | for (int i = 0; i < arr.length - 1; i++) { 32 | // Inner Loop for Comparison 33 | for (int j = i + 1; j > 0; j--) { 34 | // Compare the Elements (i.e Element at Current Index and it's Previous Index) 35 | // if Previous Element is Greater than Current Element then Swap 36 | // otherwise Break the loop Because in that Case SubArray will Already Sorted 37 | if (arr[j] < arr[j - 1]) 38 | // Swap the Elements 39 | swap(arr, j, j - 1); 40 | else 41 | break; 42 | } 43 | } 44 | } 45 | 46 | // Method to Swap Two Elements of Array 47 | static void swap(int[] arr, int a, int b) { 48 | int temp = arr[a]; 49 | arr[a] = arr[b]; 50 | arr[b] = temp; 51 | } 52 | } 53 | 54 | 55 | 56 | // Contributed by: Jayesh Kumavat 57 | -------------------------------------------------------------------------------- /1095-find-in-mountain-array/1095-find-in-mountain-array.java: -------------------------------------------------------------------------------- 1 | /** 2 | * // This is MountainArray's API interface. 3 | * // You should not implement it, or speculate about its implementation 4 | * interface MountainArray { 5 | * public int get(int index) {} 6 | * public int length() {} 7 | * } 8 | */ 9 | 10 | class Solution { 11 | public int findInMountainArray(int target, MountainArray arr) { 12 | int peak = peakIndexInMountainArray(arr); 13 | int firstTry = orderAgnosticBS(arr, target,0,peak); 14 | if(firstTry != -1 ){ 15 | return firstTry; 16 | } 17 | return orderAgnosticBS(arr, target,peak+1, arr.length()-1); 18 | } 19 | public int peakIndexInMountainArray(MountainArray arr) { 20 | int start = 0; 21 | int end = arr.length() -1; 22 | while(start < end){ 23 | int mid = start + (end - start)/2; 24 | if (arr.get(mid) > arr.get(mid+1)){ 25 | end = mid; 26 | } 27 | else{ 28 | start = mid+1; 29 | } 30 | } 31 | return start; 32 | 33 | } 34 | static int orderAgnosticBS(MountainArray arr, int target, int start, int end){ 35 | // find whether the array is sorted in ascending or descending 36 | boolean isAsc = arr.get(start) < arr.get(end); 37 | while(start <= end){ 38 | //find the middle element 39 | // int mid = (start + end)/2 ; might be possible that (start+ end) may exceed 40 | int mid = start + (end - start)/2; 41 | if (arr.get(mid) == target){ 42 | return mid; 43 | } 44 | if(isAsc) { 45 | if (target < arr.get(mid)) { 46 | end = mid - 1; 47 | 48 | } else { 49 | start = mid + 1; 50 | } 51 | } else { 52 | if (target > arr.get(mid)) { 53 | end = mid - 1; 54 | } else { 55 | start = mid + 1; 56 | 57 | } 58 | } 59 | 60 | 61 | } 62 | return -1; 63 | 64 | 65 | } 66 | } -------------------------------------------------------------------------------- /0081-search-in-rotated-sorted-array-ii/README.md: -------------------------------------------------------------------------------- 1 |

81. Search in Rotated Sorted Array II

Medium


There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).

2 | 3 |

Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4].

4 | 5 |

Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums.

6 | 7 |

You must decrease the overall operation steps as much as possible.

8 | 9 |

 

10 |

Example 1:

11 |
Input: nums = [2,5,6,0,0,1,2], target = 0
12 | Output: true
13 | 

Example 2:

14 |
Input: nums = [2,5,6,0,0,1,2], target = 3
15 | Output: false
16 | 
17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= nums.length <= 5000
  • 22 |
  • -104 <= nums[i] <= 104
  • 23 |
  • nums is guaranteed to be rotated at some pivot.
  • 24 |
  • -104 <= target <= 104
  • 25 |
26 | 27 |

 

28 |

Follow up: This problem is similar to Search in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why?

29 |
-------------------------------------------------------------------------------- /1389-create-target-array-in-the-given-order/README.md: -------------------------------------------------------------------------------- 1 |

1389. Create Target Array in the Given Order

Easy


Given two arrays of integers nums and index. Your task is to create target array under the following rules:

2 | 3 |
    4 |
  • Initially target array is empty.
  • 5 |
  • From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
  • 6 |
  • Repeat the previous step until there are no elements to read in nums and index.
  • 7 |
8 | 9 |

Return the target array.

10 | 11 |

It is guaranteed that the insertion operations will be valid.

12 | 13 |

 

14 |

Example 1:

15 | 16 |
Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
17 | Output: [0,4,1,3,2]
18 | Explanation:
19 | nums       index     target
20 | 0            0        [0]
21 | 1            1        [0,1]
22 | 2            2        [0,1,2]
23 | 3            2        [0,1,3,2]
24 | 4            1        [0,4,1,3,2]
25 | 
26 | 27 |

Example 2:

28 | 29 |
Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
30 | Output: [0,1,2,3,4]
31 | Explanation:
32 | nums       index     target
33 | 1            0        [1]
34 | 2            1        [1,2]
35 | 3            2        [1,2,3]
36 | 4            3        [1,2,3,4]
37 | 0            0        [0,1,2,3,4]
38 | 
39 | 40 |

Example 3:

41 | 42 |
Input: nums = [1], index = [0]
43 | Output: [1]
44 | 
45 | 46 |

 

47 |

Constraints:

48 | 49 |
    50 |
  • 1 <= nums.length, index.length <= 100
  • 51 |
  • nums.length == index.length
  • 52 |
  • 0 <= nums[i] <= 100
  • 53 |
  • 0 <= index[i] <= i
  • 54 |
55 |
-------------------------------------------------------------------------------- /0153-find-minimum-in-rotated-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

153. Find Minimum in Rotated Sorted Array

Medium


Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

2 | 3 |
    4 |
  • [4,5,6,7,0,1,2] if it was rotated 4 times.
  • 5 |
  • [0,1,2,4,5,6,7] if it was rotated 7 times.
  • 6 |
7 | 8 |

Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

9 | 10 |

Given the sorted rotated array nums of unique elements, return the minimum element of this array.

11 | 12 |

You must write an algorithm that runs in O(log n) time.

13 | 14 |

 

15 |

Example 1:

16 | 17 |
Input: nums = [3,4,5,1,2]
18 | Output: 1
19 | Explanation: The original array was [1,2,3,4,5] rotated 3 times.
20 | 
21 | 22 |

Example 2:

23 | 24 |
Input: nums = [4,5,6,7,0,1,2]
25 | Output: 0
26 | Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
27 | 
28 | 29 |

Example 3:

30 | 31 |
Input: nums = [11,13,15,17]
32 | Output: 11
33 | Explanation: The original array was [11,13,15,17] and it was rotated 4 times. 
34 | 
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • n == nums.length
  • 41 |
  • 1 <= n <= 5000
  • 42 |
  • -5000 <= nums[i] <= 5000
  • 43 |
  • All the integers of nums are unique.
  • 44 |
  • nums is sorted and rotated between 1 and n times.
  • 45 |
46 |
-------------------------------------------------------------------------------- /1773-count-items-matching-a-rule/README.md: -------------------------------------------------------------------------------- 1 |

1773. Count Items Matching a Rule

Easy


You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue.

2 | 3 |

The ith item is said to match the rule if one of the following is true:

4 | 5 |
    6 |
  • ruleKey == "type" and ruleValue == typei.
  • 7 |
  • ruleKey == "color" and ruleValue == colori.
  • 8 |
  • ruleKey == "name" and ruleValue == namei.
  • 9 |
10 | 11 |

Return the number of items that match the given rule.

12 | 13 |

 

14 |

Example 1:

15 | 16 |
Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
17 | Output: 1
18 | Explanation: There is only one item matching the given rule, which is ["computer","silver","lenovo"].
19 | 
20 | 21 |

Example 2:

22 | 23 |
Input: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
24 | Output: 2
25 | Explanation: There are only two items matching the given rule, which are ["phone","blue","pixel"] and ["phone","gold","iphone"]. Note that the item ["computer","silver","phone"] does not match.
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= items.length <= 104
  • 32 |
  • 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
  • 33 |
  • ruleKey is equal to either "type", "color", or "name".
  • 34 |
  • All strings consist only of lowercase letters.
  • 35 |
36 |
-------------------------------------------------------------------------------- /1095-find-in-mountain-array/README.md: -------------------------------------------------------------------------------- 1 |

1095. Find in Mountain Array

Hard


(This problem is an interactive problem.)

2 | 3 |

You may recall that an array arr is a mountain array if and only if:

4 | 5 |
    6 |
  • arr.length >= 3
  • 7 |
  • There exists some i with 0 < i < arr.length - 1 such that: 8 |
      9 |
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • 10 |
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
    • 11 |
    12 |
  • 13 |
14 | 15 |

Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target. If such an index does not exist, return -1.

16 | 17 |

You cannot access the mountain array directly. You may only access the array using a MountainArray interface:

18 | 19 |
    20 |
  • MountainArray.get(k) returns the element of the array at index k (0-indexed).
  • 21 |
  • MountainArray.length() returns the length of the array.
  • 22 |
23 | 24 |

Submissions making more than 100 calls to MountainArray.get will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.

25 | 26 |

 

27 |

Example 1:

28 | 29 |
Input: array = [1,2,3,4,5,3,1], target = 3
30 | Output: 2
31 | Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
32 | 33 |

Example 2:

34 | 35 |
Input: array = [0,1,2,4,2,1], target = 3
36 | Output: -1
37 | Explanation: 3 does not exist in the array, so we return -1.
38 | 
39 | 40 |

 

41 |

Constraints:

42 | 43 |
    44 |
  • 3 <= mountain_arr.length() <= 104
  • 45 |
  • 0 <= target <= 109
  • 46 |
  • 0 <= mountain_arr.get(index) <= 109
  • 47 |
48 |
-------------------------------------------------------------------------------- /1431-kids-with-the-greatest-number-of-candies/README.md: -------------------------------------------------------------------------------- 1 |

1431. Kids With the Greatest Number of Candies

Easy


There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

2 | 3 |

Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

4 | 5 |

Note that multiple kids can have the greatest number of candies.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: candies = [2,3,5,1,3], extraCandies = 3
11 | Output: [true,true,true,false,true] 
12 | Explanation: If you give all extraCandies to:
13 | - Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
14 | - Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
15 | - Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
16 | - Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
17 | - Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
18 | 
19 | 20 |

Example 2:

21 | 22 |
Input: candies = [4,2,1,1,2], extraCandies = 1
23 | Output: [true,false,false,false,false] 
24 | Explanation: There is only 1 extra candy.
25 | Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
26 | 
27 | 28 |

Example 3:

29 | 30 |
Input: candies = [12,1,12], extraCandies = 10
31 | Output: [true,false,true]
32 | 
33 | 34 |

 

35 |

Constraints:

36 | 37 |
    38 |
  • n == candies.length
  • 39 |
  • 2 <= n <= 100
  • 40 |
  • 1 <= candies[i] <= 100
  • 41 |
  • 1 <= extraCandies <= 50
  • 42 |
43 |
-------------------------------------------------------------------------------- /Algorithms/Greedy Algorithms/kruskal-algorithm: -------------------------------------------------------------------------------- 1 | // Kruskal's algorithm in Java 2 | 3 | import java.util.*; 4 | 5 | class Graph { 6 | class Edge implements Comparable { 7 | int src, dest, weight; 8 | 9 | public int compareTo(Edge compareEdge) { 10 | return this.weight - compareEdge.weight; 11 | } 12 | }; 13 | 14 | // Union 15 | class subset { 16 | int parent, rank; 17 | }; 18 | 19 | int vertices, edges; 20 | Edge edge[]; 21 | 22 | // Graph creation 23 | Graph(int v, int e) { 24 | vertices = v; 25 | edges = e; 26 | edge = new Edge[edges]; 27 | for (int i = 0; i < e; ++i) 28 | edge[i] = new Edge(); 29 | } 30 | 31 | int find(subset subsets[], int i) { 32 | if (subsets[i].parent != i) 33 | subsets[i].parent = find(subsets, subsets[i].parent); 34 | return subsets[i].parent; 35 | } 36 | 37 | void Union(subset subsets[], int x, int y) { 38 | int xroot = find(subsets, x); 39 | int yroot = find(subsets, y); 40 | 41 | if (subsets[xroot].rank < subsets[yroot].rank) 42 | subsets[xroot].parent = yroot; 43 | else if (subsets[xroot].rank > subsets[yroot].rank) 44 | subsets[yroot].parent = xroot; 45 | else { 46 | subsets[yroot].parent = xroot; 47 | subsets[xroot].rank++; 48 | } 49 | } 50 | 51 | // Applying Krushkal Algorithm 52 | void KruskalAlgo() { 53 | Edge result[] = new Edge[vertices]; 54 | int e = 0; 55 | int i = 0; 56 | for (i = 0; i < vertices; ++i) 57 | result[i] = new Edge(); 58 | 59 | // Sorting the edges 60 | Arrays.sort(edge); 61 | subset subsets[] = new subset[vertices]; 62 | for (i = 0; i < vertices; ++i) 63 | subsets[i] = new subset(); 64 | 65 | for (int v = 0; v < vertices; ++v) { 66 | subsets[v].parent = v; 67 | subsets[v].rank = 0; 68 | } 69 | i = 0; 70 | while (e < vertices - 1) { 71 | Edge next_edge = new Edge(); 72 | next_edge = edge[i++]; 73 | int x = find(subsets, next_edge.src); 74 | int y = find(subsets, next_edge.dest); 75 | if (x != y) { 76 | result[e++] = next_edge; 77 | Union(subsets, x, y); 78 | } 79 | } 80 | for (i = 0; i < e; ++i) 81 | System.out.println(result[i].src + " - " + result[i].dest + ": " + result[i].weight); 82 | } 83 | 84 | public static void main(String[] args) { 85 | int vertices = 6; // Number of vertices 86 | int edges = 8; // Number of edges 87 | Graph G = new Graph(vertices, edges); 88 | 89 | G.edge[0].src = 0; 90 | G.edge[0].dest = 1; 91 | G.edge[0].weight = 4; 92 | 93 | G.edge[1].src = 0; 94 | G.edge[1].dest = 2; 95 | G.edge[1].weight = 4; 96 | 97 | G.edge[2].src = 1; 98 | G.edge[2].dest = 2; 99 | G.edge[2].weight = 2; 100 | 101 | G.edge[3].src = 2; 102 | G.edge[3].dest = 3; 103 | G.edge[3].weight = 3; 104 | 105 | G.edge[4].src = 2; 106 | G.edge[4].dest = 5; 107 | G.edge[4].weight = 2; 108 | 109 | G.edge[5].src = 2; 110 | G.edge[5].dest = 4; 111 | G.edge[5].weight = 4; 112 | 113 | G.edge[6].src = 3; 114 | G.edge[6].dest = 4; 115 | G.edge[6].weight = 3; 116 | 117 | G.edge[7].src = 5; 118 | G.edge[7].dest = 4; 119 | G.edge[7].weight = 3; 120 | G.KruskalAlgo(); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /Algorithms/Sorting Algorithms/Radix sort/Radix_sort.cpp: -------------------------------------------------------------------------------- 1 | // implementation of radix sort using bin/bucket sort 2 | #include 3 | using namespace std; 4 | 5 | // structure for a single linked list to help further in the 6 | // sorting 7 | struct node { 8 | int data; 9 | node* next; 10 | }; 11 | 12 | // function for creating a new node in the linked list 13 | struct node* create(int x) 14 | { 15 | node* temp = new node(); 16 | temp->data = x; 17 | temp->next = NULL; 18 | 19 | return temp; 20 | } 21 | 22 | // utility function to append node in the linked list 23 | // here head is passed by reference, to know more about this 24 | // search pass by reference 25 | void insert(node*& head, int n) 26 | { 27 | if (head == NULL) { 28 | head = create(n); 29 | return; 30 | } 31 | 32 | node* t = head; 33 | while (t->next != NULL) 34 | t = t->next; 35 | t->next = create(n); 36 | } 37 | 38 | // utility function to pop an element from front in the list 39 | // for the sake of stability in sorting 40 | int del(node*& head) 41 | { 42 | if (head == NULL) 43 | return 0; 44 | node* temp = head; 45 | // storing the value of head before updating 46 | int val = head->data; 47 | 48 | // updation of head to next node 49 | head = head->next; 50 | 51 | delete temp; 52 | return val; 53 | } 54 | 55 | // utility function to get the number of digits in the 56 | // max_element 57 | int digits(int n) 58 | { 59 | int i = 1; 60 | if (n < 10) 61 | return 1; 62 | 63 | while (n > (int)pow(10, i)) 64 | i++; 65 | return i; 66 | } 67 | 68 | void radix_sort(vector& arr) 69 | { 70 | // size of the array to be sorted 71 | int sz = arr.size(); 72 | 73 | // getting the maximum element in the array 74 | int max_val = *max_element(arr.begin(), arr.end()); 75 | 76 | // getting digits in the maximum element 77 | int d = digits(max_val); 78 | 79 | // creating buckets to store the pointers 80 | node** bins; 81 | 82 | // array of pointers to linked list of size 10 as 83 | // integers are decimal numbers so they can hold numbers 84 | // from 0-9 only, that's why size of 10 85 | 86 | bins = new node*[10]; 87 | 88 | // initializing the hash array with null to all 89 | for (int i = 0; i < 10; i++) 90 | bins[i] = NULL; 91 | 92 | // first loop working for a constant time only and inner 93 | // loop is iterating through the array to store elements 94 | // of array in the linked list by their digits value 95 | for (int i = 0; i < d; i++) { 96 | for (int j = 0; j < sz; j++) // bins updation 97 | insert(bins[(arr[j] / (int)pow(10, i)) % 10], 98 | arr[j]); 99 | 100 | int x = 0, y = 0; 101 | // write back to the array after each pass 102 | 103 | while (x < 10) { 104 | while (bins[x] != NULL) 105 | arr[y++] = del(bins[x]); 106 | x++; 107 | } 108 | } 109 | } 110 | 111 | // a utility function to print the sorted array 112 | void print(vector arr) 113 | { 114 | for (int i = 0; i < arr.size(); i++) 115 | cout << arr[i] << " "; 116 | cout << endl; 117 | } 118 | 119 | int main() 120 | { 121 | vector arr = { 573, 25, 415, 12, 161, 6 }; 122 | 123 | // function call 124 | radix_sort(arr); 125 | print(arr); 126 | 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // public boolean search(int[] nums, int target) { 3 | // //Brute force solution --> O(n) using two pointers method 4 | // // if start == end then ignore 5 | // //if start > end then end-- 6 | // //if start < end then search 7 | // int start = 0; 8 | // int end = nums.length -1; 9 | // int tempEnd = end; 10 | // int tempStart = start; 11 | // if(nums.length == 1){ 12 | // if (nums[start] == target){ 13 | // return true; 14 | // } 15 | // else{ 16 | // return false; 17 | // } 18 | // } 19 | // while (nums[start] == nums[end]){ 20 | // if(tempEnd - start<= 2){ 21 | // return BinarySearch(start, tempEnd, target, nums); 22 | // } 23 | // end--; 24 | // tempEnd = end; 25 | // start++; 26 | // // problem 27 | // // at last the array will be [1,1] or [1] 28 | 29 | 30 | // } 31 | // while (nums[start] > nums[end]){ 32 | // end--; 33 | // } 34 | 35 | // return BinarySearch(start, end, target, nums) || BinarySearch(end+1, tempEnd, target, nums); 36 | // } 37 | // public boolean BinarySearch(int start, int end, int target, int[] arr){ 38 | // if(arr.length == 1){ 39 | // if(arr[start] == target){ 40 | // return true; 41 | // } 42 | // else{ 43 | // return false; 44 | // } 45 | // } 46 | // while(start <= end){ 47 | // //find the middle element 48 | // // int mid = (start + end) /2; 49 | // // might be possible start + end overflows 50 | // int mid = start + (end - start) / 2; 51 | 52 | // if ( target < arr[mid]){ 53 | // end = mid - 1; 54 | // } 55 | // else if (target > arr[mid]){ 56 | // start = mid + 1; 57 | // } 58 | // else{ 59 | // return true; 60 | // } 61 | // } 62 | // return false; 63 | 64 | // } 65 | public boolean search(int[] nums, int target) { 66 | int start = 0; 67 | int end = nums.length - 1; 68 | 69 | while (start <= end) { 70 | int mid = start + (end - start) / 2; 71 | 72 | if (nums[mid] == target) { 73 | return true; 74 | } 75 | 76 | if (nums[start] == nums[mid] && nums[mid] == nums[end]) { 77 | start++; 78 | end--; 79 | } else if (nums[start] <= nums[mid]) { // Left side is sorted 80 | if (nums[start] <= target && target < nums[mid]) { 81 | end = mid - 1; 82 | } else { 83 | start = mid + 1; 84 | } 85 | } else { // Right side is sorted 86 | if (nums[mid] < target && target <= nums[end]) { 87 | start = mid + 1; 88 | } else { 89 | end = mid - 1; 90 | } 91 | } 92 | } 93 | 94 | return false; 95 | } } 96 | --------------------------------------------------------------------------------