├── README.md ├── Heaps ├── ListNode.java ├── MinLargestElement.java ├── GoodRanges.java ├── RunningMedian.java └── ProductOf3.java ├── Linked List ├── ListNode.java ├── MiddleElement.java ├── RemoveDuplicates.java ├── SwapPairs.java ├── RemoveNthNodeFromEnd.java └── ReverseLinkedList.java ├── Stacks & Queues ├── ListNode.java ├── ReverseBelements.java ├── ReverseStack.java ├── FirstNonRepeatingChar.java ├── SimplifyDirectoryPath.java ├── NintegersContaining123.java ├── RedundantBraces.java ├── NearestSmallerElement.java ├── OrderThem.java ├── SortArrayUsingStack.java └── EvaluateExpression.java ├── Tree Data Structure ├── TreeLinkNode.java ├── TreeNode.java ├── PostorderTraversal.java ├── PreorderTraversal.java ├── BalancedBinaryTree.java ├── KthSmallestElement.java ├── BalancedBST.java ├── CheckBSTOneChild.java ├── GetInput.java ├── ValidBST.java ├── TwoSumBinTree.java ├── InorderTraversal.java ├── RightView.java ├── KDistance.java ├── LeftView.java ├── LevelOrderTraversal.java ├── SymmetricTree.java ├── DifferenceBwlevels.java ├── LargestSubtree.java ├── DistanceBwNodes.java ├── SumOfCommonNodes.java ├── RecoverBST.java └── ZigZagLevelTraversal.java ├── Arrays & Math ├── Solutions │ ├── Interval.java │ ├── NoOfOpenDoors.java │ ├── ToAndFro.java │ ├── FindingPositions.java │ ├── CutTheChocolate.java │ ├── XORQueries.java │ ├── SubsetMedian.java │ ├── EnumeratingGCD.java │ ├── TrailingZerosFactorial.java │ ├── ConsecutiveNumberSum.java │ ├── ExcelColumnNumber.java │ ├── MaxMin.java │ ├── Carotenemia.java │ ├── RepeatedSubtraction.java │ ├── GCD.java │ ├── DivisibilityBy8.java │ ├── ArrayWithConsecutiveElements.java │ ├── MaxChunksSorted.java │ ├── RearrangeTheArray.java │ ├── PowerfulArray.java │ ├── PowersOf3.java │ ├── UniqueElements.java │ ├── OverlappingRectangles.java │ ├── ReverseInteger.java │ ├── LuckyNumbers.java │ ├── ExcelColumnTitle.java │ ├── ReadingNewspaper.java │ ├── SumOfAllSubMatrices.java │ ├── SortThePermutation.java │ ├── FizzBuzz.java │ ├── MinimumSwaps2.java │ ├── RainWaterTrapped.java │ ├── NthMagicNumber.java │ ├── WaveArray.java │ ├── ComputeNcR.java │ ├── MissingArray.java │ ├── DivisorGame.java │ ├── MaximumMinimumLogic.java │ ├── SumTheDifference.java │ ├── MinSwaps.java │ ├── NextPermutation.java │ ├── SpiralOrderMatrix2.java │ ├── CountDivisors.java │ ├── MaxAbsoluteDifference.java │ ├── MaxChunksSorted2.java │ ├── PrimeSum.java │ ├── PrimeSubsequences.java │ ├── Pubg.java │ ├── SolveSubsequences.java │ ├── CountPairs.java │ ├── DeleteElements.java │ ├── RemoveElements.java │ ├── LargestCoprimeDivisor.java │ ├── LargestNumber.java │ ├── ReversePairs.java │ ├── SrchRowColWiseSortedMatrix.java │ ├── MinOperationMatrixEqual.java │ ├── A_B.java │ ├── CoPrimeAgain.java │ ├── DeleteOne.java │ ├── CountOfRangeSum.java │ └── ChristmasTree.java └── Problems │ ├── RearrangeTheArray.md │ ├── RainWaterTrapped.md │ ├── SpiralOrderMatrix2.md │ ├── MaxChunksSorted2.md │ ├── SumOfAllSubMatrices.md │ ├── SetMatrixZero.md │ ├── FizzBuzz.md │ ├── MaxAbsoluteDifference.md │ ├── MergeIntervals.md │ ├── MaxConsecutiveGap.md │ ├── WaveArray.md │ ├── SrchRowColWiseSortedMatrix.md │ ├── Carotenemia.md │ └── MinOperationMatrixEqual.md ├── Dynamic Programming ├── Fibonacci.java ├── CoinSumInfinite.java ├── DistinctSubsequence.java ├── LongestValidParenthesis.java ├── LongPallinSubsequence.java ├── PickFromBothSides.java ├── LCS.java ├── MinSumPath.java ├── LongestFibonacciSubset.java ├── UnstablePermutation.java ├── MatrixChainMultiplication.java ├── UniquePaths.java ├── WordBreak.java ├── EditDistance.java ├── NDigitNumbers.java ├── MinSumPathTriangle.java ├── PallindromePartitioning2.java └── DungeonPrincess.java ├── Greedy Algorithm ├── AnotherCoinProblem.java ├── StackingCubes.java ├── Bulbs.java ├── BinaryStrings.java ├── MiceToHoles.java ├── Seats.java ├── DistributeCandy.java ├── CandiesInBox.java ├── GasStation.java └── ShipCompany.java ├── Binary Search ├── MaxHeightStaircase.java ├── SquareRoot.java ├── SingleElementSortedArray.java ├── PeakElement.java ├── SmallestGoodBase.java ├── MatrixSearch.java ├── FindSmallestAgain.java └── AggressiveCows.java ├── Strings ├── ReverseTheString.java ├── EqualStrings.java ├── IntegerToRoman.java ├── CountAndSay.java ├── SameFormatString.java ├── OrderedSubstring.java ├── MinCharsToPallindrome.java ├── LongestCommonPrefix.java ├── RegroupWithMinSwaps.java ├── StrStr.java ├── AddBinary.java ├── KthCharacter.java └── RomanToInteger.java ├── Bit Manipulation ├── InterestingArray.java ├── SingleNumber.java ├── FindMinXOR.java ├── ReverseBits.java ├── SingleNumber2.java ├── DivideIntegers.java └── DiffBitsSumPairwise.java ├── Two Pointers ├── HideBoxes.java ├── CountPairsGivenSum.java ├── ContainerWithMostWater.java ├── Count_Rectangles.java ├── CountSubarrays.java ├── ThreeSumClosest.java ├── CountPairsGivenSum2.java ├── Array3Pointers.java └── SubArraySum.java ├── Backtracking ├── GrayCode.java ├── Sixlets.java └── KthPermutationSequence.java ├── Hashing ├── SubarrayWithZeroSum.java ├── ColorfulNumber.java ├── ReplicatingSubstring.java ├── ShaggyAndDistances.java ├── LongestSubstringWithoutRepeat.java ├── LongestConsecutiveSequence.java ├── CountRightTriangles.java ├── Permutations.java └── CountRectangles.java └── Recursion ├── Combinations.java └── Subset.java /README.md: -------------------------------------------------------------------------------- 1 | # Scaler Academy 2 | 3 | ### [Batch - Jan 2020 - SuperX](https://www.scaler.com/academy/mentee-dashboard/todos) 4 | -------------------------------------------------------------------------------- /Heaps/ListNode.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day70_Apr17; 2 | 3 | class ListNode { 4 | public int val; 5 | public ListNode next; 6 | 7 | ListNode(int x) { 8 | val = x; 9 | next = null; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Linked List/ListNode.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day44_Mar20; 2 | 3 | public class ListNode { 4 | public int val; 5 | public ListNode next; 6 | 7 | ListNode(int x) { 8 | val = x; 9 | next = null; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Stacks & Queues/ListNode.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day44_Mar20; 2 | 3 | public class ListNode { 4 | public int val; 5 | public ListNode next; 6 | 7 | ListNode(int x) { 8 | val = x; 9 | next = null; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Tree Data Structure/TreeLinkNode.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day56_Apr3; 2 | 3 | public class TreeLinkNode { 4 | int val; 5 | TreeLinkNode left, right, next; 6 | 7 | TreeLinkNode(int x) { 8 | val = x; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Tree Data Structure/TreeNode.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day54_Apr1; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | left = null; 11 | right = null; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/Interval.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day07_Feb3; 2 | 3 | public class Interval { 4 | int start; 5 | int end; 6 | 7 | Interval() { 8 | start = 0; 9 | end = 0; 10 | } 11 | 12 | Interval(int s, int e) { 13 | start = s; 14 | end = e; 15 | } 16 | } -------------------------------------------------------------------------------- /Dynamic Programming/Fibonacci.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Fibonacci { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s = new Scanner(System.in); 8 | int A[] = new int[45]; 9 | A[0] = 0; 10 | A[1] = 1; 11 | for (int i = 2; i < 45; i++) 12 | A[i] = A[i - 1] + A[i - 2]; 13 | 14 | System.out.println(A[s.nextInt()]); 15 | s.close(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/NoOfOpenDoors.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day11_Feb7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class NoOfOpenDoors { 6 | 7 | public int solve(int A) { 8 | return (int) Math.sqrt(A); 9 | } 10 | 11 | public static void main(String[] args) { 12 | // TODO Auto-generated method stub 13 | Scanner s = new Scanner(System.in); 14 | int n = s.nextInt(); 15 | NoOfOpenDoors od = new NoOfOpenDoors(); 16 | System.out.println(od.solve(n)); 17 | s.close(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ToAndFro.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day13_Feb10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ToAndFro { 6 | 7 | public int solve(int A) { 8 | if (A % 2 == 1) 9 | return A / 2; 10 | return (A / 2 - 1); 11 | } 12 | 13 | public static void main(String[] args) { 14 | // TODO Auto-generated method stub 15 | Scanner s = new Scanner(System.in); 16 | int a = s.nextInt(); 17 | ToAndFro tf = new ToAndFro(); 18 | System.out.println(tf.solve(a)); 19 | s.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/FindingPositions.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class FindingPositions { 6 | 7 | public int solve(int A) { 8 | int i = (int) (Math.log(A) / Math.log(2)); 9 | 10 | return (int) Math.pow(2, i); 11 | } 12 | 13 | public static void main(String[] args) { 14 | // TODO Auto-generated method stub 15 | Scanner s = new Scanner(System.in); 16 | int n = s.nextInt(); 17 | FindingPositions fp = new FindingPositions(); 18 | System.out.println(fp.solve(n)); 19 | s.close(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/CutTheChocolate.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day13_Feb10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CutTheChocolate { 6 | 7 | public int solve(int A, int B) { 8 | if ((A % 2 * B % 2) % 2 == 1) 9 | return 0; 10 | return 1; 11 | } 12 | 13 | public static void main(String[] args) { 14 | // TODO Auto-generated method stub 15 | Scanner s = new Scanner(System.in); 16 | int a = s.nextInt(); 17 | int b = s.nextInt(); 18 | CutTheChocolate ctc = new CutTheChocolate(); 19 | System.out.println(ctc.solve(a, b)); 20 | s.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/XORQueries.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day60_Apr7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class XORQueries { 6 | 7 | public int solve(int A, int B, int C) { 8 | int max = Integer.MIN_VALUE; 9 | 10 | for (int i = B; i <= C; i++) { 11 | max = Math.max(max, A ^ i); 12 | } 13 | return max; 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | Scanner s = new Scanner(System.in); 19 | System.out.println(new XORQueries().solve(s.nextInt(), s.nextInt(), s.nextInt())); 20 | s.close(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Greedy Algorithm/AnotherCoinProblem.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.Scanner; 4 | 5 | public class AnotherCoinProblem { 6 | 7 | public int solve(int A) { 8 | int count = 0; 9 | int x = 1; 10 | 11 | while (x <= A) 12 | x *= 5; 13 | 14 | while (A > 0) { 15 | count += A / x; 16 | A %= x; 17 | x /= 5; 18 | } 19 | return count; 20 | } 21 | 22 | public static void main(String[] args) { 23 | // TODO Auto-generated method stub 24 | Scanner s = new Scanner(System.in); 25 | System.out.print(new AnotherCoinProblem().solve(s.nextInt())); 26 | s.close(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Greedy Algorithm/StackingCubes.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.Scanner; 4 | 5 | public class StackingCubes { 6 | 7 | public int solve(int A) { 8 | int i, count = 1; 9 | int n = (int) Math.sqrt(A); 10 | 11 | for (i = 2; i <= n; i++) { 12 | if (i * i == A) 13 | count++; 14 | else if (A % i == 0) 15 | count += 2; 16 | } 17 | 18 | return count; 19 | } 20 | 21 | public static void main(String[] args) { 22 | // TODO Auto-generated method stub 23 | Scanner s = new Scanner(System.in); 24 | System.out.print(new StackingCubes().solve(s.nextInt())); 25 | s.close(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/SubsetMedian.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day114_Jun10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SubsetMedian { 6 | 7 | public int solve(int[] A) { 8 | int sum = 0; 9 | 10 | for (int i = 0; i < A.length; i++) 11 | sum += A[i]; 12 | 13 | return sum; 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | Scanner s = new Scanner(System.in); 19 | int n = s.nextInt(); 20 | int A[] = new int[n]; 21 | for (int i = 0; i < n; i++) 22 | A[i] = s.nextInt(); 23 | System.out.println(new PartitionSort().solve(A)); 24 | s.close(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/EnumeratingGCD.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class EnumeratingGCD { // GCD of consecutive numbers is always 1 6 | public String solve(String A, String B) { 7 | 8 | if (A.equals(B)) 9 | return A; 10 | return "1"; 11 | } 12 | 13 | public static void main(String[] args) { 14 | // TODO Auto-generated method stub 15 | Scanner s = new Scanner(System.in); 16 | String A, B; 17 | 18 | A = s.nextLine(); 19 | B = s.nextLine(); 20 | 21 | EnumeratingGCD c = new EnumeratingGCD(); 22 | System.out.println(c.solve(A, B)); 23 | 24 | s.close(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Binary Search/MaxHeightStaircase.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day17_Feb14; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MaxHeightStaircase { 6 | 7 | public int solve(int A) { 8 | 9 | long a = 2 * (long) A; 10 | long b = (long) Math.sqrt(a); 11 | 12 | if (b * (b + 1) <= a) 13 | return (int) b; 14 | else 15 | return (int) (b - 1); 16 | } 17 | 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | Scanner s = new Scanner(System.in); 21 | int n = s.nextInt(); 22 | 23 | MaxHeightStaircase ms = new MaxHeightStaircase(); 24 | System.out.println(ms.solve(n)); 25 | s.close(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Strings/ReverseTheString.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day40_Mar16; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReverseTheString { 6 | 7 | public String solve(String A) { 8 | String ans[] = A.trim().split(" "); 9 | String S = ""; 10 | 11 | for (int i = ans.length - 1; i >= 0; i--) 12 | S += ans[i] + " "; 13 | 14 | return S.trim(); 15 | } 16 | 17 | public static void main(String[] args) { 18 | // TODO Auto-generated method stub 19 | Scanner s = new Scanner(System.in); 20 | String A; 21 | 22 | A = s.nextLine(); 23 | 24 | ReverseTheString rts = new ReverseTheString(); 25 | System.out.print(rts.solve(A)); 26 | s.close(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/TrailingZerosFactorial.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day11_Feb7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class TrailingZerosFactorial { 6 | 7 | public int trailingZeroes(int A) { 8 | 9 | int cnt = 0; 10 | int val = 5; 11 | 12 | while (val <= A) { 13 | cnt += A / val; 14 | val *= 5; 15 | } 16 | 17 | return cnt; 18 | } 19 | 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | Scanner s = new Scanner(System.in); 23 | int n = s.nextInt(); 24 | TrailingZerosFactorial ln = new TrailingZerosFactorial(); 25 | System.out.println(ln.trailingZeroes(n)); 26 | s.close(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ConsecutiveNumberSum.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day114_Jun10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ConsecutiveNumberSum { 6 | 7 | public int solve(int A) { 8 | int cnt = 0; 9 | double sum; 10 | 11 | for (int i = 0; i * (i + 1) / 2 < A; i++) { 12 | sum = ((double) A - ((double) i * (i + 1) / 2)) / (i + 1); 13 | if (sum - (int) sum == 0) 14 | cnt++; 15 | } 16 | return cnt; 17 | } 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | Scanner s = new Scanner(System.in); 22 | System.out.println(new ConsecutiveNumberSum().solve(s.nextInt())); 23 | s.close(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Greedy Algorithm/Bulbs.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bulbs { 6 | 7 | public int bulbs(int[] A) { 8 | int cnt = 0; 9 | 10 | for (int i = 0; i < A.length; i++) { 11 | if ((A[i] == 0 && cnt % 2 == 0) || (A[i] == 1 && cnt % 2 == 1)) { 12 | cnt++; 13 | } 14 | } 15 | return cnt; 16 | } 17 | 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | Scanner s = new Scanner(System.in); 21 | int n = s.nextInt(); 22 | int A[] = new int[n]; 23 | for (int i = 0; i < n; i++) 24 | A[i] = s.nextInt(); 25 | 26 | System.out.println(new Bulbs().bulbs(A)); 27 | s.close(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/RearrangeTheArray.md: -------------------------------------------------------------------------------- 1 | # Rearrange the array 2 | Given an array of integers A of size N that is a permutation of [0, 1, 2, ..., (N-1)]. 3 | 4 | Rearrange the array such that A[i] = j is changed to A[j] = i for all i and j form 0 to N-1. 5 | 6 | Note: Try solving this with O(1) extra space. 7 | 8 | 9 | ### Input Format 10 | 11 | The only argument given is the integer array A. 12 | ### Output Format 13 | 14 | Return the rearranged array A. 15 | ### Constraints 16 | 17 | 1 <= N <= 100000 18 | 0 <= A[i] < N 19 | ### For Example 20 | 21 | ### Input 1: 22 | A = [1, 2, 3, 4, 0] 23 | ### Output 1: 24 | [4, 0, 1, 2, 3] 25 | 26 | ### Input 2: 27 | A = [2, 0, 1, 3] 28 | ### Output 2: 29 | [1, 2, 0, 3] 30 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ExcelColumnNumber.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day13_Feb10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ExcelColumnNumber { 6 | 7 | public int titleToNumber(String A) { 8 | int pow = 1; 9 | int val, sum = 0; 10 | 11 | for (int i = A.length() - 1; i >= 0; i--) { 12 | val = A.charAt(i) - 64; 13 | sum += pow * val; 14 | pow *= 26; 15 | } 16 | 17 | return sum; 18 | } 19 | 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | Scanner s = new Scanner(System.in); 23 | String str = s.nextLine(); 24 | ExcelColumnNumber ecn = new ExcelColumnNumber(); 25 | System.out.println(ecn.titleToNumber(str)); 26 | s.close(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MaxMin.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day29_Mar2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class MaxMin { 7 | 8 | public int solve(int[] A, int B) { 9 | 10 | Arrays.sort(A); 11 | int l = A.length; 12 | 13 | return A[l - B] - A[B - 1]; 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | Scanner s = new Scanner(System.in); 19 | int n, A[]; 20 | 21 | n = s.nextInt(); 22 | A = new int[n]; 23 | for (int i = 0; i < n; i++) { 24 | A[i] = s.nextInt(); 25 | } 26 | int b = s.nextInt(); 27 | 28 | MaxMin mm = new MaxMin(); 29 | System.out.println(mm.solve(A, b)); 30 | s.close(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/Carotenemia.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Carotenemia { 4 | 5 | public int solve(int[] A, int B) { 6 | 7 | for (int i = 0; i < A.length; i++) { 8 | B -= A[i]; 9 | if (B <= 0) { 10 | return i; 11 | } 12 | } 13 | if (B > 0) { 14 | return -1; 15 | } 16 | return 0; 17 | } 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | 22 | Scanner s = new Scanner(System.in); 23 | int n, A[], B; 24 | n = s.nextInt(); 25 | A = new int[n]; 26 | for (int i = 0; i < n; i++) { 27 | A[i] = s.nextInt(); 28 | } 29 | B = s.nextInt(); 30 | 31 | Carotenemia c = new Carotenemia(); 32 | System.out.println(c.solve(A, B)); 33 | s.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/RepeatedSubtraction.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class RepeatedSubtraction { 6 | public int getFinal(int A, int B) { 7 | 8 | while (A != 0 && B != 0 && A != B) { 9 | if (A > B) { 10 | A = (A % B == 0) ? B : A % B; 11 | } else { 12 | B = (B % A == 0) ? A : B % A; 13 | } 14 | } 15 | return A + B; 16 | } 17 | 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | Scanner s = new Scanner(System.in); 21 | int A, B; 22 | 23 | A = s.nextInt(); 24 | B = s.nextInt(); 25 | 26 | RepeatedSubtraction c = new RepeatedSubtraction(); 27 | System.out.println(c.getFinal(A, B)); 28 | 29 | s.close(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/GCD.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class GCD { 6 | 7 | public int gcd(int A, int B) { 8 | if (A == 1 || B == 1) 9 | return 1; 10 | if (A == 0 || B == 0) 11 | return Math.max(A, B); 12 | 13 | if (A > B) 14 | return gcd(A % B, B); 15 | if (A < B) 16 | return gcd(A, B % A); 17 | 18 | if (A == B) 19 | return A; 20 | 21 | return -1; 22 | } 23 | 24 | public static void main(String[] args) { 25 | // TODO Auto-generated method stub 26 | Scanner s = new Scanner(System.in); 27 | int B, A; 28 | 29 | A = s.nextInt(); 30 | B = s.nextInt(); 31 | 32 | GCD c = new GCD(); 33 | System.out.println(c.gcd(A, B)); 34 | 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Bit Manipulation/InterestingArray.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day15_Feb12; 2 | 3 | import java.util.Scanner; 4 | 5 | public class InterestingArray { 6 | 7 | public String solve(int[] A) { 8 | int sum = 0; 9 | 10 | for (int i = 0; i < A.length; i++) { 11 | sum += A[i]; 12 | } 13 | 14 | if (sum % 2 == 1) 15 | return "No"; 16 | else 17 | return "Yes"; 18 | } 19 | 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | Scanner s = new Scanner(System.in); 23 | int n = s.nextInt(); 24 | int A[] = new int[n]; 25 | 26 | for (int i = 0; i < n; i++) 27 | A[i] = s.nextInt(); 28 | 29 | InterestingArray ia = new InterestingArray(); 30 | System.out.println(ia.solve(A)); 31 | s.close(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Bit Manipulation/SingleNumber.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day15_Feb12; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SingleNumber { 6 | 7 | public int singleNumber(final int[] A) { 8 | if (A.length == 1) 9 | return A[0]; 10 | 11 | int ans = A[0] ^ A[1]; 12 | 13 | for (int i = 2; i < A.length; i++) { 14 | ans ^= A[i]; 15 | } 16 | 17 | return ans; 18 | } 19 | 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | Scanner s = new Scanner(System.in); 23 | int n = s.nextInt(); 24 | int A[] = new int[n]; 25 | for (int i = 0; i < n; i++) 26 | A[i] = s.nextInt(); 27 | 28 | SingleNumber sn = new SingleNumber(); 29 | System.out.println(sn.singleNumber(A)); 30 | s.close(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/DivisibilityBy8.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day42_Mar18; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DivisibilityBy8 { 6 | 7 | public int solve(String A) { 8 | int l = A.length(); 9 | int val = 0; 10 | 11 | if (l >= 3) { 12 | for (int i = l - 3; i < l; i++) 13 | val = val * 10 + (int) A.charAt(i); 14 | } else 15 | val = Integer.parseInt(A); 16 | 17 | if (val % 8 == 0) 18 | return 1; 19 | else 20 | return 0; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | Scanner s = new Scanner(System.in); 26 | String A; 27 | A = s.nextLine(); 28 | DivisibilityBy8 div8 = new DivisibilityBy8(); 29 | System.out.println(div8.solve(A)); 30 | s.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ArrayWithConsecutiveElements.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day29_Mar2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class ArrayWithConsecutiveElements { 7 | 8 | public int solve(int[] A) { 9 | Arrays.parallelSort(A); 10 | for (int i = 1; i < A.length; i++) { 11 | if (A[i] != A[i - 1] + 1) 12 | return 0; 13 | } 14 | return 1; 15 | } 16 | 17 | public static void main(String[] args) { 18 | // TODO Auto-generated method stub 19 | Scanner s = new Scanner(System.in); 20 | int n, A[]; 21 | 22 | n = s.nextInt(); 23 | A = new int[n]; 24 | for (int i = 0; i < n; i++) { 25 | A[i] = s.nextInt(); 26 | } 27 | System.out.print(new ArrayWithConsecutiveElements().solve(A)); 28 | s.close(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/RainWaterTrapped.md: -------------------------------------------------------------------------------- 1 | # Rain Water Trapped 2 | ### Problem Description 3 | 4 | Given a vector A of non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. 5 | 6 | 7 | 8 | ### Problem Constraints 9 | 1 <= |A| <= 100000 10 | 11 | 12 | 13 | ### Input Format 14 | First and only argument is the vector A 15 | 16 | 17 | 18 | ### Output Format 19 | Return one integer, the answer to the question 20 | 21 | 22 | 23 | ### Example Input 24 | - Input 1: A = [0, 1, 0, 2] 25 | - Input 2: A = [1, 2] 26 | 27 | 28 | ### Example Output 29 | - Output 1: 1 30 | - Output 2: 0 31 | 32 | 33 | ### Example Explanation 34 | - Explanation 1: 1 unit is trapped on top of the 3rd element. 35 | - Explanation 2: No water is trapped. 36 | 37 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MaxChunksSorted.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day31_Mar4; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MaxChunksSorted { 6 | 7 | public int solve(int[] A) { 8 | int chunks = 0; 9 | int max = A[0]; 10 | for (int i = 0; i < A.length; i++) { 11 | max = Math.max(max, A[i]); 12 | if (max == i) 13 | chunks++; 14 | } 15 | return chunks; 16 | } 17 | 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | Scanner s = new Scanner(System.in); 21 | int n, A[]; 22 | 23 | n = s.nextInt(); 24 | A = new int[n]; 25 | for (int i = 0; i < n; i++) { 26 | A[i] = s.nextInt(); 27 | } 28 | 29 | MaxChunksSorted c = new MaxChunksSorted(); 30 | System.out.println(c.solve(A)); 31 | s.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/RearrangeTheArray.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day07_Feb3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class RearrangeTheArray { 6 | 7 | public int[] solve(int[] A) { 8 | int B[] = new int[A.length]; 9 | 10 | for (int i = 0; i < A.length; i++) { 11 | B[A[i]] = i; 12 | } 13 | return B; 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | Scanner s = new Scanner(System.in); 19 | int n, A[]; 20 | 21 | n = s.nextInt(); 22 | A = new int[n]; 23 | for (int i = 0; i < n; i++) { 24 | A[i] = s.nextInt(); 25 | } 26 | 27 | RearrangeTheArray c = new RearrangeTheArray(); 28 | A = c.solve(A); 29 | for (int i = 0; i < n; i++) { 30 | System.out.print(A[i] + " "); 31 | } 32 | s.close(); 33 | } 34 | } -------------------------------------------------------------------------------- /Strings/EqualStrings.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day73_Apr21; 2 | 3 | import java.util.Scanner; 4 | 5 | public class EqualStrings { 6 | 7 | public int solve(String A, String B) { 8 | if (A.length() != B.length()) 9 | return 0; 10 | 11 | int a[] = new int[26]; 12 | int n = A.length(); 13 | 14 | for (int i = 0; i < n; i++) 15 | a[A.charAt(i) - 'a']++; 16 | 17 | for (int i = 0; i < n; i++) 18 | a[B.charAt(i) - 'a']++; 19 | 20 | for (int i = 0; i < 26; i++) 21 | if (a[i] % 2 == 1) 22 | return 0; 23 | 24 | return 1; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | System.out.println(new EqualStrings().solve(s.next(), s.next())); 31 | s.close(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Bit Manipulation/FindMinXOR.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day15_Feb12; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class FindMinXOR { 7 | 8 | public int findMinXor(int[] A) { 9 | 10 | Arrays.sort(A); 11 | int minxor = A[0] ^ A[1]; 12 | for (int i = 1; i < A.length - 1; i++) { 13 | minxor = minxor > (A[i] ^ A[i + 1]) ? A[i] ^ A[i + 1] : minxor; 14 | } 15 | 16 | return minxor; 17 | } 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | Scanner s = new Scanner(System.in); 22 | 23 | int n = s.nextInt(); 24 | int A[] = new int[n]; 25 | for (int i = 0; i < n; i++) 26 | A[i] = s.nextInt(); 27 | FindMinXOR mx = new FindMinXOR(); 28 | System.out.println(mx.findMinXor(A)); 29 | s.close(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Bit Manipulation/ReverseBits.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day15_Feb12; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReverseBits { 6 | 7 | public long reverse(long a) { 8 | 9 | long ans = 0; 10 | int x[] = new int[32]; 11 | int i = 0; 12 | 13 | while (a > 0) { 14 | x[i++] = (int) (a % 2); 15 | a /= 2; 16 | } 17 | int p = 31; 18 | for (i = 0; i < 32; i++) { 19 | System.out.print(x[i]); 20 | ans += x[i] * Math.pow(2, p--); 21 | } 22 | System.out.println(); 23 | 24 | return ans; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | long n = s.nextLong(); 31 | ReverseBits rb = new ReverseBits(); 32 | System.out.println(rb.reverse(n)); 33 | s.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Tree Data Structure/PostorderTraversal.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day54_Apr1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Stack; 5 | 6 | public class PostorderTraversal { 7 | 8 | public ArrayList postorderTraversal(TreeNode A) { 9 | ArrayList ans = new ArrayList<>(); 10 | Stack S = new Stack<>(); 11 | TreeNode tmp; 12 | S.push(A); 13 | 14 | while (!S.isEmpty()) { 15 | tmp = S.pop(); 16 | ans.add(0, tmp.val); 17 | if (tmp.left != null) 18 | S.add(tmp.left); 19 | if (tmp.right != null) 20 | S.add(tmp.right); 21 | } 22 | return ans; 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | System.out.println(new PostorderTraversal().postorderTraversal(new GetInput().getInput())); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Tree Data Structure/PreorderTraversal.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day54_Apr1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Stack; 5 | 6 | public class PreorderTraversal { 7 | 8 | public ArrayList preorderTraversal(TreeNode A) { 9 | ArrayList ans = new ArrayList<>(); 10 | TreeNode tmp; 11 | Stack S = new Stack<>(); 12 | S.push(A); 13 | 14 | while (!S.isEmpty()) { 15 | tmp = S.pop(); 16 | ans.add(tmp.val); 17 | if (tmp.right != null) 18 | S.push(tmp.right); 19 | if (tmp.left != null) 20 | S.push(tmp.left); 21 | } 22 | return ans; 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | System.out.println(new PreorderTraversal().preorderTraversal(new GetInput().getInput())); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Dynamic Programming/CoinSumInfinite.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day80_Apr29; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CoinSumInfinite { 6 | 7 | public int coinchange2(int[] A, int B) { 8 | int[] dp = new int[B + 1]; 9 | int mod = 1000007; 10 | dp[0] = 1; 11 | 12 | for (int j = 0; j < A.length; j++) { 13 | for (int i = A[j]; i <= B; i++) { 14 | dp[i] = (dp[i] + dp[i - A[j]]) % mod; 15 | } 16 | } 17 | return dp[B]; 18 | } 19 | 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | Scanner s = new Scanner(System.in); 23 | int n = s.nextInt(); 24 | int A[] = new int[n]; 25 | for (int i = 0; i < n; i++) 26 | A[i] = s.nextInt(); 27 | System.out.println(new CoinSumInfinite().coinchange2(A, s.nextInt())); 28 | s.close(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Binary Search/SquareRoot.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day21_Feb19; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SquareRoot { 6 | 7 | public int sqrt(int A) { 8 | 9 | long s, e, m = 1, val; 10 | s = 1; 11 | e = A; 12 | while (s <= e) { 13 | m = (s + e) / 2; 14 | val = m * m; 15 | if (val == A) 16 | return (int) m; 17 | else if (val > A) 18 | e = m - 1; 19 | else 20 | s = m + 1; 21 | } 22 | 23 | if (m * m > A) 24 | return (int) (m - 1); 25 | else 26 | return (int) m; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | 32 | Scanner s = new Scanner(System.in); 33 | int n = s.nextInt(); 34 | 35 | SquareRoot sr = new SquareRoot(); 36 | System.out.println(sr.sqrt(n)); 37 | s.close(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Two Pointers/HideBoxes.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day35_Mar8; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class HideBoxes { 7 | 8 | public int solve(int[] A) { 9 | int cnt = 0, x, y; 10 | Arrays.sort(A); 11 | x = A.length / 2 - 1; 12 | y = A.length - 1; 13 | 14 | while (x >= 0) { 15 | if (A[y] >= 2 * A[x]) { 16 | cnt++; 17 | y--; 18 | } 19 | x--; 20 | } 21 | return A.length - cnt; 22 | } 23 | 24 | public static void main(String[] args) { 25 | // TODO Auto-generated method stub 26 | Scanner s = new Scanner(System.in); 27 | int n = s.nextInt(); 28 | int A[] = new int[n]; 29 | for (int i = 0; i < n; i++) 30 | A[i] = s.nextInt(); 31 | 32 | HideBoxes hb = new HideBoxes(); 33 | System.out.println(hb.solve(A)); 34 | s.close(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/PowerfulArray.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day47_Mar24; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PowerfulArray { 6 | 7 | public int solve(int[] A, int[] B) { 8 | 9 | double p1 = 1, p2 = 1; 10 | 11 | for (int i = 0; i < A.length; i++) { 12 | p1 += Math.log(A[i]); 13 | p2 += Math.log(B[i]); 14 | } 15 | return p1 >= p2 ? 1 : 0; 16 | } 17 | 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | Scanner s = new Scanner(System.in); 21 | int n = s.nextInt(); 22 | 23 | int A[] = new int[n]; 24 | for (int i = 0; i < n; i++) 25 | A[i] = s.nextInt(); 26 | 27 | int B[] = new int[n]; 28 | for (int i = 0; i < n; i++) 29 | B[i] = s.nextInt(); 30 | 31 | System.out.println(new PowerfulArray().solve(A, B)); 32 | s.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Backtracking/GrayCode.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day27_Feb28; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class GrayCode { 7 | 8 | ArrayList ans = new ArrayList<>(); 9 | 10 | void create(int n, int a) { 11 | if (n == a) 12 | return; 13 | 14 | for (int i = ans.size() - 1; i >= 0; i--) { 15 | int val = (1 << n) + ans.get(i); 16 | ans.add(val); 17 | } 18 | create(n + 1, a); 19 | } 20 | 21 | public ArrayList grayCode(int a) { 22 | 23 | ans.add(0); 24 | ans.add(1); 25 | 26 | create(1, a); 27 | 28 | return ans; 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | System.out.println(new GrayCode().grayCode(s.nextInt())); 35 | s.close(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Dynamic Programming/DistinctSubsequence.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day78_Apr27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DistinctSubsequence { 6 | 7 | public int numDistinct(String A, String B) { 8 | int m = A.length(); 9 | int n = B.length(); 10 | 11 | int[][] dp = new int[n + 1][m + 1]; 12 | 13 | for (int j = 0; j < m; j++) 14 | dp[0][j] = 1; 15 | 16 | for (int i = 1; i <= n; i++) 17 | for (int j = 1; j <= m; j++) 18 | dp[i][j] = A.charAt(j - 1) == B.charAt(i - 1) ? dp[i][j - 1] + dp[i - 1][j - 1] : dp[i][j - 1]; 19 | 20 | return dp[n][m]; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | Scanner s = new Scanner(System.in); 26 | System.out.println(new DistinctSubsequence().numDistinct(s.next(), s.next())); 27 | s.close(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Hashing/SubarrayWithZeroSum.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day36_Mar11; 2 | 3 | import java.util.HashSet; 4 | import java.util.Scanner; 5 | 6 | public class SubarrayWithZeroSum { 7 | 8 | public int solve(int[] A) { 9 | 10 | HashSet hs = new HashSet<>(); 11 | Long sum = 0l; 12 | for (int i = 0; i < A.length; i++) { 13 | sum += A[i]; 14 | if (hs.contains(sum) || sum == 0) 15 | return 1; 16 | hs.add(sum); 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | public static void main(String[] args) { 23 | // TODO Auto-generated method stub 24 | Scanner s = new Scanner(System.in); 25 | int n = s.nextInt(); 26 | int A[] = new int[n]; 27 | 28 | for (int i = 0; i < n; i++) 29 | A[i] = s.nextInt(); 30 | 31 | SubarrayWithZeroSum sa = new SubarrayWithZeroSum(); 32 | System.out.print(sa.solve(A)); 33 | s.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Strings/IntegerToRoman.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day40_Mar16; 2 | 3 | import java.util.Scanner; 4 | 5 | public class IntegerToRoman { 6 | 7 | public String intToRoman(int A) { 8 | 9 | int B[] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 }; 10 | String S[] = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" }; 11 | int i = 12; 12 | String ans = ""; 13 | 14 | while (i >= 0) { 15 | if (A >= B[i]) { 16 | ans += S[i]; 17 | A -= B[i]; 18 | } else 19 | i--; 20 | } 21 | 22 | return ans; 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | Scanner s = new Scanner(System.in); 28 | int A; 29 | 30 | A = s.nextInt(); 31 | IntegerToRoman itr = new IntegerToRoman(); 32 | System.out.print(itr.intToRoman(A)); 33 | s.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/SpiralOrderMatrix2.md: -------------------------------------------------------------------------------- 1 | # Spiral Order Matrix II 2 | ### Problem Description 3 | 4 | Given an integer A, generate a square matrix filled with elements from 1 to A2 in spiral order. 5 | 6 | 7 | 8 | ### Problem Constraints 9 | 1 <= A <= 1000 10 | 11 | 12 | 13 | ### Input Format 14 | First and only argument is integer A 15 | 16 | 17 | ### Output Format 18 | Return a 2-D matrix which consists of the elements in spiral order. 19 | 20 | 21 | 22 | ### Example Input 23 | ### Input 1: 24 | 25 | 1 26 | ### Input 2: 27 | 28 | 2 29 | 30 | 31 | ### Example Output 32 | ### Output 1: 33 | 34 | [ [1] ] 35 | ### Output 2: 36 | 37 | [ [1, 2], [4, 3] ] 38 | 39 | 40 | ### Example Explanation 41 | ### Explanation 1: 42 | 43 | Only 1 is to be arranged. 44 | 45 | ### Explanation 2: 46 | 1 --> 2 47 | | 48 | | 49 | 4<--- 3 50 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/PowersOf3.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day24_Feb25; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class PowersOf3 { 7 | 8 | public ArrayList solve(int A) { 9 | 10 | ArrayList B = new ArrayList<>(); 11 | int po3[] = new int[19]; 12 | int i; 13 | for (i = 0; i < 19; i++) 14 | po3[i] = (int) Math.pow(3, i); 15 | 16 | i--; 17 | while (i >= 0) { 18 | if (A >= po3[i]) { 19 | B.add(po3[i]); 20 | A -= po3[i]; 21 | } else 22 | i--; 23 | } 24 | 25 | return B; 26 | } 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | Scanner s = new Scanner(System.in); 31 | int n; 32 | n = s.nextInt(); 33 | 34 | PowersOf3 po3 = new PowersOf3(); 35 | System.out.print(po3.solve(n)); 36 | s.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/UniqueElements.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day31_Mar4; 2 | 3 | import java.util.HashSet; 4 | import java.util.Scanner; 5 | 6 | public class UniqueElements { 7 | 8 | public int solve(int[] A) { 9 | HashSet hs = new HashSet<>(); 10 | int cnt = 0; 11 | 12 | for (int i = 0; i < A.length; i++) { 13 | int val = A[i]; 14 | 15 | while (hs.contains(val)) { 16 | val++; 17 | cnt++; 18 | } 19 | 20 | hs.add(val); 21 | } 22 | return cnt; 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | Scanner s = new Scanner(System.in); 28 | int n, A[]; 29 | 30 | n = s.nextInt(); 31 | A = new int[n]; 32 | for (int i = 0; i < n; i++) { 33 | A[i] = s.nextInt(); 34 | } 35 | System.out.print(new UniqueElements().solve(A)); 36 | s.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Stacks & Queues/ReverseBelements.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day48_Mar25; 2 | 3 | import java.util.Arrays; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.Scanner; 7 | 8 | public class ReverseBelements { 9 | 10 | public int[] solve(int[] A, int B) { 11 | Queue q = new LinkedList<>(); 12 | for (int i = 0; i < B; i++) 13 | q.add(A[i]); 14 | for (int i = B - 1; i >= 0; i--) 15 | A[i] = q.remove(); 16 | 17 | return A; 18 | } 19 | 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | Scanner s = new Scanner(System.in); 23 | int n = s.nextInt(); 24 | int A[] = new int[n]; 25 | for (int i = 0; i < n; i++) 26 | A[i] = s.nextInt(); 27 | int B = s.nextInt(); 28 | System.out.println(Arrays.toString(new ReverseBelements().solve(A, B))); 29 | s.close(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Bit Manipulation/SingleNumber2.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day15_Feb12; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SingleNumber2 { 6 | 7 | public int singleNumber2(final int[] A) { 8 | int cnt, ans = 0; 9 | 10 | for (int i = 0; i <= 31; i++) { 11 | cnt = 0; 12 | for (int j = 0; j < A.length; j++) { 13 | if (((A[j] >> i) & 1) == 1) 14 | cnt++; 15 | } 16 | if (cnt % 3 == 1) { 17 | ans += 1 << i; 18 | } 19 | } 20 | 21 | return ans; 22 | } 23 | 24 | public static void main(String[] args) { 25 | // TODO Auto-generated method stub 26 | Scanner s = new Scanner(System.in); 27 | int n = s.nextInt(); 28 | int A[] = new int[n]; 29 | for (int i = 0; i < n; i++) 30 | A[i] = s.nextInt(); 31 | 32 | SingleNumber2 sn = new SingleNumber2(); 33 | System.out.println(sn.singleNumber2(A)); 34 | s.close(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/OverlappingRectangles.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class OverlappingRectangles { 6 | 7 | public int solve(int A, int B, int C, int D, int E, int F, int G, int H) { 8 | 9 | if (A >= G || E >= C) 10 | return 0; 11 | if (F >= D || B >= H) 12 | return 0; 13 | return 1; 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | Scanner s = new Scanner(System.in); 19 | int a = s.nextInt(); 20 | int b = s.nextInt(); 21 | int c = s.nextInt(); 22 | int d = s.nextInt(); 23 | int e = s.nextInt(); 24 | int f = s.nextInt(); 25 | int g = s.nextInt(); 26 | int h = s.nextInt(); 27 | 28 | OverlappingRectangles or = new OverlappingRectangles(); 29 | System.out.println(or.solve(a, b, c, d, e, f, g, h)); 30 | s.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Greedy Algorithm/BinaryStrings.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BinaryStrings { 6 | 7 | public int solve(String A, int B) { 8 | int cnt = 0; 9 | int n = A.length(); 10 | int X[] = new int[n]; 11 | 12 | for (int i = 0; i < n; i++) 13 | X[i] = A.charAt(i) - '0'; 14 | 15 | for (int i = 0; i <= n - B; i++) { 16 | if (X[i] == 0) { 17 | cnt++; 18 | for (int j = i; j < i + B; j++) 19 | X[j] = X[j] == 0 ? 1 : 0; 20 | } 21 | } 22 | for (int i = n - B + 1; i < n; i++) 23 | if (X[i] == 0) 24 | return -1; 25 | 26 | return cnt; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | System.out.print(new BinaryStrings().solve(s.next(), s.nextInt())); 33 | s.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Greedy Algorithm/MiceToHoles.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class MiceToHoles { 7 | 8 | public int mice(int[] A, int[] B) { 9 | Arrays.parallelSort(A); 10 | Arrays.parallelSort(B); 11 | int max = Integer.MIN_VALUE; 12 | 13 | for (int i = 0; i < A.length; i++) 14 | max = Math.max(max, Math.abs(A[i] - B[i])); 15 | 16 | return max; 17 | } 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | Scanner s = new Scanner(System.in); 22 | int n = s.nextInt(); 23 | int A[] = new int[n]; 24 | for (int i = 0; i < n; i++) 25 | A[i] = s.nextInt(); 26 | int B[] = new int[n]; 27 | for (int i = 0; i < n; i++) 28 | B[i] = s.nextInt(); 29 | 30 | System.out.println(new MiceToHoles().mice(A, B)); 31 | s.close(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Stacks & Queues/ReverseStack.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day46_Mar23; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class ReverseStack { 7 | 8 | public void reverse(int[] A, int index) { 9 | if (index == A.length - 1) { 10 | A[0] = A[index]; 11 | } else { 12 | int tmp = A[index]; 13 | reverse(A, index + 1); 14 | A[A.length - index - 1] = tmp; 15 | } 16 | } 17 | 18 | public int[] solve(int[] A) { 19 | reverse(A, 0); 20 | return A; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | Scanner s = new Scanner(System.in); 26 | int n = s.nextInt(); 27 | int A[] = new int[n]; 28 | for (int i = 0; i < n; i++) 29 | A[i] = s.nextInt(); 30 | ReverseStack rs = new ReverseStack(); 31 | System.out.print(Arrays.toString(rs.solve(A))); 32 | s.close(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ReverseInteger.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day13_Feb10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReverseInteger { 6 | 7 | public int reverse(int A) { 8 | 9 | int a = Math.abs(A); 10 | long sum = 0; 11 | int val; 12 | 13 | while (a > 0) { 14 | val = (int) (a % 10); 15 | sum = sum * 10 + val; 16 | a /= 10; 17 | } 18 | if (A >= 0 && sum <= Integer.MAX_VALUE) 19 | return (int) sum; 20 | else if (A < 0 && Math.negateExact(sum) >= Integer.MIN_VALUE) 21 | return (int) Math.negateExact(sum); 22 | else 23 | return 0; 24 | 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | int a = s.nextInt(); 31 | ReverseInteger ri = new ReverseInteger(); 32 | System.out.println(ri.reverse(a)); 33 | s.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Greedy Algorithm/Seats.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class Seats { 7 | 8 | public int seats(String A) { 9 | ArrayList seats = new ArrayList<>(); 10 | int n = A.length(); 11 | int mod = 10000003; 12 | long sum = 0; 13 | 14 | for (int i = 0; i < n; i++) { 15 | if (A.charAt(i) == 'x') 16 | seats.add(i); 17 | } 18 | n = seats.size(); 19 | int median = n / 2; 20 | 21 | for (int i = 0; i < n; i++) 22 | sum = (sum + Math.abs(seats.get(median) - Math.abs(seats.get(i))) - Math.abs(median - i)) % mod; 23 | 24 | return (int) sum; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | System.out.println(new Seats().seats(s.next())); 31 | s.close(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Hashing/ColorfulNumber.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day36_Mar11; 2 | 3 | import java.util.HashSet; 4 | import java.util.Scanner; 5 | 6 | public class ColorfulNumber { 7 | 8 | public int colorful(int A) { 9 | HashSet hs = new HashSet<>(); 10 | 11 | String S = "" + A; 12 | int l = S.length(); 13 | int prod; 14 | 15 | for (int i = 0; i < l; i++) { 16 | prod = 1; 17 | for (int j = i; j < l; j++) { 18 | prod *= S.charAt(j) - '0'; 19 | if (hs.contains(prod)) 20 | return 0; 21 | else 22 | hs.add(prod); 23 | } 24 | } 25 | return 1; 26 | } 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | Scanner s = new Scanner(System.in); 31 | int n = s.nextInt(); 32 | 33 | ColorfulNumber cn = new ColorfulNumber(); 34 | System.out.println(cn.colorful(n)); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Two Pointers/CountPairsGivenSum.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day33_Mar6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CountPairsGivenSum { 6 | 7 | public int solve(int[] A, int B) { 8 | int i = 0, j = A.length - 1, sum, cnt = 0; 9 | 10 | while (i < j) { 11 | sum = A[i] + A[j]; 12 | if (sum == B) { 13 | cnt++; 14 | i++; 15 | } else if (sum > B) 16 | j--; 17 | else 18 | i++; 19 | } 20 | return cnt; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | Scanner s = new Scanner(System.in); 26 | int n, A[]; 27 | 28 | n = s.nextInt(); 29 | A = new int[n]; 30 | for (int i = 0; i < n; i++) { 31 | A[i] = s.nextInt(); 32 | } 33 | int b = s.nextInt(); 34 | CountPairsGivenSum cp = new CountPairsGivenSum(); 35 | System.out.print(cp.solve(A, b)); 36 | s.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Bit Manipulation/DivideIntegers.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day15_Feb12; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DivideIntegers { 6 | 7 | public int divide(int A, int B) { 8 | 9 | long sign = ((A < 0) ^ (B < 0)) ? -1 : 1; 10 | long dividend = Math.abs((long) A); 11 | long divisor = Math.abs((long) B); 12 | long q = 0; 13 | 14 | while (dividend >= divisor) { 15 | dividend -= divisor; 16 | q++; 17 | } 18 | 19 | if (q * sign < Integer.MIN_VALUE || q * sign > Integer.MAX_VALUE) 20 | return Integer.MAX_VALUE; 21 | 22 | return (int) (q * sign); 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | Scanner s = new Scanner(System.in); 28 | int a = s.nextInt(); 29 | int b = s.nextInt(); 30 | DivideIntegers di = new DivideIntegers(); 31 | System.out.println(di.divide(a, b)); 32 | s.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Dynamic Programming/LongestValidParenthesis.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day83_May4; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class LongestValidParenthesis { 7 | 8 | public int longestValidParentheses(String A) { 9 | Stack S = new Stack<>(); 10 | int n = A.length(); 11 | int max = 0; 12 | 13 | S.push(-1); 14 | 15 | for (int i = 0; i < n; i++) { 16 | if (A.charAt(i) == '(') { 17 | S.add(i); 18 | } else { 19 | S.pop(); 20 | if (S.isEmpty()) 21 | S.add(i); 22 | else 23 | max = Math.max(max, i - S.peek()); 24 | } 25 | } 26 | return max; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | System.out.println(new LongestValidParenthesis().longestValidParentheses(s.next())); 33 | s.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Dynamic Programming/LongPallinSubsequence.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day78_Apr27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LongPallinSubsequence { 6 | 7 | int dp[][]; 8 | 9 | int find(String A, int i, int j) { 10 | if (i > j) 11 | return 0; 12 | 13 | if (i == j) 14 | return 1; 15 | 16 | if (dp[i][j] != 0) 17 | return dp[i][j]; 18 | 19 | if (A.charAt(i) == A.charAt(j)) 20 | dp[i][j] = 2 + find(A, i + 1, j - 1); 21 | else 22 | dp[i][j] = Math.max(find(A, i + 1, j), find(A, i, j - 1)); 23 | 24 | return dp[i][j]; 25 | } 26 | 27 | public int solve(String A) { 28 | int n = A.length(); 29 | dp = new int[n][n]; 30 | 31 | return find(A, 0, n - 1); 32 | } 33 | 34 | public static void main(String[] args) { 35 | Scanner s = new Scanner(System.in); 36 | System.out.println(new LongPallinSubsequence().solve(s.next())); 37 | s.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Tree Data Structure/BalancedBinaryTree.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day56_Apr3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BalancedBinaryTree { 6 | 7 | boolean flg = true; 8 | 9 | public int height(TreeNode A) { 10 | int lh = 0, rh = 0; 11 | 12 | if (A == null) 13 | return -1; 14 | else { 15 | lh = height(A.left); 16 | rh = height(A.right); 17 | 18 | if (Math.abs(lh - rh) > 1) 19 | flg = false; 20 | } 21 | 22 | return Math.max(lh, rh) + 1; 23 | } 24 | 25 | public int isBalanced(TreeNode A) { 26 | height(A); 27 | 28 | if (flg == true) 29 | return 1; 30 | else 31 | return 0; 32 | } 33 | 34 | public static void main(String[] args) { 35 | // TODO Auto-generated method stub 36 | Scanner s = new Scanner(System.in); 37 | System.out.println(new BalancedBinaryTree().isBalanced(new GetInput().getInput(s))); 38 | s.close(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/LuckyNumbers.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day11_Feb7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LuckyNumbers { // Time Complexity: O(nlogn) 6 | 7 | public int solve(int n) { 8 | 9 | int A[] = new int[n + 1]; 10 | int cnt = 0; 11 | 12 | for (int i = 0; i <= n; i++) 13 | A[i] = 0; 14 | 15 | for (int i = 2; i <= n; i++) { 16 | if (A[i] != 0) 17 | continue; 18 | for (int j = i; j <= n; j += i) { 19 | A[j]++; 20 | } 21 | } 22 | 23 | for (int i = 1; i <= n; i++) { 24 | if (A[i] == 2) { 25 | cnt++; 26 | } 27 | } 28 | 29 | return cnt; 30 | } 31 | 32 | public static void main(String[] args) { 33 | // TODO Auto-generated method stub 34 | 35 | Scanner s = new Scanner(System.in); 36 | int n = s.nextInt(); 37 | LuckyNumbers ln = new LuckyNumbers(); 38 | System.out.println(ln.solve(n)); 39 | s.close(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Strings/CountAndSay.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day42_Mar18; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CountAndSay { 6 | 7 | public String countAndSay(int A) { 8 | String S = "1"; 9 | String tmp; 10 | int cnt; 11 | 12 | if (A == 1) 13 | return S; 14 | 15 | while (A-- > 1) { 16 | tmp = ""; 17 | for (int i = 0; i < S.length(); i++) { 18 | char c = S.charAt(i); 19 | cnt = 0; 20 | while (i < S.length() && S.charAt(i) == c) { 21 | cnt++; 22 | i++; 23 | } 24 | i--; 25 | tmp += "" + cnt + c; 26 | } 27 | S = tmp; 28 | } 29 | 30 | return S; 31 | } 32 | 33 | public static void main(String[] args) { 34 | // TODO Auto-generated method stub 35 | Scanner s = new Scanner(System.in); 36 | int A = s.nextInt(); 37 | CountAndSay cas = new CountAndSay(); 38 | System.out.print(cas.countAndSay(A)); 39 | s.close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ExcelColumnTitle.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day13_Feb10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ExcelColumnTitle { 6 | 7 | public String convertToTitle(int A) { 8 | char a[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 9 | 'U', 'V', 'W', 'X', 'Y', 'Z' }; 10 | String ans = ""; 11 | int i = 0; 12 | 13 | while (A > 26) { 14 | i = (A + 25) % 26; 15 | ans = a[i] + ans; 16 | A = A / 26; 17 | if (i == 25) 18 | A--; 19 | } 20 | ans = a[(A + 25) % 26] + ans; 21 | 22 | return ans; 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | Scanner s = new Scanner(System.in); 28 | int a = s.nextInt(); 29 | ExcelColumnTitle ect = new ExcelColumnTitle(); 30 | System.out.println(ect.convertToTitle(a)); 31 | s.close(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ReadingNewspaper.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ReadingNewspaper { 4 | 5 | public int solve(int A, int[] B) { 6 | int sum = B[0]; 7 | int i; 8 | 9 | for (i = 1; i < B.length; i++) { 10 | sum += B[i]; 11 | } 12 | 13 | if (A > sum) { 14 | A = A % sum; 15 | } 16 | 17 | for (i = 0; i < B.length; i++) { 18 | if (B[i] > 0) { 19 | A -= B[i]; 20 | if (A <= 0) { 21 | break; 22 | } 23 | } 24 | } 25 | return i + 1; 26 | } 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | 31 | Scanner s = new Scanner(System.in); 32 | int n, A[], B; 33 | B = s.nextInt(); 34 | 35 | n = s.nextInt(); 36 | A = new int[n]; 37 | for (int i = 0; i < n; i++) { 38 | A[i] = s.nextInt(); 39 | } 40 | 41 | ReadingNewspaper c = new ReadingNewspaper(); 42 | System.out.println(c.solve(B, A)); 43 | s.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Linked List/MiddleElement.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day50_Mar27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MiddleElement { 6 | 7 | public int solve(ListNode A) { 8 | int n = 0, x; 9 | ListNode tmp = A; 10 | 11 | while (tmp != null) { 12 | n++; 13 | tmp = tmp.next; 14 | } 15 | tmp = A; 16 | x = n / 2; 17 | while (x-- > 0) 18 | tmp = tmp.next; 19 | 20 | return (x % 2 == 0) ? tmp.next.val : tmp.val; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | Scanner s = new Scanner(System.in); 26 | int n = s.nextInt(); 27 | ListNode A = new ListNode(s.nextInt()); 28 | ListNode tail = A; 29 | while (n-- > 1) { 30 | ListNode tmp = new ListNode(s.nextInt()); 31 | tail.next = tmp; 32 | tail = tmp; 33 | } 34 | 35 | System.out.print(new MiddleElement().solve(A)); 36 | 37 | s.close(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Dynamic Programming/PickFromBothSides.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day145_Jul17; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PickFromBothSides { 6 | 7 | public int solve(int[] A, int B) { 8 | int x, y, sum = 0, maxSum = Integer.MIN_VALUE, n; 9 | 10 | x = 0; 11 | n = B; 12 | while (n-- > 0) 13 | sum += A[x++]; 14 | 15 | maxSum = Math.max(maxSum, sum); 16 | x--; 17 | y = A.length - 1; 18 | 19 | while (x >= 0) { 20 | sum += A[y--] - A[x--]; 21 | maxSum = Math.max(maxSum, sum); 22 | } 23 | 24 | return maxSum; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | int n = s.nextInt(); 31 | int[] A = new int[n]; 32 | for (int i = 0; i < n; i++) 33 | A[i] = s.nextInt(); 34 | System.out.println(new PickFromBothSides().solve(A, s.nextInt())); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Two Pointers/ContainerWithMostWater.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day33_Mar6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ContainerWithMostWater { 6 | 7 | public int maxArea(int[] A) { 8 | int maxArea = 0, i = 0, j = A.length - 1, min, area; 9 | 10 | while (i < j) { 11 | min = Math.min(A[i], A[j]); 12 | area = (j - i) * min; 13 | maxArea = Math.max(maxArea, area); 14 | if (A[i] == min) 15 | i++; 16 | else 17 | j--; 18 | } 19 | 20 | return maxArea; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | Scanner s = new Scanner(System.in); 26 | int n, A[]; 27 | 28 | n = s.nextInt(); 29 | A = new int[n]; 30 | for (int i = 0; i < n; i++) { 31 | A[i] = s.nextInt(); 32 | } 33 | ContainerWithMostWater co = new ContainerWithMostWater(); 34 | System.out.print(co.maxArea(A)); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Backtracking/Sixlets.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day27_Feb28; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Sixlets { 6 | 7 | int count = 0; 8 | 9 | void subsequences(int[] A, int start, int sum, int cnt, int B) { 10 | if (cnt == B && sum <= 1000) { 11 | count++; 12 | return; 13 | } 14 | 15 | for (int i = start; i < A.length && (cnt < B) && (A.length - i + cnt >= B); i++) 16 | subsequences(A, i + 1, sum + A[i], cnt + 1, B); 17 | } 18 | 19 | public int solve(int[] A, int B) { 20 | subsequences(A, 0, 0, 0, B); 21 | return count; 22 | } 23 | 24 | public static void main(String[] args) { 25 | // TODO Auto-generated method stub 26 | Scanner s = new Scanner(System.in); 27 | int n = s.nextInt(); 28 | int A[] = new int[n]; 29 | for (int i = 0; i < n; i++) 30 | A[i] = s.nextInt(); 31 | 32 | System.out.println(new Sixlets().solve(A, s.nextInt())); 33 | s.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/SumOfAllSubMatrices.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day07_Feb3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SumOfAllSubMatrices { 6 | 7 | public int solve(int[][] A) { 8 | int n, m, ans = 0; 9 | 10 | n = A.length; 11 | m = A[0].length; 12 | 13 | for (int i = 0; i < n; i++) { 14 | for (int j = 0; j < m; j++) { 15 | ans += A[i][j] * (i + 1) * (j + 1) * (n - i) * (m - j); 16 | } 17 | } 18 | return ans; 19 | } 20 | 21 | public static void main(String[] args) { 22 | // TODO Auto-generated method stub 23 | Scanner s = new Scanner(System.in); 24 | int n, m, A[][]; 25 | 26 | n = s.nextInt(); 27 | m = s.nextInt(); 28 | A = new int[n][m]; 29 | for (int i = 0; i < n; i++) 30 | for (int j = 0; j < m; j++) 31 | A[i][j] = s.nextInt(); 32 | 33 | SumOfAllSubMatrices c = new SumOfAllSubMatrices(); 34 | System.out.println(c.solve(A)); 35 | s.close(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Binary Search/SingleElementSortedArray.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day17_Feb14; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SingleElementSortedArray { 6 | 7 | public int solve(int[] A) { 8 | if (A.length == 1) 9 | return A[0]; 10 | 11 | if (A.length == 2) { 12 | if (A[0] != A[1]) 13 | return 1; 14 | if (A[0] == A[1]) 15 | return 0; 16 | } 17 | int i; 18 | for (i = 0; i < A.length - 1; i += 2) { 19 | if (A[i] != A[i + 1]) 20 | return A[i]; 21 | } 22 | 23 | return A[i]; 24 | } 25 | 26 | public static void main(String[] args) { 27 | // TODO Auto-generated method stub 28 | Scanner s = new Scanner(System.in); 29 | int n = s.nextInt(); 30 | int A[] = new int[n]; 31 | 32 | for (int i = 0; i < n; i++) 33 | A[i] = s.nextInt(); 34 | 35 | SingleElementSortedArray sa = new SingleElementSortedArray(); 36 | System.out.println(sa.solve(A)); 37 | s.close(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Tree Data Structure/KthSmallestElement.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class KthSmallestElement { 7 | 8 | public int kthsmallest(TreeNode A, int B) { 9 | 10 | TreeNode tmp, curr; 11 | Stack S = new Stack<>(); 12 | curr = A; 13 | int cnt = 0; 14 | 15 | while (curr != null || !S.isEmpty()) { 16 | if (curr != null) { 17 | S.push(curr); 18 | curr = curr.left; 19 | } else { 20 | tmp = S.pop(); 21 | curr = tmp.right; 22 | cnt++; 23 | if (cnt == B) 24 | return tmp.val; 25 | } 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | 35 | System.out.println(new KthSmallestElement().kthsmallest(new GetInput().getInput(s), s.nextInt())); 36 | s.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/SortThePermutation.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day35_Mar8; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SortThePermutation { 6 | 7 | public int solve(int[] A) { 8 | int cnt = 0, x, tmp; 9 | for (int i = 0; i < A.length; i++) { 10 | if (A[i] == i + 1) 11 | cnt++; 12 | else if (A[i] != 0) { 13 | x = A[i] - 1; 14 | A[i] = 0; 15 | cnt++; 16 | while (x != i) { 17 | tmp = x; 18 | x = A[x] - 1; 19 | A[tmp] = 0; 20 | } 21 | } 22 | } 23 | 24 | return cnt; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | int n = s.nextInt(); 31 | int A[] = new int[n]; 32 | for (int i = 0; i < n; i++) 33 | A[i] = s.nextInt(); 34 | 35 | SortThePermutation stp = new SortThePermutation(); 36 | System.out.println(stp.solve(A)); 37 | s.close(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day07_Feb3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class FizzBuzz { 6 | 7 | public String[] fizzBuzz(int A) { 8 | 9 | String[] s = new String[A]; 10 | 11 | for (int i = 1; i <= A; i++) { 12 | if (i % 15 == 0) { 13 | s[i - 1] = "FizzBuzz"; 14 | } else if (i % 3 == 0) { 15 | s[i - 1] = "Fizz"; 16 | } else if (i % 5 == 0) { 17 | s[i - 1] = "Buzz"; 18 | } else { 19 | s[i - 1] = Integer.toString(i); 20 | } 21 | } 22 | return s; 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | Scanner s = new Scanner(System.in); 28 | int n; 29 | String A[]; 30 | 31 | n = s.nextInt(); 32 | A = new String[n]; 33 | 34 | FizzBuzz c = new FizzBuzz(); 35 | A = c.fizzBuzz(n); 36 | for (int i = 0; i < n; i++) { 37 | System.out.print(A[i] + " "); 38 | } 39 | s.close(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Linked List/RemoveDuplicates.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day50_Mar27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class RemoveDuplicates { 6 | 7 | public ListNode deleteDuplicates(ListNode A) { 8 | ListNode tmp = A; 9 | 10 | while (tmp.next != null) { 11 | if (tmp.next.val == tmp.val) 12 | tmp.next = tmp.next.next; 13 | else 14 | tmp = tmp.next; 15 | } 16 | 17 | return A; 18 | } 19 | 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | Scanner s = new Scanner(System.in); 23 | int n = s.nextInt(); 24 | ListNode A = new ListNode(s.nextInt()); 25 | ListNode tail = A; 26 | while (n-- > 1) { 27 | ListNode tmp = new ListNode(s.nextInt()); 28 | tail.next = tmp; 29 | tail = tmp; 30 | } 31 | A = new RemoveDuplicates().deleteDuplicates(A); 32 | while (A != null) { 33 | System.out.print(A.val + " "); 34 | A = A.next; 35 | } 36 | s.close(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Two Pointers/Count_Rectangles.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day33_Mar6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Count_Rectangles { 6 | 7 | public int solve(int[] A, int B) { 8 | 9 | long cnt = 0; 10 | int i, j = A.length - 1; 11 | 12 | for (i = 0; i < A.length; i++) { 13 | if (j < 0) 14 | break; 15 | while ((long) A[i] * (long) A[j] >= B) { 16 | j--; 17 | if (j < 0) 18 | break; 19 | } 20 | cnt += j + 1; 21 | } 22 | 23 | return (int) (cnt % 1000000007); 24 | } 25 | 26 | public static void main(String[] args) { 27 | // TODO Auto-generated method stub 28 | Scanner s = new Scanner(System.in); 29 | int n, A[]; 30 | 31 | n = s.nextInt(); 32 | A = new int[n]; 33 | for (int i = 0; i < n; i++) { 34 | A[i] = s.nextInt(); 35 | } 36 | int b = s.nextInt(); 37 | Count_Rectangles cr = new Count_Rectangles(); 38 | System.out.print(cr.solve(A, b)); 39 | s.close(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Tree Data Structure/BalancedBST.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BalancedBST { 6 | 7 | TreeNode createTree(int[] A, int s, int e) { 8 | 9 | if (s > e) 10 | return null; 11 | 12 | if (s == e) 13 | return new TreeNode(A[s]); 14 | 15 | int m = (s + e) / 2; 16 | TreeNode X = new TreeNode(A[m]); 17 | X.left = createTree(A, s, m - 1); 18 | X.right = createTree(A, m + 1, e); 19 | 20 | return X; 21 | } 22 | 23 | public TreeNode sortedArrayToBST(final int[] A) { 24 | return createTree(A, 0, A.length - 1); 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | int n = s.nextInt(); 31 | int A[] = new int[n]; 32 | for (int i = 0; i < n; i++) 33 | A[i] = s.nextInt(); 34 | System.out.println(new BalancedBST().sortedArrayToBST(A)); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Binary Search/PeakElement.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day17_Feb14; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PeakElement { 6 | 7 | public int solve(int[] A) { 8 | 9 | int l = A.length; 10 | 11 | if (l == 1) 12 | return A[0]; 13 | if (l == 2) 14 | return Math.max(A[0], A[1]); 15 | 16 | int peak = A[0] > A[1] ? A[0] : A[l - 1] > A[l - 2] ? A[l - 1] : 0; 17 | 18 | for (int i = 1; i < l - 1; i++) { 19 | if (A[i] >= A[i - 1] && A[i] >= A[i + 1]) { 20 | peak = A[i]; 21 | break; 22 | } 23 | } 24 | 25 | return peak; 26 | } 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | Scanner s = new Scanner(System.in); 31 | int n = s.nextInt(); 32 | int A[] = new int[n]; 33 | 34 | for (int i = 0; i < n; i++) 35 | A[i] = s.nextInt(); 36 | 37 | PeakElement pe = new PeakElement(); 38 | System.out.println(pe.solve(A)); 39 | s.close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Binary Search/SmallestGoodBase.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day21_Feb19; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SmallestGoodBase { 6 | 7 | public String solve(String A) { 8 | 9 | long a = Long.parseLong(A); 10 | int b = (int) (Math.log(a) / Math.log(2)); 11 | long s, e, k, g; 12 | 13 | for (int i = b + 1; i > 2; i--) { 14 | s = 2; 15 | e = a - 1; 16 | while (s <= e) { 17 | k = (s + e) / 2; 18 | g = (long) (Math.pow(k, i) - 1) / (k - 1); 19 | if (g == a) 20 | return "" + k; 21 | if (g > a) 22 | e = k - 1; 23 | else 24 | s = k + 1; 25 | } 26 | } 27 | 28 | return "" + (a - 1); 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | String A = s.nextLine(); 35 | 36 | SmallestGoodBase sgb = new SmallestGoodBase(); 37 | System.out.println(sgb.solve(A)); 38 | s.close(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Hashing/ReplicatingSubstring.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day38_Mar13; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Scanner; 6 | 7 | public class ReplicatingSubstring { 8 | 9 | public int solve(int A, String B) { 10 | HashMap hm = new HashMap<>(); 11 | char c; 12 | 13 | for (int i = 0; i < B.length(); i++) { 14 | c = B.charAt(i); 15 | if (hm.containsKey(c)) 16 | hm.replace(c, hm.get(c) + 1); 17 | else 18 | hm.put(c, 1); 19 | } 20 | for (Map.Entry entry : hm.entrySet()) { 21 | if (entry.getValue() % A != 0) 22 | return -1; 23 | } 24 | 25 | return 1; 26 | } 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | Scanner s = new Scanner(System.in); 31 | int A = s.nextInt(); 32 | String B = s.next(); 33 | 34 | System.out.print(new ReplicatingSubstring().solve(A, B)); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Tree Data Structure/CheckBSTOneChild.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CheckBSTOneChild { 6 | 7 | public String solve(int[] A) { 8 | int i, min = Integer.MIN_VALUE, max = Integer.MAX_VALUE; 9 | 10 | if (A[1] > A[0]) 11 | min = A[0]; 12 | else 13 | max = A[0]; 14 | 15 | i = 2; 16 | 17 | while (i < A.length) { 18 | if (A[i] > min && A[i] <= max) { 19 | if (A[i] > A[i - 1]) 20 | min = A[i - 1]; 21 | else 22 | max = A[i - 1]; 23 | } else 24 | return "NO"; 25 | i++; 26 | } 27 | 28 | return "YES"; 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | int n = s.nextInt(); 35 | int A[] = new int[n]; 36 | for (int i = 0; i < n; i++) 37 | A[i] = s.nextInt(); 38 | System.out.println(new CheckBSTOneChild().solve(A)); 39 | s.close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MinimumSwaps2.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day31_Mar4; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MinimumSwaps2 { 6 | 7 | public int solve(int[] A) { 8 | int l = A.length; 9 | int tmp, cnt = 0; 10 | 11 | for (int i = 0; i < l; i++) { 12 | if (A[i] != i) { 13 | // Find i 14 | for (int j = i + 1; j < l; j++) { 15 | if (A[j] == i) { 16 | tmp = A[i]; 17 | A[i] = A[j]; 18 | A[j] = tmp; 19 | cnt++; 20 | break; 21 | } 22 | } 23 | } 24 | 25 | } 26 | return cnt; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | int n, A[]; 33 | 34 | n = s.nextInt(); 35 | A = new int[n]; 36 | for (int i = 0; i < n; i++) { 37 | A[i] = s.nextInt(); 38 | } 39 | 40 | MinimumSwaps2 ms = new MinimumSwaps2(); 41 | System.out.println(ms.solve(A)); 42 | s.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/RainWaterTrapped.java: -------------------------------------------------------------------------------- 1 | public class RainWaterTrapped { 2 | 3 | public static void main(String[] args) { 4 | // TODO Auto-generated method stub 5 | 6 | Scanner s = new Scanner(System.in); 7 | int n, A[]; 8 | n = s.nextInt(); 9 | A = new int[n]; 10 | 11 | for (int i = 0; i < n; i++) { 12 | A[i] = s.nextInt(); 13 | } 14 | 15 | int a[] = new int[A.length]; 16 | int b[] = new int[A.length]; 17 | 18 | int max = 0; 19 | 20 | for (int i = 1; i < A.length; i++) { 21 | if (max < A[i - 1]) { 22 | max = A[i - 1]; 23 | } 24 | a[i] = max; 25 | } 26 | max = 0; 27 | for (int i = A.length - 2; i >= 0; i--) { 28 | if (max < A[i + 1]) { 29 | max = A[i + 1]; 30 | } 31 | b[i] = max; 32 | } 33 | 34 | int sum = 0, min; 35 | 36 | for (int i = 0; i < A.length; i++) { 37 | min = a[i] > b[i] ? b[i] : a[i]; 38 | sum += (min - A[i]) > 0 ? (min - A[i]) : 0; 39 | } 40 | System.out.println(sum); 41 | 42 | s.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Bit Manipulation/DiffBitsSumPairwise.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day15_Feb12; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DiffBitsSumPairwise { 6 | 7 | public int cntBits(int[] A) { 8 | 9 | int a[] = new int[32]; 10 | int cnt, l = A.length; 11 | long sum = 0; 12 | 13 | for (int i = 0; i < 32; i++) { 14 | cnt = 0; 15 | for (int j = 0; j < l; j++) { 16 | if (((A[j] >> i) & 1) == 1) { 17 | cnt++; 18 | } 19 | } 20 | a[i] = cnt; 21 | sum += (long) (cnt) * (long) (l - cnt); 22 | } 23 | 24 | return (int) ((sum * 2) % 1000000007); 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | int n = s.nextInt(); 31 | int A[] = new int[n]; 32 | for (int i = 0; i < n; i++) 33 | A[i] = s.nextInt(); 34 | DiffBitsSumPairwise df = new DiffBitsSumPairwise(); 35 | System.out.println(df.cntBits(A)); 36 | s.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/NthMagicNumber.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day11_Feb7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class NthMagicNumber { 6 | 7 | public int solve(int A) { 8 | 9 | if (A == 1) 10 | return 5; 11 | if (A == 2) 12 | return 25; 13 | 14 | int a[] = new int[A]; 15 | a[0] = 5; 16 | a[1] = 25; 17 | 18 | int i = 1; 19 | int x = 2; 20 | int p = 25; 21 | 22 | while (x < A) { 23 | for (int j = 0; j < i; j++) { 24 | a[x++] = a[i] + a[j]; 25 | if (x == A) 26 | break; 27 | } 28 | 29 | if (x == A) 30 | break; 31 | 32 | p = p * 5; 33 | a[x] = p; 34 | i = x++; 35 | } 36 | 37 | return a[A - 1]; 38 | } 39 | 40 | public static void main(String[] args) { 41 | // TODO Auto-generated method stub 42 | 43 | Scanner s = new Scanner(System.in); 44 | int n = s.nextInt(); 45 | NthMagicNumber mn = new NthMagicNumber(); 46 | System.out.println(mn.solve(n)); 47 | s.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/WaveArray.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day29_Mar2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class WaveArray { 7 | 8 | public int[] wave(int[] A) { 9 | 10 | Arrays.sort(A); 11 | 12 | if (A.length == 1) 13 | return A; 14 | 15 | int l = (A.length % 2 == 0) ? A.length - 1 : A.length - 2; 16 | int tmp; 17 | 18 | for (int i = 0; i < l; i += 2) { 19 | tmp = A[i]; 20 | A[i] = A[i + 1]; 21 | A[i + 1] = tmp; 22 | } 23 | return A; 24 | } 25 | 26 | public static void main(String[] args) { 27 | // TODO Auto-generated method stub 28 | Scanner s = new Scanner(System.in); 29 | int n, A[]; 30 | 31 | n = s.nextInt(); 32 | A = new int[n]; 33 | for (int i = 0; i < n; i++) { 34 | A[i] = s.nextInt(); 35 | } 36 | 37 | WaveArray c = new WaveArray(); 38 | A = c.wave(A); 39 | for (int i = 0; i < n; i++) { 40 | System.out.print(A[i] + " "); 41 | } 42 | s.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Stacks & Queues/FirstNonRepeatingChar.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day48_Mar25; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | import java.util.Scanner; 6 | 7 | public class FirstNonRepeatingChar { 8 | 9 | public String solve(String A) { 10 | Queue Q = new LinkedList<>(); 11 | int X[] = new int[26]; 12 | int l = A.length(); 13 | char ans[] = new char[l]; 14 | char c; 15 | 16 | for (int i = 0; i < l; i++) { 17 | c = A.charAt(i); 18 | X[c - 'a']++; 19 | if (X[c - 'a'] == 1) 20 | Q.add(c); 21 | 22 | while (!Q.isEmpty() && X[Q.peek() - 'a'] > 1) 23 | Q.remove(); 24 | 25 | ans[i] = Q.isEmpty() ? '#' : Q.peek(); 26 | } 27 | return new String(ans); 28 | } 29 | 30 | public static void main(String[] args) { 31 | // TODO Auto-generated method stub 32 | Scanner s = new Scanner(System.in); 33 | String S = s.next(); 34 | System.out.println(new FirstNonRepeatingChar().solve(S)); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Dynamic Programming/LCS.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day78_Apr27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LCS { 6 | 7 | public int solve(String A, String B) { 8 | int m = A.length(); 9 | int n = B.length(); 10 | int x = 1; 11 | 12 | if (m < n) { 13 | String tmp = A; 14 | A = B; 15 | B = tmp; 16 | int l = m; 17 | m = n; 18 | n = l; 19 | } 20 | 21 | int dp[][] = new int[2][n + 1]; 22 | 23 | for (int i = 1; i <= m; i++) { 24 | x = i % 2; 25 | for (int j = 1; j <= n; j++) { 26 | 27 | if (A.charAt(i - 1) == B.charAt(j - 1)) 28 | dp[x][j] = dp[1 - x][j - 1] + 1; 29 | 30 | else 31 | dp[x][j] = Math.max(dp[1 - x][j], dp[x][j - 1]); 32 | } 33 | } 34 | return dp[x][n]; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | System.out.println(new LCS().solve(s.next(), s.next())); 41 | s.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Hashing/ShaggyAndDistances.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day36_Mar11; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class ShaggyAndDistances { 7 | 8 | public int solve(int[] A) { 9 | int min = Integer.MAX_VALUE; 10 | 11 | HashMap hm = new HashMap<>(); 12 | 13 | for (int i = 0; i < A.length; i++) { 14 | if (hm.containsKey(A[i])) { 15 | min = Math.min(min, i - hm.get(A[i])); 16 | hm.replace(A[i], i); 17 | } else { 18 | hm.put(A[i], i); 19 | } 20 | } 21 | 22 | return (min == Integer.MAX_VALUE ? -1 : min); 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | Scanner s = new Scanner(System.in); 28 | int n = s.nextInt(); 29 | int A[] = new int[n]; 30 | 31 | for (int i = 0; i < n; i++) 32 | A[i] = s.nextInt(); 33 | 34 | ShaggyAndDistances sd = new ShaggyAndDistances(); 35 | System.out.print(sd.solve(A)); 36 | s.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Stacks & Queues/SimplifyDirectoryPath.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day44_Mar20; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class SimplifyDirectoryPath { 7 | 8 | public String simplifyPath(String A) { 9 | 10 | Stack S = new Stack(); 11 | String X[] = A.split("/"); 12 | int l = X.length; 13 | 14 | for (int i = 0; i < l; i++) { 15 | if (X[i].equals("..")) { 16 | if (!S.empty()) 17 | S.pop(); 18 | } else if (!X[i].equals("") && !X[i].equals(".")) 19 | S.push(X[i]); 20 | } 21 | A = ""; 22 | while (!S.empty()) { 23 | A = "/" + S.pop() + A; 24 | } 25 | 26 | return (A.length() == 0) ? "/" : A; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | String A = s.next(); 33 | SimplifyDirectoryPath sdp = new SimplifyDirectoryPath(); 34 | System.out.print(sdp.simplifyPath(A)); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ComputeNcR.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day13_Feb10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ComputeNcR { 6 | 7 | public int solve(int A, int B, int C) { 8 | int min = (B < A - B) ? B : A - B; 9 | long nmod = 1, dmod = 1; 10 | 11 | for (int i = A; i > A - min; i--) 12 | nmod = (nmod * i) % C; 13 | 14 | for (int i = min; i > 1; i--) 15 | dmod = (dmod * i) % C; 16 | 17 | long x = 1; 18 | int p = C - 2; 19 | 20 | while (p > 0) { 21 | if (p % 2 == 1) { 22 | x = (x * dmod) % C; 23 | } 24 | dmod = (dmod * dmod) % C; 25 | p /= 2; 26 | } 27 | 28 | return (int) ((nmod * x) % C); 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | int a = s.nextInt(); 35 | int b = s.nextInt(); 36 | int c = s.nextInt(); 37 | ComputeNcR ncr = new ComputeNcR(); 38 | System.out.println(ncr.solve(a, b, c)); 39 | s.close(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MissingArray.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day145_Jul17; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class MissingArray { 7 | 8 | public int[] solve(char[] A) { 9 | int[] ans = new int[A.length]; 10 | 11 | int cnt = 0; 12 | 13 | for (int i = 0; i < A.length; i++) { 14 | if (A[i] == '+') 15 | cnt++; 16 | } 17 | int x = A.length - cnt; 18 | int y = x + 1; 19 | 20 | for (int i = 0; i < A.length; i++) { 21 | char c = A[i]; 22 | if (c == '.' || c == '-') 23 | ans[i] = x--; 24 | else 25 | ans[i] = y++; 26 | } 27 | return ans; 28 | } 29 | 30 | public static void main(String[] args) { 31 | // TODO Auto-generated method stub 32 | Scanner s = new Scanner(System.in); 33 | int n = s.nextInt(); 34 | char[] A = new char[n]; 35 | for (int i = 0; i < n; i++) 36 | A[i] = s.next().charAt(0); 37 | System.out.println(Arrays.toString(new MissingArray().solve(A))); 38 | s.close(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Hashing/LongestSubstringWithoutRepeat.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day36_Mar11; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class LongestSubstringWithoutRepeat { 7 | 8 | public int lengthOfLongestSubstring(String A) { 9 | int max = 1; 10 | HashMap hm = new HashMap<>(); 11 | int s = 0, e = 0, x, n = A.length(); 12 | 13 | while (e < n) { 14 | char c = A.charAt(e); 15 | if (hm.containsKey(c)) { 16 | max = Math.max(max, e - s); 17 | x = hm.get(c); 18 | 19 | while (s <= x) { 20 | hm.remove(A.charAt(s)); 21 | s++; 22 | } 23 | } 24 | 25 | hm.put(c, e); 26 | e++; 27 | } 28 | max = Math.max(max, e - s); 29 | return max; 30 | } 31 | 32 | public static void main(String[] args) { 33 | // TODO Auto-generated method stub 34 | Scanner s = new Scanner(System.in); 35 | System.out.println(new LongestSubstringWithoutRepeat().lengthOfLongestSubstring(s.next())); 36 | s.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Tree Data Structure/GetInput.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day61_Apr8; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | import java.util.Scanner; 6 | 7 | public class GetInput { 8 | 9 | TreeNode getInput(Scanner s) { 10 | Queue q = new LinkedList<>(); 11 | TreeNode root = null, tmp, left, right; 12 | int n, val; 13 | 14 | n = s.nextInt(); 15 | while (n > 0) { 16 | if (q.isEmpty()) { 17 | tmp = new TreeNode(s.nextInt()); 18 | n--; 19 | q.add(tmp); 20 | root = tmp; 21 | } else { 22 | tmp = q.remove(); 23 | val = s.nextInt(); 24 | n--; 25 | left = (val != -1) ? new TreeNode(val) : null; 26 | if (left != null) { 27 | q.add(left); 28 | tmp.left = left; 29 | } 30 | val = s.nextInt(); 31 | n--; 32 | right = (val != -1) ? new TreeNode(val) : null; 33 | if (right != null) { 34 | q.add(right); 35 | tmp.right = right; 36 | } 37 | } 38 | } 39 | return root; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/DivisorGame.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DivisorGame { 6 | 7 | public int gcd(int A, int B) { 8 | if (A == 1 || B == 1) 9 | return 1; 10 | if (A == 0 || B == 0) 11 | return Math.max(A, B); 12 | 13 | if (A > B) 14 | return gcd(A % B, B); 15 | if (A < B) 16 | return gcd(A, B % A); 17 | 18 | if (A == B) 19 | return A; 20 | 21 | return -1; 22 | } 23 | 24 | public int solve(int A, int B, int C) { 25 | 26 | long prod = (B * C) / gcd(B, C); 27 | 28 | if (A >= prod) { 29 | return ((int) (A / prod)); 30 | } 31 | return 0; 32 | } 33 | 34 | public static void main(String[] args) { 35 | // TODO Auto-generated method stub 36 | Scanner s = new Scanner(System.in); 37 | int a = s.nextInt(); 38 | int b = s.nextInt(); 39 | int c = s.nextInt(); 40 | DivisorGame dg = new DivisorGame(); 41 | System.out.println(dg.solve(a, b, c)); 42 | s.close(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MaximumMinimumLogic.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day31_Mar4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class MaximumMinimumLogic { 7 | 8 | public int[] solve(int[] A) { 9 | Arrays.parallelSort(A); 10 | long max = 0, min = 0; 11 | int n = A.length, mod = 1000000007; 12 | 13 | for (int i = 0; i < n; i += 2) 14 | min = Math.floorMod(min + Math.abs(A[i] - A[i + 1]), mod); 15 | 16 | for (int i = 0; i < n / 2; i++) 17 | max = Math.floorMod(max + Math.abs(A[i] - A[n - i - 1]), mod); 18 | 19 | return new int[] { (int) max, (int) min }; 20 | } 21 | 22 | public static void main(String[] args) { 23 | // TODO Auto-generated method stub 24 | Scanner s = new Scanner(System.in); 25 | int n, A[]; 26 | 27 | n = s.nextInt(); 28 | A = new int[n]; 29 | for (int i = 0; i < n; i++) { 30 | A[i] = s.nextInt(); 31 | } 32 | System.out.print(Arrays.toString(new MaximumMinimumLogic().solve(A))); 33 | s.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/MaxChunksSorted2.md: -------------------------------------------------------------------------------- 1 | # Max Chunks To Make Sorted II 2 | ### Problem Description 3 | 4 | Given an array of integers (not necessarily distinct) A, if we split the array into some number of "chunks" (partitions), 5 | and individually sort each chunk. After concatenating them, the result equals the sorted array. 6 | 7 | What is the most number of chunks we could have made? 8 | 9 | 10 | 11 | ### Problem Constraints 12 | 1 <= N <= 100000 13 | -109 <= A[i] <= 109 14 | 15 | 16 | 17 | ### Input Format 18 | The only argument given is the integer array A. 19 | 20 | 21 | 22 | ### Output Format 23 | Return the maximum number of chunks that we could have made. 24 | 25 | 26 | 27 | ### Example Input 28 | A = [2, 0, 1, 2] 29 | 30 | 31 | ### Example Output 32 | 2 33 | 34 | 35 | ### Example Explanation 36 | We can split the array into two subarray one consisting element [2,0,1] and second one with only element [2]. 37 | Sort them individually and concat them. The result will be sorted. 38 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/SumOfAllSubMatrices.md: -------------------------------------------------------------------------------- 1 | # Sum of all Submatrices 2 | ### Problem Description 3 | 4 | Given a 2D Matrix A of dimensions N*N, we need to return sum of all possible submatrices. 5 | 6 | 7 | 8 | ### Problem Constraints 9 | 1 <= N <=30 10 | 11 | 0 <= A[i][j] <= 10 12 | 13 | 14 | 15 | ### Input Format 16 | Single argument representing a 2-D array A of size N x N. 17 | 18 | 19 | 20 | ### Output Format 21 | Return an integer denoting the sum of all possible submatrices in the given matrix. 22 | 23 | 24 | 25 | ### Example Input 26 | A = [ [1, 1] 27 | [1, 1] ] 28 | 29 | 30 | ### Example Output 31 | 16 32 | 33 | 34 | ### Example Explanation 35 | Number of submatrices with 1 elements = 4, so sum of all such submatrices = 4 * 1 = 4 36 | Number of submatrices with 2 elements = 4, so sum of all such submatrices = 4 * 2 = 8 37 | Number of submatrices with 3 elements = 0 38 | Number of submatrices with 4 elements = 1, so sum of such submatrix = 4 39 | Total Sum = 4+8+4 = 16 40 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/SumTheDifference.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day31_Mar4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class SumTheDifference { 7 | 8 | int mod = 1000000007; 9 | 10 | public int solve(int[] A) { 11 | 12 | Arrays.sort(A); 13 | int n = A.length; 14 | long sum = 0; 15 | 16 | int pow[] = new int[n + 1]; 17 | pow[0] = 1; 18 | for (int i = 1; i <= n; i++) 19 | pow[i] = (int) ((long) pow[i - 1] * 2) % mod; 20 | 21 | for (int i = 0; i < n; i++) 22 | sum = (sum + Math.floorMod((long) A[i] * (pow[i] - pow[n - i - 1]), mod)) % mod; 23 | 24 | return (int) sum; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | int n, A[]; 31 | 32 | n = s.nextInt(); 33 | A = new int[n]; 34 | for (int i = 0; i < n; i++) { 35 | A[i] = s.nextInt(); 36 | } 37 | System.out.print(new SumTheDifference().solve(A)); 38 | s.close(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Strings/SameFormatString.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day42_Mar18; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SameFormatString { 6 | 7 | public int solve(final String A, final String B) { 8 | int i, j, l1, l2; 9 | char x; 10 | i = 0; 11 | j = 0; 12 | l1 = A.length(); 13 | l2 = B.length(); 14 | 15 | if (l2 < l1) 16 | return 0; 17 | 18 | while (i < l1) { 19 | x = A.charAt(i); 20 | if (j == l2 || B.charAt(j) != x) { 21 | return 0; 22 | } else { 23 | while (j < l2 && B.charAt(j) == x) 24 | j++; 25 | i++; 26 | } 27 | } 28 | if (i == l1 && j == l2) 29 | return 1; 30 | else 31 | return 0; 32 | } 33 | 34 | public static void main(String[] args) { 35 | // TODO Auto-generated method stub 36 | Scanner s = new Scanner(System.in); 37 | String A, B; 38 | 39 | A = s.nextLine(); 40 | B = s.nextLine(); 41 | SameFormatString sfs = new SameFormatString(); 42 | System.out.println(sfs.solve(A, B)); 43 | s.close(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Two Pointers/CountSubarrays.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day33_Mar6; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class CountSubarrays { 7 | 8 | public int solve(int[] A) { 9 | 10 | int s = 0, e = 1; 11 | long count = 1; 12 | HashMap hm = new HashMap<>(); 13 | hm.put(A[0], 0); 14 | 15 | while (e < A.length) { 16 | if (hm.containsKey(A[e])) { 17 | s = hm.get(A[e]) >= s ? hm.get(A[e]) + 1 : s; 18 | hm.replace(A[e], e); 19 | } else 20 | hm.put(A[e], e); 21 | 22 | count += e - s + 1; 23 | e++; 24 | } 25 | return (int) (count % 1000000007); 26 | } 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | Scanner s = new Scanner(System.in); 31 | int n = s.nextInt(); 32 | int A[] = new int[n]; 33 | for (int i = 0; i < n; i++) 34 | A[i] = s.nextInt(); 35 | CountSubarrays cs = new CountSubarrays(); 36 | System.out.println(cs.solve(A)); 37 | s.close(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/SetMatrixZero.md: -------------------------------------------------------------------------------- 1 | # Set Matrix Zeros 2 | Given a matrix, A of size M x N of 0s and 1s. If an element is 0, set its entire row and column to 0. 3 | 4 | Note: This will be evaluated on the extra memory used. Try to minimize the space and time complexity. 5 | 6 | 7 | 8 | ### Input Format: 9 | 10 | The first and the only argument of input contains a 2-d integer matrix, A, of size M x N. 11 | 12 | 13 | ### Output Format: 14 | 15 | Return a 2-d matrix that satisfies the given conditions. 16 | 17 | 18 | ### Constraints: 19 | 20 | 1 <= N, M <= 1000 21 | 0 <= A[i][j] <= 1 22 | 23 | 24 | ### Examples: 25 | 26 | 27 | ### Input 1: 28 | [ [1, 0, 1], 29 | [1, 1, 1], 30 | [1, 1, 1] ] 31 | 32 | 33 | 34 | ### Output 1: 35 | [ [0, 0, 0], 36 | [1, 0, 1], 37 | [1, 0, 1] ] 38 | 39 | 40 | 41 | ### Input 2: 42 | [ [1, 0, 1], 43 | [1, 1, 1], 44 | [1, 0, 1] ] 45 | 46 | 47 | 48 | ### Output 2: 49 | [ [0, 0, 0], 50 | [1, 0, 1], 51 | [0, 0, 0] ] 52 | -------------------------------------------------------------------------------- /Stacks & Queues/NintegersContaining123.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day48_Mar25; 2 | 3 | import java.util.Arrays; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.Scanner; 7 | 8 | public class NintegersContaining123 { 9 | 10 | public int[] solve(int A) { 11 | Queue q = new LinkedList<>(); 12 | int ans[] = new int[A]; 13 | int k = 0; 14 | q.add(1); 15 | q.add(2); 16 | q.add(3); 17 | int cnt = 3; 18 | 19 | while (cnt < A) { 20 | int x = q.remove(); 21 | ans[k++] = x; 22 | x = x * 10; 23 | q.add(x + 1); 24 | q.add(x + 2); 25 | q.add(x + 3); 26 | cnt += 3; 27 | } 28 | 29 | for (int i = k; i < A; i++) 30 | ans[i] = q.remove(); 31 | 32 | return ans; 33 | } 34 | 35 | public static void main(String[] args) { 36 | // TODO Auto-generated method stub 37 | Scanner s = new Scanner(System.in); 38 | int n = s.nextInt(); 39 | System.out.println(Arrays.toString(new NintegersContaining123().solve(n))); 40 | s.close(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Tree Data Structure/ValidBST.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ValidBST { 6 | 7 | class Pair { 8 | int min; 9 | int max; 10 | 11 | Pair(int x, int y) { 12 | min = x; 13 | max = y; 14 | } 15 | } 16 | 17 | int isBST = 1; 18 | 19 | Pair Max_Min(TreeNode root) { 20 | if (root == null) 21 | return new Pair(Integer.MAX_VALUE, Integer.MIN_VALUE); 22 | 23 | Pair L = Max_Min(root.left); 24 | Pair R = Max_Min(root.right); 25 | 26 | if (root.val <= L.max || root.val >= R.min) 27 | isBST = 0; 28 | 29 | return new Pair(Math.min(root.val, L.min), Math.max(root.val, R.max)); 30 | } 31 | 32 | public int isValidBST(TreeNode A) { 33 | Max_Min(A); 34 | 35 | return isBST; 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | Scanner s = new Scanner(System.in); 41 | System.out.println(new ValidBST().isValidBST(new GetInput().getInput(s))); 42 | s.close(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MinSwaps.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day29_Mar2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MinSwaps { 6 | 7 | public int solve(int[] A, int B) { 8 | int cnt, min, k = 0; 9 | 10 | for (int i = 0; i < A.length; i++) { 11 | if (A[i] <= B) 12 | k++; 13 | } 14 | cnt = 0; 15 | for (int i = 0; i < k; i++) { 16 | if (A[i] > B) 17 | cnt++; 18 | } 19 | min = cnt; 20 | 21 | for (int i = k; i < A.length; i++) { 22 | if (A[i] > B) 23 | cnt++; 24 | if (A[i - k] > B) 25 | cnt--; 26 | 27 | min = Math.min(min, cnt); 28 | } 29 | 30 | return min; 31 | } 32 | 33 | public static void main(String[] args) { 34 | // TODO Auto-generated method stub 35 | Scanner s = new Scanner(System.in); 36 | int n, A[]; 37 | 38 | n = s.nextInt(); 39 | A = new int[n]; 40 | for (int i = 0; i < n; i++) { 41 | A[i] = s.nextInt(); 42 | } 43 | int B = s.nextInt(); 44 | System.out.print(new MinSwaps().solve(A, B)); 45 | s.close(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Greedy Algorithm/DistributeCandy.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DistributeCandy { 6 | 7 | public int candy(int[] A) { 8 | 9 | int ans[] = new int[A.length]; 10 | ans[0] = 1; 11 | 12 | for (int i = 1; i < A.length; i++) { 13 | if (A[i] > A[i - 1]) 14 | ans[i] = ans[i - 1] + 1; 15 | else 16 | ans[i] = 1; 17 | } 18 | int sum = ans[A.length - 1]; 19 | for (int i = A.length - 2; i >= 0; i--) { 20 | int val = 1; 21 | if (A[i] > A[i + 1]) 22 | val = ans[i + 1] + 1; 23 | sum += Math.max(val, ans[i]); 24 | ans[i] = val; 25 | } 26 | return sum; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | int n, A[]; 33 | 34 | n = s.nextInt(); 35 | A = new int[n]; 36 | for (int i = 0; i < n; i++) { 37 | A[i] = s.nextInt(); 38 | } 39 | System.out.print(new DistributeCandy().candy(A)); 40 | s.close(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Stacks & Queues/RedundantBraces.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day46_Mar23; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class RedundantBraces { 7 | 8 | public int braces(String A) { 9 | Stack S = new Stack<>(); 10 | int l = A.length(); 11 | boolean flg; 12 | for (int i = 0; i < l; i++) { 13 | if (A.charAt(i) != ')') 14 | S.push(A.charAt(i)); 15 | else { 16 | flg = false; 17 | while (S.peek() != '(') { 18 | char c = S.peek(); 19 | if (c == '+' || c == '-' || c == '*' || c == '/') { 20 | flg = true; 21 | } 22 | S.pop(); 23 | } 24 | if (flg == false) { 25 | return 1; 26 | } 27 | S.pop(); 28 | } 29 | } 30 | return 0; 31 | } 32 | 33 | public static void main(String[] args) { 34 | // TODO Auto-generated method stub 35 | Scanner s = new Scanner(System.in); 36 | String A = s.next(); 37 | RedundantBraces rb = new RedundantBraces(); 38 | System.out.println(rb.braces(A)); 39 | s.close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Strings/OrderedSubstring.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day42_Mar18; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | import java.util.Scanner; 6 | 7 | public class OrderedSubstring { 8 | 9 | public String[] solve(String[] A) { 10 | 11 | Arrays.sort(A, new Comparator() { 12 | public int compare(String s1, String s2) { 13 | return s1.length() == s2.length() ? 0 : s1.length() > s2.length() ? 1 : -1; 14 | } 15 | }); 16 | 17 | for (int i = 1; i < A.length; i++) { 18 | if (!A[i].contains(A[i - 1])) 19 | return new String[] { "NO" }; 20 | } 21 | 22 | return A; 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | Scanner s = new Scanner(System.in); 28 | int n = s.nextInt(); 29 | String A[] = new String[n]; 30 | for (int i = 0; i < n; i++) 31 | A[i] = s.next(); 32 | OrderedSubstring os = new OrderedSubstring(); 33 | A = os.solve(A); 34 | System.out.println(Arrays.toString(A)); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/NextPermutation.java: -------------------------------------------------------------------------------- 1 | public class NextPermutation { 2 | 3 | public int[] nextPermutation(int[] A) { 4 | 5 | int l = A.length; 6 | int k, tmp; 7 | 8 | for (k = l - 2; k >= 0; k--) { 9 | if (A[k] < A[k + 1]) { 10 | break; 11 | } 12 | } 13 | 14 | if (k == -1) { 15 | Arrays.sort(A); 16 | return A; 17 | } 18 | 19 | for (int i = l - 1; i >= 0; i--) { 20 | if (A[i] > A[k]) { 21 | tmp = A[i]; 22 | A[i] = A[k]; 23 | A[k] = tmp; 24 | break; 25 | } 26 | } 27 | 28 | Arrays.sort(A, k + 1, l); 29 | 30 | return A; 31 | } 32 | 33 | public static void main(String[] args) { 34 | // TODO Auto-generated method stub 35 | Scanner s = new Scanner(System.in); 36 | int n, A[]; 37 | 38 | n = s.nextInt(); 39 | A = new int[n]; 40 | for (int i = 0; i < n; i++) { 41 | A[i] = s.nextInt(); 42 | } 43 | 44 | NextPermutation c = new NextPermutation(); 45 | A = c.nextPermutation(A); 46 | for (int i = 0; i < n; i++) { 47 | System.out.print(A[i] + " "); 48 | } 49 | s.close(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Strings/MinCharsToPallindrome.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day40_Mar16; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MinCharsToPallindrome { 6 | 7 | public boolean isPallindrome(String A, int s, int e) { 8 | while (s < e) { 9 | if (A.charAt(s) != A.charAt(e)) 10 | return false; 11 | else { 12 | s++; 13 | e--; 14 | } 15 | } 16 | 17 | return true; 18 | } 19 | 20 | public int solve(String A) { 21 | int s = 0, e = A.length() - 1; 22 | 23 | // find max pallindrome from s 24 | while (s < e) { 25 | if (A.charAt(s) == A.charAt(e) && isPallindrome(A, s, e)) { 26 | return A.length() - e - 1; 27 | } else 28 | e--; 29 | } 30 | 31 | return A.length() - e - 1; 32 | } 33 | 34 | public static void main(String[] args) { 35 | // TODO Auto-generated method stub 36 | Scanner s = new Scanner(System.in); 37 | String A; 38 | 39 | A = s.nextLine(); 40 | MinCharsToPallindrome mc = new MinCharsToPallindrome(); 41 | System.out.print(mc.solve(A)); 42 | s.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/SpiralOrderMatrix2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Spiral { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s = new Scanner(System.in); 8 | 9 | int A[][], count, n, val = 1; 10 | 11 | n = s.nextInt(); 12 | count = n * n; 13 | A = new int[n][n]; 14 | 15 | int k = 0, l = n, i, j; 16 | 17 | while (val <= count) { 18 | i = k; 19 | for (j = k; j < l; j++) { 20 | A[i][j] = val++; 21 | } 22 | j = (j >= l) ? j - 1 : j; 23 | 24 | for (i = k + 1; i < l; i++) { 25 | A[i][j] = val++; 26 | } 27 | i = (i >= l) ? i - 1 : i; 28 | 29 | for (j = l - 2; j >= k; j--) { 30 | A[i][j] = val++; 31 | } 32 | j = (j < k) ? j + 1 : j; 33 | 34 | for (i = l - 2; i > k; i--) { 35 | A[i][j] = val++; 36 | } 37 | k++; 38 | l--; 39 | } 40 | 41 | for (i = 0; i < n; i++) { 42 | for (j = 0; j < n; j++) { 43 | System.out.print(A[i][j] + " "); 44 | } 45 | System.out.println(); 46 | } 47 | s.close(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Strings/LongestCommonPrefix.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day40_Mar16; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LongestCommonPrefix { 6 | 7 | public String longestCommonPrefix(String[] A) { 8 | 9 | String ans = ""; 10 | char x; 11 | int i = 0, l = A[0].length(), k; 12 | 13 | while (i < l) { 14 | x = A[0].charAt(i); 15 | for (k = 1; k < A.length; k++) { 16 | if (i >= A[k].length() || A[k].charAt(i) != x) { 17 | break; 18 | } 19 | } 20 | if (k == A.length) { 21 | ans += x; 22 | } else { 23 | return ans; 24 | } 25 | i++; 26 | } 27 | return ans; 28 | } 29 | 30 | public static void main(String[] args) { 31 | // TODO Auto-generated method stub 32 | Scanner s = new Scanner(System.in); 33 | int n; 34 | n = s.nextInt(); 35 | String A[] = new String[n]; 36 | for (int i = 0; i < n; i++) 37 | A[i] = s.next(); 38 | 39 | LongestCommonPrefix lcp = new LongestCommonPrefix(); 40 | System.out.println(lcp.longestCommonPrefix(A)); 41 | s.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/CountDivisors.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day11_Feb7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CountDivisors { 6 | 7 | public int[] solve(int[] A) { 8 | 9 | int l = A.length; 10 | int ans[] = new int[l]; 11 | int B[] = new int[1000001]; 12 | 13 | for (int i = 0; i <= 1000000; i++) 14 | B[i] = 1; 15 | 16 | for (int i = 2; i <= 1000000; i++) { 17 | for (int j = i; j <= 1000000; j += i) { 18 | B[j]++; 19 | } 20 | } 21 | 22 | for (int i = 0; i < l; i++) { 23 | ans[i] = B[A[i]]; 24 | } 25 | 26 | return ans; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | int n = s.nextInt(); 33 | int A[] = new int[n]; 34 | for (int i = 0; i < n; i++) 35 | A[i] = s.nextInt(); 36 | 37 | CountDivisors ln = new CountDivisors(); 38 | A = ln.solve(A); 39 | for (int i = 0; i < n; i++) { 40 | System.out.print(A[i] + " "); 41 | } 42 | 43 | s.close(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/FizzBuzz.md: -------------------------------------------------------------------------------- 1 | # FizzBuzz 2 | ### Problem Description 3 | 4 | Given a positive integer A, return an array of strings with all the integers from 1 to N. But for multiples of 3 5 | the array should have “Fizz” instead of the number. For the multiples of 5, the array should have “Buzz” instead of the number. 6 | For numbers which are multiple of 3 and 5 both, the array should have "FizzBuzz" instead of the number. 7 | 8 | Look at the example for more details. 9 | 10 | 11 | 12 | ### Problem Constraints 13 | 1 <= A <= 500000 14 | 15 | 16 | 17 | ### Input Format 18 | The first argument has the integer A. 19 | 20 | 21 | 22 | ### Output Format 23 | Return an array of string. 24 | 25 | 26 | 27 | ### Example Input 28 | ### Input 1: 29 | 30 | A = 5 31 | 32 | 33 | ### Example Output 34 | ### Output 1: 35 | 36 | [1 2 Fizz 4 Buzz] 37 | 38 | 39 | ### Example Explanation 40 | ### Explanation 1: 41 | 42 | 3 is divisible by 3 so it is replaced by "Fizz". 43 | Similarly, 5 is divisible by 5 so it is replaced by "Buzz". 44 | -------------------------------------------------------------------------------- /Stacks & Queues/NearestSmallerElement.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day46_Mar23; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | import java.util.Stack; 6 | 7 | public class NearestSmallerElement { 8 | 9 | public int[] prevSmaller(int[] A) { 10 | Stack S = new Stack<>(); 11 | int ans[] = new int[A.length]; 12 | ans[0] = -1; 13 | S.push(A[0]); 14 | 15 | for (int i = 1; i < A.length; i++) { 16 | while (S.peek() >= A[i]) { 17 | S.pop(); 18 | if (S.empty()) 19 | break; 20 | } 21 | if (S.empty()) 22 | ans[i] = -1; 23 | else 24 | ans[i] = S.peek(); 25 | 26 | S.push(A[i]); 27 | } 28 | return ans; 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | int n = s.nextInt(); 35 | int A[] = new int[n]; 36 | for (int i = 0; i < n; i++) 37 | A[i] = s.nextInt(); 38 | System.out.print(Arrays.toString(new NearestSmallerElement().prevSmaller(A))); 39 | s.close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MaxAbsoluteDifference.java: -------------------------------------------------------------------------------- 1 | public class MaximumAbsoluteDifference { 2 | 3 | public int maxArr(int[] A) { 4 | 5 | int max_a = A[0], max_b = A[0], min_a = A[0], min_b = A[0]; 6 | int val_a, val_b; 7 | 8 | for (int i = 0; i < A.length; i++) { 9 | val_a = A[i] + i; 10 | val_b = A[i] - i; 11 | 12 | if (max_a < val_a) { 13 | max_a = val_a; 14 | } 15 | if (max_b < val_b) { 16 | max_b = val_b; 17 | } 18 | if (min_a > val_a) { 19 | min_a = val_a; 20 | } 21 | if (min_b > val_b) { 22 | min_b = val_b; 23 | } 24 | } 25 | return ((max_a - min_a) > (max_b - min_b)) ? (max_a - min_a) : (max_b - min_b); 26 | } 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | Scanner s = new Scanner(System.in); 31 | int n, A[]; 32 | n = s.nextInt(); 33 | A = new int[n]; 34 | for (int i = 0; i < n; i++) { 35 | A[i] = s.nextInt(); 36 | } 37 | 38 | MaximumAbsoluteDifference c = new MaximumAbsoluteDifference(); 39 | System.out.println(c.maxArr(A)); 40 | s.close(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MaxChunksSorted2.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day07_Feb3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MaxChunksSorted2 { 6 | 7 | public int solve(int[] A) { 8 | 9 | // Create suffix min array 10 | int smin[], count, l, max; 11 | l = A.length; 12 | smin = new int[l]; 13 | max = Integer.MIN_VALUE; 14 | smin[l - 1] = A[l - 1]; 15 | 16 | for (int i = l - 2; i >= 0; i--) 17 | smin[i] = Math.min(smin[i + 1], A[i]); 18 | 19 | count = 1; 20 | 21 | for (int i = 1; i < l; i++) { 22 | max = Math.max(max, A[i - 1]); 23 | if (smin[i] >= max) 24 | count++; 25 | } 26 | 27 | return count; 28 | } 29 | 30 | public static void main(String[] args) { 31 | // TODO Auto-generated method stub 32 | Scanner s = new Scanner(System.in); 33 | int n, A[]; 34 | 35 | n = s.nextInt(); 36 | A = new int[n]; 37 | for (int i = 0; i < n; i++) { 38 | A[i] = s.nextInt(); 39 | } 40 | 41 | MaxChunksSorted2 c = new MaxChunksSorted2(); 42 | System.out.println(c.solve(A)); 43 | s.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/PrimeSum.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day11_Feb7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PrimeSum { 6 | 7 | public int[] primesum(int A) { 8 | 9 | int ans[] = new int[2]; 10 | 11 | int B[] = new int[A]; 12 | 13 | for (int i = 0; i < A; i++) 14 | B[i] = 1; 15 | 16 | for (int i = 2; i < A; i++) { 17 | if (B[i] != 1) 18 | continue; 19 | for (int j = 2 * i; j < A; j += i) { 20 | B[j] = 0; 21 | } 22 | } 23 | 24 | for (int i = 2; i < A; i++) { 25 | if (B[i] == 1 && B[A - i] == 1) { 26 | ans[0] = i; 27 | ans[1] = A - i; 28 | break; 29 | } 30 | } 31 | 32 | return ans; 33 | } 34 | 35 | public static void main(String[] args) { 36 | // TODO Auto-generated method stub 37 | Scanner s = new Scanner(System.in); 38 | int A[] = new int[2]; 39 | int x = s.nextInt(); 40 | 41 | PrimeSum ln = new PrimeSum(); 42 | A = ln.primesum(x); 43 | for (int i = 0; i < 2; i++) { 44 | System.out.print(A[i] + " "); 45 | } 46 | 47 | s.close(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Recursion/Combinations.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day25_Feb26; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class Combinations { 7 | 8 | ArrayList> ans = new ArrayList<>(); 9 | 10 | public void findCombinations(ArrayList curr, int start, int A, int B) { 11 | if (curr.size() == B) { 12 | ans.add(new ArrayList(curr)); 13 | return; 14 | } 15 | 16 | for (int i = start; i <= A; i++) { 17 | curr.add(i); 18 | findCombinations(curr, i + 1, A, B); 19 | curr.remove(curr.size() - 1); 20 | } 21 | } 22 | 23 | public ArrayList> combine(int A, int B) { 24 | ArrayList tmp = new ArrayList<>(); 25 | findCombinations(tmp, 1, A, B); 26 | 27 | return ans; 28 | } 29 | 30 | public static void main(String[] args) { 31 | // TODO Auto-generated method stub 32 | Scanner s = new Scanner(System.in); 33 | int A = s.nextInt(); 34 | int B = s.nextInt(); 35 | System.out.println(new Combinations().combine(A, B)); 36 | s.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Dynamic Programming/MinSumPath.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day78_Apr27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MinSumPath { 6 | 7 | public int minPathSum(int[][] A) { 8 | int n = A.length; 9 | int m = A[0].length; 10 | 11 | int dp[][] = new int[n][m]; 12 | dp[0][0] = A[0][0]; 13 | 14 | for (int i = 1; i < n; i++) 15 | dp[i][0] = dp[i - 1][0] + A[i][0]; 16 | 17 | for (int j = 1; j < m; j++) 18 | dp[0][j] = dp[0][j - 1] + A[0][j]; 19 | 20 | for (int i = 1; i < n; i++) 21 | for (int j = 1; j < m; j++) 22 | dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + A[i][j]; 23 | 24 | return dp[n - 1][m - 1]; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | int n = s.nextInt(); 31 | int m = s.nextInt(); 32 | 33 | int A[][] = new int[n][m]; 34 | for (int i = 0; i < n; i++) 35 | for (int j = 0; j < m; j++) 36 | A[i][j] = s.nextInt(); 37 | System.out.println(new MinSumPath().minPathSum(A)); 38 | s.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Tree Data Structure/TwoSumBinTree.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class TwoSumBinTree { 7 | 8 | public int t2Sum(TreeNode A, int B) { 9 | int val; 10 | 11 | TreeNode tmp, curr; 12 | Stack S = new Stack<>(); 13 | curr = A; 14 | 15 | while (curr != null || !S.isEmpty()) { 16 | if (curr != null) { 17 | 18 | val = B - curr.val; 19 | tmp = A; 20 | while (tmp != null) { 21 | if (tmp.val == val && tmp != curr) { 22 | return 1; 23 | } else if (tmp.val > val) 24 | tmp = tmp.left; 25 | else 26 | tmp = tmp.right; 27 | } 28 | 29 | S.push(curr); 30 | curr = curr.left; 31 | } else 32 | curr = S.pop().right; 33 | } 34 | 35 | return 0; 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | Scanner s = new Scanner(System.in); 41 | System.out.println(new TwoSumBinTree().t2Sum(new GetInput().getInput(s), s.nextInt())); 42 | s.close(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Dynamic Programming/LongestFibonacciSubset.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day80_Apr29; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class LongestFibonacciSubset { 7 | 8 | public int solve(int[] A) { 9 | int n = A.length; 10 | int max = 0; 11 | int dp[][] = new int[n][n]; 12 | 13 | for (int j = 1; j < n; j++) 14 | dp[0][j] = 2; 15 | 16 | for (int i = 1; i < n; i++) { 17 | for (int j = i + 1; j < n; j++) { 18 | int v = A[j] - A[i]; 19 | int ind = Arrays.binarySearch(A, 0, i, v); 20 | if (ind >= 0) { 21 | dp[i][j] = dp[ind][i] + 1; 22 | max = Math.max(max, dp[i][j]); 23 | } else 24 | dp[i][j] = 2; 25 | } 26 | } 27 | 28 | return max; 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | int n = s.nextInt(); 35 | int[] A = new int[n]; 36 | for (int i = 0; i < n; i++) 37 | A[i] = s.nextInt(); 38 | System.out.println(new LongestFibonacciSubset().solve(A)); 39 | s.close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/PrimeSubsequences.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day108_Jun3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PrimeSubsequences { 6 | 7 | public boolean isPrime(int n) { 8 | if (n == 1) 9 | return false; 10 | if (n == 2) 11 | return true; 12 | 13 | for (int i = 2; i <= Math.sqrt(n); i++) { 14 | if (n % i == 0) 15 | return false; 16 | } 17 | return true; 18 | } 19 | 20 | public int solve(int[] A) { 21 | 22 | int mod = 1000000007; 23 | int cnt = 0; 24 | 25 | for (int i = 0; i < A.length; i++) { 26 | if (isPrime(A[i])) 27 | cnt++; 28 | } 29 | long x = 0; 30 | 31 | while (cnt-- > 0) { 32 | x = (x * 2 + 1) % mod; 33 | } 34 | return (int) x; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n = s.nextInt(); 41 | int A[] = new int[n]; 42 | for (int i = 0; i < n; i++) 43 | A[i] = s.nextInt(); 44 | System.out.println(new PrimeSubsequences().solve(A)); 45 | s.close(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/Pubg.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pubg { 6 | 7 | public int gcd(int A, int B) { 8 | if (A == 1 || B == 1) 9 | return 1; 10 | if (A == 0 || B == 0) 11 | return Math.max(A, B); 12 | 13 | if (A > B) 14 | return gcd(A % B, B); 15 | if (A < B) 16 | return gcd(A, B % A); 17 | 18 | if (A == B) 19 | return A; 20 | 21 | return -1; 22 | } 23 | 24 | public int solve(int[] A) { 25 | if (A.length == 1) 26 | return A[0]; 27 | int g = gcd(A[0], A[1]); 28 | if (A.length == 2) 29 | return g; 30 | 31 | for (int i = 2; i < A.length; i++) { 32 | g = gcd(g, A[i]); 33 | } 34 | return g; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n = s.nextInt(); 41 | int A[] = new int[n]; 42 | for (int i = 0; i < n; i++) 43 | A[i] = s.nextInt(); 44 | Pubg pubg = new Pubg(); 45 | System.out.println(pubg.solve(A)); 46 | s.close(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/SolveSubsequences.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SolveSubsequences { 6 | 7 | public int gcd(int A, int B) { 8 | if (A == 1 || B == 1) 9 | return 1; 10 | if (A == 0 || B == 0) 11 | return Math.max(A, B); 12 | 13 | if (A > B) 14 | return gcd(A % B, B); 15 | if (A < B) 16 | return gcd(A, B % A); 17 | 18 | if (A == B) 19 | return A; 20 | 21 | return -1; 22 | } 23 | 24 | public int solve(int[] A) { 25 | 26 | if (A.length == 1) 27 | return 1; 28 | 29 | int gcd = gcd(A[0], A[1]); 30 | 31 | for (int i = 2; i < A.length; i++) { 32 | gcd = gcd(gcd, A[i]); 33 | } 34 | if (gcd == 1) 35 | return 1; 36 | else 37 | return 0; 38 | } 39 | 40 | public static void main(String[] args) { 41 | // TODO Auto-generated method stub 42 | Scanner s = new Scanner(System.in); 43 | int n = s.nextInt(); 44 | int A[] = new int[n]; 45 | SolveSubsequences ss = new SolveSubsequences(); 46 | System.out.println(ss.solve(A)); 47 | s.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Dynamic Programming/UnstablePermutation.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day114_Jun10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class UnstablePermutation { 6 | 7 | public int solve(int[] A) { 8 | int n = A.length; 9 | boolean isVisited[] = new boolean[n]; 10 | int cnt, maxcnt = 1; 11 | 12 | for (int i = 0; i < n; i++) { 13 | cnt = 1; 14 | if (isVisited[i] == true || A[i] == A[A[i] - 1]) 15 | continue; 16 | 17 | int j = A[i] - 1; 18 | 19 | while (j != i) { 20 | cnt++; 21 | isVisited[j] = true; 22 | j = A[j] - 1; 23 | } 24 | if ((cnt & (cnt - 1)) != 0) 25 | return -1; 26 | 27 | maxcnt = Math.max(maxcnt, cnt); 28 | } 29 | return (int) (Math.log10(maxcnt) / Math.log10(2)); 30 | } 31 | 32 | public static void main(String[] args) { 33 | // TODO Auto-generated method stub 34 | Scanner s = new Scanner(System.in); 35 | int n = s.nextInt(); 36 | int A[] = new int[n]; 37 | for (int i = 0; i < n; i++) 38 | A[i] = s.nextInt(); 39 | System.out.println(new UnstablePermutation().solve(A)); 40 | s.close(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Tree Data Structure/InorderTraversal.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day54_Apr1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Stack; 5 | 6 | public class InorderTraversal { 7 | 8 | // public void inorder(ArrayList ans, TreeNode A) { 9 | // if (A != null) { 10 | // inorder(ans, A.left); 11 | // ans.add(A.val); 12 | // inorder(ans, A.right); 13 | // } 14 | // } 15 | 16 | public ArrayList inorderTraversal(TreeNode A) { 17 | ArrayList ans = new ArrayList<>(); 18 | // inorder(ans, A); 19 | TreeNode tmp, curr; 20 | Stack S = new Stack<>(); 21 | curr = A; 22 | 23 | while (curr != null || !S.isEmpty()) { 24 | if (curr != null) { 25 | S.push(curr); 26 | curr = curr.left; 27 | } else { 28 | tmp = S.pop(); 29 | curr = tmp.right; 30 | ans.add(tmp.val); 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | System.out.println(new InorderTraversal().inorderTraversal(new GetInput().getInput())); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Tree Data Structure/RightView.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day56_Apr3; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.Scanner; 7 | 8 | public class RightView { 9 | 10 | public ArrayList solve(TreeNode A) { 11 | ArrayList ans = new ArrayList<>(); 12 | Queue q = new LinkedList<>(); 13 | TreeNode end, tmp, last; 14 | 15 | q.add(A); 16 | end = A; 17 | last = A; 18 | 19 | while (!q.isEmpty()) { 20 | tmp = q.remove(); 21 | if (tmp.left != null) { 22 | q.add(tmp.left); 23 | last = tmp.left; 24 | } 25 | if (tmp.right != null) { 26 | q.add(tmp.right); 27 | last = tmp.right; 28 | } 29 | if (end == tmp) { 30 | ans.add(tmp.val); 31 | end = last; 32 | } 33 | } 34 | 35 | return ans; 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | Scanner s = new Scanner(System.in); 41 | System.out.println(new RightView().solve(new GetInput().getInput(s))); 42 | s.close(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/MaxAbsoluteDifference.md: -------------------------------------------------------------------------------- 1 | # Maximum Absolute Difference 2 | ### Problem Description 3 | 4 | You are given an array of N integers, A1, A2, .... AN. 5 | 6 | Return the maximum value of f(i, j) for all 1 ≤ i, j ≤ N. f(i, j) is defined as |A[i] - A[j]| + |i - j|, where |x| denotes absolute value of x. 7 | 8 | 9 | 10 | ### Problem Constraints 11 | 1 <= N <= 100000 12 | 13 | -109 <= A[i] <= 109 14 | 15 | 16 | 17 | ### Input Format 18 | First argument is an integer array A of size N. 19 | 20 | 21 | 22 | ### Output Format 23 | Return an integer denoting the maximum value of f(i, j). 24 | 25 | 26 | 27 | ### Example Input 28 | - Input 1: A = [1, 3, -1] 29 | - Input 2: A = [2] 30 | 31 | 32 | ### Example Output 33 | - Output 1: 5 34 | - Output 2: 0 35 | 36 | 37 | ### Example Explanation 38 | - Explanation 1: 39 | 40 | f(1, 1) = f(2, 2) = f(3, 3) = 0 41 | f(1, 2) = f(2, 1) = |1 - 3| + |1 - 2| = 3 42 | f(1, 3) = f(3, 1) = |1 - (-1)| + |1 - 3| = 4 43 | f(2, 3) = f(3, 2) = |3 - (-1)| + |2 - 3| = 5 44 | 45 | So, we return 5. 46 | - Explanation 2: 47 | 48 | Only possibility is i = 1 and j = 1. That gives answer = 0. 49 | -------------------------------------------------------------------------------- /Greedy Algorithm/CandiesInBox.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class CandiesInBox { 7 | 8 | public int solve(int[] A) { 9 | int minSum = Integer.MAX_VALUE, a, sum; 10 | Arrays.parallelSort(A); 11 | 12 | for (int i = 0; i < A.length; i++) { 13 | for (int j = i + 1; j < A.length; j++) { 14 | a = -1; 15 | sum = 0; 16 | for (int k = 0; k < A.length; k++) { 17 | if (k != i && k != j) { 18 | if (a == -1) 19 | a = k; 20 | else { 21 | sum += A[k] - A[a]; 22 | a = -1; 23 | } 24 | } 25 | } 26 | minSum = Math.min(minSum, sum); 27 | } 28 | } 29 | return minSum; 30 | } 31 | 32 | public static void main(String[] args) { 33 | // TODO Auto-generated method stub 34 | Scanner s = new Scanner(System.in); 35 | int n, A[]; 36 | 37 | n = s.nextInt(); 38 | A = new int[n]; 39 | for (int i = 0; i < n; i++) { 40 | A[i] = s.nextInt(); 41 | } 42 | System.out.print(new CandiesInBox().solve(A)); 43 | s.close(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Strings/RegroupWithMinSwaps.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day42_Mar18; 2 | 3 | import java.util.Scanner; 4 | 5 | public class RegroupWithMinSwaps { 6 | 7 | public int solve(final String A) { 8 | int cnt_l = 0, cnt_r = 0, cnt = 0, i = 0, l = A.length(); 9 | 10 | // 1's on left 11 | while (i < l) { 12 | while (i < l && A.charAt(i) == '1') { 13 | i++; 14 | cnt_l += cnt; 15 | } 16 | while (i < l && A.charAt(i) == '0') { 17 | i++; 18 | cnt++; 19 | } 20 | } 21 | 22 | // 1's on right 23 | i = 0; 24 | cnt = 0; 25 | while (i < l) { 26 | while (i < l && A.charAt(i) == '1') { 27 | i++; 28 | cnt++; 29 | } 30 | while (i < l && A.charAt(i) == '0') { 31 | i++; 32 | cnt_r += cnt; 33 | } 34 | } 35 | return Math.min(cnt_l, cnt_r); 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | Scanner s = new Scanner(System.in); 41 | String A = s.next(); 42 | RegroupWithMinSwaps rg = new RegroupWithMinSwaps(); 43 | System.out.println(rg.solve(A)); 44 | s.close(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Hashing/LongestConsecutiveSequence.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day36_Mar11; 2 | 3 | import java.util.HashSet; 4 | import java.util.Scanner; 5 | 6 | public class LongestConsecutiveSequence { 7 | 8 | public int longestConsecutive(final int[] A) { 9 | HashSet H = new HashSet<>(); 10 | for (int i = 0; i < A.length; i++) 11 | H.add(A[i]); 12 | 13 | int cnt, max = Integer.MIN_VALUE, val; 14 | for (int i = 0; i < A.length; i++) { 15 | cnt = 1; 16 | if (!H.contains(A[i] - 1)) { 17 | val = A[i] + 1; 18 | while (H.contains(val)) { 19 | cnt++; 20 | val++; 21 | } 22 | } 23 | max = Math.max(max, cnt); 24 | } 25 | 26 | return max; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | int n = s.nextInt(); 33 | int A[] = new int[n]; 34 | 35 | for (int i = 0; i < n; i++) 36 | A[i] = s.nextInt(); 37 | 38 | LongestConsecutiveSequence lcs = new LongestConsecutiveSequence(); 39 | System.out.print(lcs.longestConsecutive(A)); 40 | s.close(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/CountPairs.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day24_Feb25; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CountPairs { 6 | 7 | public int solve(int[] A, int B) { 8 | long cnt = 0; 9 | int l = A.length; 10 | int freq[] = new int[B]; 11 | 12 | for (int i = 0; i < l; i++) 13 | freq[A[i] % B]++; 14 | 15 | cnt += ((long) freq[0] * (long) (freq[0] - 1)) / 2; 16 | 17 | for (int i = 1; i <= B / 2; i++) { 18 | if (i == (B - i)) 19 | break; 20 | cnt += (long) freq[i] * (long) freq[B - i]; 21 | } 22 | if (B % 2 == 0) { 23 | cnt += ((long) freq[B / 2] * (long) (freq[B / 2] - 1)) / 2; 24 | } 25 | 26 | return (int) (cnt % 1000000007); 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | int n, A[]; 33 | 34 | n = s.nextInt(); 35 | A = new int[n]; 36 | for (int i = 0; i < n; i++) { 37 | A[i] = s.nextInt(); 38 | } 39 | int b = s.nextInt(); 40 | CountPairs cp = new CountPairs(); 41 | System.out.print(cp.solve(A, b)); 42 | s.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/DeleteElements.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DeleteElements { 6 | 7 | public int gcd(int A, int B) { 8 | if (A == 1 || B == 1) 9 | return 1; 10 | if (A == 0 || B == 0) 11 | return Math.max(A, B); 12 | 13 | if (A > B) 14 | return gcd(A % B, B); 15 | if (A < B) 16 | return gcd(A, B % A); 17 | 18 | if (A == B) 19 | return A; 20 | 21 | return -1; 22 | } 23 | 24 | public int solve(int[] A) { 25 | if (A.length == 1) 26 | return -1; 27 | int g = gcd(A[0], A[1]); 28 | for (int i = 2; i < A.length; i++) { 29 | g = gcd(g, A[i]); 30 | } 31 | if (g != 1) 32 | return -1; 33 | else 34 | return 0; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n = s.nextInt(); 41 | int A[] = new int[n]; 42 | for (int i = 0; i < n; i++) 43 | A[i] = s.nextInt(); 44 | DeleteElements de = new DeleteElements(); 45 | System.out.println(de.solve(A)); 46 | s.close(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Tree Data Structure/KDistance.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day61_Apr8; 2 | 3 | import java.util.Scanner; 4 | 5 | public class KDistance { 6 | 7 | int count = 0; 8 | 9 | void calc2(int val, TreeNode A, int B) { 10 | if (A.left != null) { 11 | if (Math.abs(val - A.left.val) <= B) 12 | count++; 13 | calc2(val, A.left, B); 14 | } 15 | 16 | if (A.right != null) { 17 | if (Math.abs(val - A.right.val) <= B) 18 | count++; 19 | calc2(val, A.right, B); 20 | } 21 | } 22 | 23 | void calc1(TreeNode A, int B) { 24 | calc2(A.val, A, B); 25 | 26 | if (A.left != null) 27 | calc1(A.left, B); 28 | 29 | if (A.right != null) 30 | calc1(A.right, B); 31 | 32 | } 33 | 34 | public int solve(TreeNode A, int B) { 35 | calc1(A, B); 36 | 37 | return count; 38 | } 39 | 40 | public static void main(String[] args) { 41 | // TODO Auto-generated method stub 42 | Scanner s = new Scanner(System.in); 43 | GetInput gi = new GetInput(); 44 | TreeNode A = gi.getInput(s); 45 | int B = s.nextInt(); 46 | System.out.println(new KDistance().solve(A, B)); 47 | s.close(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/RemoveElements.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day73_Apr21; 2 | 3 | import java.util.Collections; 4 | import java.util.PriorityQueue; 5 | import java.util.Scanner; 6 | 7 | public class RemoveElements { 8 | 9 | public int solve(int[] A) { 10 | PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); 11 | long sum = 0; 12 | int mod = 1000000007; 13 | int n = A.length; 14 | 15 | for (int i = 0; i < n; i++) { 16 | sum = (sum + A[i]) % mod; 17 | maxHeap.add(A[i]); 18 | } 19 | 20 | long currSum = sum; 21 | while (n > 0) { 22 | currSum = Math.floorMod(currSum - maxHeap.remove(), mod); 23 | sum = Math.floorMod(sum + currSum, mod); 24 | n--; 25 | } 26 | 27 | return (int) sum; 28 | } 29 | 30 | public static void main(String[] args) { 31 | // TODO Auto-generated method stub 32 | Scanner s = new Scanner(System.in); 33 | int n, A[]; 34 | 35 | n = s.nextInt(); 36 | A = new int[n]; 37 | for (int i = 0; i < n; i++) { 38 | A[i] = s.nextInt(); 39 | } 40 | System.out.print(new RemoveElements().solve(A)); 41 | s.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Recursion/Subset.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day23_Feb24; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Scanner; 6 | 7 | public class Subset { 8 | 9 | ArrayList> ans = new ArrayList<>(); 10 | 11 | public void findSubsets(ArrayList curr, int start, ArrayList A) { 12 | 13 | ans.add(new ArrayList(curr)); 14 | 15 | for (int i = start; i < A.size(); i++) { 16 | curr.add(A.get(i)); 17 | findSubsets(curr, i + 1, A); 18 | curr.remove(curr.size() - 1); 19 | } 20 | } 21 | 22 | public ArrayList> subsets(ArrayList A) { 23 | ArrayList curr = new ArrayList<>(); 24 | Collections.sort(A); 25 | findSubsets(curr, 0, A); 26 | 27 | return ans; 28 | } 29 | 30 | public static void main(String[] args) { 31 | // TODO Auto-generated method stub 32 | ArrayList A = new ArrayList<>(); 33 | Scanner s = new Scanner(System.in); 34 | int n = s.nextInt(); 35 | while (n-- > 0) 36 | A.add(s.nextInt()); 37 | System.out.println(new Subset().subsets(A)); 38 | s.close(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/MergeIntervals.md: -------------------------------------------------------------------------------- 1 | # Merge Intervals 2 | ### Problem Description 3 | 4 | Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). 5 | 6 | You may assume that the intervals were initially sorted according to their start times. 7 | 8 | 9 | 10 | ### Problem Constraints 11 | 1 <= |intervals| <= 105 12 | 13 | 14 | 15 | ### Input Format 16 | First argument is the vector of intervals 17 | 18 | second argument is the new interval to be merged 19 | 20 | 21 | 22 | ### Output Format 23 | Return the vector of intervals after merging 24 | 25 | 26 | 27 | ### Example Input 28 | ### Input 1: 29 | 30 | Given intervals [1, 3], [6, 9] insert and merge [2, 5] . 31 | ### Input 2: 32 | 33 | Given intervals [1, 3], [6, 9] insert and merge [2, 6] . 34 | 35 | 36 | ### Example Output 37 | ### Output 1: 38 | 39 | [ [1, 5], [6, 9] ] 40 | ### Output 2: 41 | 42 | [ [1, 9] ] 43 | 44 | 45 | ### Example Explanation 46 | ### Explanation 1: 47 | 48 | (2,5) does not completely merge the given intervals 49 | ### Explanation 2: 50 | 51 | (2,6) completely merges the given intervals 52 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/LargestCoprimeDivisor.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day11_Feb7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LargestCoprimeDivisor { 6 | 7 | public int gcd(int A, int B) { 8 | if (A == 1 || B == 1) 9 | return 1; 10 | if (A == 0 || B == 0) 11 | return Math.max(A, B); 12 | 13 | if (A > B) 14 | return gcd(A % B, B); 15 | if (A < B) 16 | return gcd(A, B % A); 17 | 18 | if (A == B) 19 | return A; 20 | 21 | return -1; 22 | } 23 | 24 | public int cpFact(int A, int B) { 25 | int m = 1; 26 | for (int i = 1; i <= Math.sqrt(A); i++) { 27 | if ((A % i == 0)) { 28 | if (gcd(i, B) == 1) 29 | m = Math.max(m, i); 30 | if (gcd(A / i, B) == 1) 31 | m = Math.max(m, A / i); 32 | } 33 | } 34 | return m; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int a, b; 41 | 42 | a = s.nextInt(); 43 | b = s.nextInt(); 44 | LargestCoprimeDivisor lcd = new LargestCoprimeDivisor(); 45 | System.out.println(lcd.cpFact(a, b)); 46 | s.close(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Tree Data Structure/LeftView.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day56_Apr3; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.Scanner; 7 | 8 | public class LeftView { 9 | 10 | public ArrayList solve(TreeNode A) { 11 | ArrayList ans = new ArrayList<>(); 12 | Queue q = new LinkedList<>(); 13 | TreeNode end, tmp, last; 14 | 15 | q.add(A); 16 | end = A; 17 | last = A; 18 | ans.add(A.val); 19 | 20 | while (!q.isEmpty()) { 21 | tmp = q.remove(); 22 | if (tmp.left != null) { 23 | q.add(tmp.left); 24 | last = tmp.left; 25 | } 26 | if (tmp.right != null) { 27 | q.add(tmp.right); 28 | last = tmp.right; 29 | } 30 | if (end == tmp) { 31 | end = last; 32 | if (!q.isEmpty()) 33 | ans.add(q.peek().val); 34 | } 35 | } 36 | 37 | return ans; 38 | } 39 | 40 | public static void main(String[] args) { 41 | // TODO Auto-generated method stub 42 | Scanner s = new Scanner(System.in); 43 | System.out.println(new LeftView().solve(new GetInput().getInput(s))); 44 | s.close(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/MaxConsecutiveGap.md: -------------------------------------------------------------------------------- 1 | # Maximum Consecutive Gap 2 | ### Problem Description 3 | 4 | Given an unsorted integer array A of size N. 5 | Find the maximum difference between the successive elements in its sorted form. 6 | 7 | Try to solve it in linear time/space. 8 | 9 | You may assume that all the elements in the array are non-negative integers and fit in the 32-bit signed integer range. 10 | You may also assume that the difference will not overflow. 11 | Return 0 if the array contains less than 2 elements. 12 | 13 | 14 | 15 | ### Problem Constraints 16 | 1 <= N <= 106 17 | 18 | 1 <= A[i] <= 109 19 | 20 | 21 | 22 | ### Input Format 23 | First argument is an integer array A of size N. 24 | 25 | 26 | 27 | ### Output Format 28 | Return an integer denoting the maximum difference. 29 | 30 | 31 | 32 | ### Example Input 33 | - Input 1: A = [1, 10, 5] 34 | - Input 2: A = [10, 9, 10] 35 | 36 | ### Example Output 37 | - Output 1: 5 38 | - Output 2: 1 39 | 40 | 41 | ### Example Explanation 42 | - Explanation 1: After sorting, the array becomes [1, 5, 10] 43 | Maximum difference is (10 - 5) = 5. 44 | - Explanation 2: Maximum difference is (10 - 9) = 1. 45 | -------------------------------------------------------------------------------- /Dynamic Programming/MatrixChainMultiplication.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day83_May4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class MatrixChainMultiplication { 7 | int[][] dp; 8 | 9 | int calc(int[] A, int i, int j) { 10 | if (i == j) 11 | return 0; 12 | 13 | if (dp[i][j] != Integer.MAX_VALUE) 14 | return dp[i][j]; 15 | 16 | for (int k = i; k < j; k++) { 17 | int count = calc(A, i, k) + calc(A, k + 1, j) + A[i - 1] * A[k] * A[j]; 18 | 19 | dp[i][j] = Math.min(dp[i][j], count); 20 | } 21 | return dp[i][j]; 22 | } 23 | 24 | public int solve(int[] A) { 25 | int n = A.length; 26 | dp = new int[n][n]; 27 | for (int i = 0; i < n; i++) 28 | Arrays.fill(dp[i], Integer.MAX_VALUE); 29 | 30 | return calc(A, 1, n - 1); 31 | } 32 | 33 | public static void main(String[] args) { 34 | // TODO Auto-generated method stub 35 | Scanner s = new Scanner(System.in); 36 | int n = s.nextInt(); 37 | int[] A = new int[n]; 38 | for (int i = 0; i < n; i++) 39 | A[i] = s.nextInt(); 40 | System.out.println(new MatrixChainMultiplication().solve(A)); 41 | s.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Stacks & Queues/OrderThem.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day44_Mar20; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class OrderThem { 7 | 8 | public int solve(int[] A) { 9 | int C[] = new int[A.length]; 10 | Stack B = new Stack(); 11 | 12 | int rank = 1, k = 0, i = 0; 13 | 14 | while (A[i] != 1) 15 | B.push(A[i++]); 16 | 17 | while (i < A.length) { 18 | if (A[i] == rank) { 19 | C[k++] = rank++; 20 | } else if (!B.isEmpty() && B.peek() == rank) { 21 | C[k++] = B.pop(); 22 | i--; 23 | rank++; 24 | } else 25 | B.push(A[i]); 26 | i++; 27 | } 28 | while (!B.isEmpty()) { 29 | if (B.peek() != rank) 30 | return 0; 31 | C[k++] = B.pop(); 32 | rank++; 33 | } 34 | return 1; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n = s.nextInt(); 41 | int A[] = new int[n]; 42 | for (int i = 0; i < n; i++) 43 | A[i] = s.nextInt(); 44 | OrderThem ot = new OrderThem(); 45 | System.out.print(ot.solve(A)); 46 | s.close(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Stacks & Queues/SortArrayUsingStack.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day44_Mar20; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class SortArrayUsingStack { 7 | 8 | public void sortInserted(Stack S, int x) { 9 | int temp; 10 | if (S.isEmpty() || S.peek() >= x) { 11 | S.push(x); 12 | } else { 13 | temp = S.pop(); 14 | sortInserted(S, x); 15 | S.push(temp); 16 | } 17 | } 18 | 19 | public int[] solve(int[] A) { 20 | 21 | Stack S = new Stack<>(); 22 | S.push(A[0]); 23 | for (int i = 1; i < A.length; i++) { 24 | sortInserted(S, A[i]); 25 | } 26 | 27 | for (int i = 0; i < A.length; i++) 28 | A[i] = S.pop(); 29 | 30 | return A; 31 | } 32 | 33 | public static void main(String[] args) { 34 | // TODO Auto-generated method stub 35 | Scanner s = new Scanner(System.in); 36 | int n = s.nextInt(); 37 | int A[] = new int[n]; 38 | for (int i = 0; i < n; i++) 39 | A[i] = s.nextInt(); 40 | SortArrayUsingStack sa = new SortArrayUsingStack(); 41 | A = sa.solve(A); 42 | for (int i = 0; i < n; i++) 43 | System.out.print(A[i] + " "); 44 | s.close(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Strings/StrStr.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day40_Mar16; 2 | 3 | import java.util.Scanner; 4 | 5 | public class StrStr { 6 | 7 | public int strStr(final String A, final String B) { 8 | 9 | if (A == "" || B == "") 10 | return -1; 11 | 12 | int x, y, l1 = A.length(), l2 = B.length(); 13 | boolean flg; 14 | 15 | for (int i = 0; i < l1; i++) { 16 | if (A.charAt(i) == B.charAt(0)) { 17 | x = 0; 18 | y = l2 - 1; 19 | flg = true; 20 | while (x <= y) { 21 | if (i + x >= l1 || i + y >= l1) { 22 | flg = false; 23 | break; 24 | } else if (A.charAt(i + x) != B.charAt(x) || A.charAt(i + y) != B.charAt(y)) { 25 | flg = false; 26 | break; 27 | } 28 | x++; 29 | y--; 30 | } 31 | if (flg == true) 32 | return i; 33 | } 34 | } 35 | 36 | return -1; 37 | } 38 | 39 | public static void main(String[] args) { 40 | // TODO Auto-generated method stub 41 | Scanner s = new Scanner(System.in); 42 | String A, B; 43 | 44 | A = s.nextLine(); 45 | B = s.nextLine(); 46 | StrStr str = new StrStr(); 47 | System.out.print(str.strStr(A, B)); 48 | s.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/WaveArray.md: -------------------------------------------------------------------------------- 1 | # Wave Array 2 | ### Problem Description 3 | 4 | Given an array of integers A, sort the array into a wave like array and return it, In other words, arrange the elements 5 | into a sequence such that a1 >= a2 <= a3 >= a4 <= a5..... 6 | 7 | NOTE : If there are multiple answers possible, return the one that's lexicographically smallest. 8 | 9 | 10 | 11 | ### Problem Constraints 12 | 1 <= len(A) <= 106 13 | 1 <= A[i] <= 106 14 | 15 | 16 | 17 | ### Input Format 18 | First argument is an integer array A. 19 | 20 | 21 | 22 | ### Output Format 23 | Return an array arranged in the sequence as described. 24 | 25 | 26 | 27 | ### Example Input 28 | ### Input 1: 29 | 30 | A = [1, 2, 3, 4] 31 | ### Input 2: 32 | 33 | A = [1, 2] 34 | 35 | 36 | ### Example Output 37 | ### Output 1: 38 | 39 | [2, 1, 4, 3] 40 | ### Output 2: 41 | 42 | [2, 1] 43 | 44 | 45 | ### Example Explanation 46 | ### Explanation 1: 47 | 48 | One possible answer : [2, 1, 4, 3] 49 | Another possible answer : [4, 1, 3, 2] 50 | First answer is lexicographically smallest. So, return [2, 1, 4, 3]. 51 | ### Explanation 1: 52 | 53 | Only possible answer is [2, 1]. 54 | -------------------------------------------------------------------------------- /Greedy Algorithm/GasStation.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day48_Mar25; 2 | 3 | import java.util.Scanner; 4 | 5 | public class GasStation { 6 | 7 | public int canCompleteCircuit(final int[] A, final int[] B) { 8 | 9 | int j, cnt, cap = 0, n = A.length; 10 | 11 | for (int i = 0; i < n; i++) 12 | cap += A[i] - B[i]; 13 | 14 | if (cap < 0) 15 | return -1; 16 | 17 | for (int i = 0; i < n; i++) { 18 | if (A[i] >= B[i]) { 19 | cap = 0; 20 | j = i; 21 | cnt = 0; 22 | while (cnt < n) { 23 | cap += A[j] - B[j]; 24 | if (cap < 0) 25 | break; 26 | j = (j + 1) % n; 27 | cnt++; 28 | } 29 | if (cnt == n) 30 | return i; 31 | } 32 | } 33 | 34 | return -1; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n = s.nextInt(); 41 | int A[] = new int[n]; 42 | for (int i = 0; i < n; i++) 43 | A[i] = s.nextInt(); 44 | int B[] = new int[n]; 45 | for (int i = 0; i < n; i++) 46 | B[i] = s.nextInt(); 47 | 48 | System.out.println(new GasStation().canCompleteCircuit(A, B)); 49 | s.close(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Dynamic Programming/UniquePaths.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day78_Apr27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class UniquePaths { 6 | 7 | public int uniquePathsWithObstacles(int[][] A) { 8 | int n = A.length; 9 | int m = A[0].length; 10 | 11 | int dp[][] = new int[n][m]; 12 | 13 | for (int i = 0; i < n; i++) { 14 | if (A[i][0] == 1) 15 | break; 16 | dp[i][0] = 1; 17 | } 18 | for (int j = 0; j < m; j++) { 19 | if (A[0][j] == 1) 20 | break; 21 | dp[0][j] = 1; 22 | } 23 | 24 | for (int i = 1; i < n; i++) { 25 | for (int j = 1; j < m; j++) { 26 | if (A[i][j] == 1) 27 | dp[i][j] = 0; 28 | else 29 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 30 | } 31 | } 32 | return dp[n - 1][m - 1]; 33 | } 34 | 35 | public static void main(String[] args) { 36 | // TODO Auto-generated method stub 37 | Scanner s = new Scanner(System.in); 38 | int n = s.nextInt(); 39 | int m = s.nextInt(); 40 | 41 | int A[][] = new int[n][m]; 42 | for (int i = 0; i < n; i++) 43 | for (int j = 0; j < m; j++) 44 | A[i][j] = s.nextInt(); 45 | System.out.println(new UniquePaths().uniquePathsWithObstacles(A)); 46 | s.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Dynamic Programming/WordBreak.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day108_Jun3; 2 | 3 | import java.util.HashSet; 4 | import java.util.Scanner; 5 | 6 | public class WordBreak { 7 | 8 | public int wordBreak(String A, String[] B) { 9 | int n = A.length(); 10 | int maxl = 0; 11 | boolean isValid[] = new boolean[n + 1]; 12 | HashSet set = new HashSet<>(); 13 | 14 | for (int i = 0; i < B.length; i++) { 15 | set.add(B[i]); 16 | maxl = Math.max(maxl, B[i].length()); 17 | } 18 | 19 | isValid[0] = true; 20 | 21 | for (int i = 1; i <= n; i++) { 22 | for (int j = i - 1; j >= 0 && i - j <= maxl; j--) { 23 | String str = A.substring(j, i); 24 | 25 | if (isValid[j] && set.contains(str)) { 26 | isValid[i] = true; 27 | break; 28 | } 29 | } 30 | } 31 | 32 | return isValid[n] == true ? 1 : 0; 33 | } 34 | 35 | public static void main(String[] args) { 36 | // TODO Auto-generated method stub 37 | Scanner s = new Scanner(System.in); 38 | int n = s.nextInt(); 39 | String A[] = new String[n]; 40 | for (int i = 0; i < n; i++) 41 | A[i] = s.next(); 42 | System.out.println(new WordBreak().wordBreak(s.next(), A)); 43 | s.close(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Linked List/SwapPairs.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day50_Mar27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SwapPairs { 6 | 7 | public ListNode swapPairs(ListNode A) { 8 | int n = 0; 9 | ListNode tmp = A, n1, n2; 10 | while (tmp != null) { 11 | n++; 12 | tmp = tmp.next; 13 | } 14 | if (n == 1) 15 | return A; 16 | n = n / 2 - 1; 17 | 18 | n1 = A; 19 | n2 = A.next; 20 | tmp = n2.next; 21 | A = n2; 22 | 23 | while (n-- > 0) { 24 | n2.next = n1; 25 | n1.next = tmp.next; 26 | n1 = tmp; 27 | n2 = tmp.next; 28 | tmp = n2.next; 29 | } 30 | n2.next = n1; 31 | n1.next = tmp; 32 | 33 | return A; 34 | } 35 | 36 | public static void main(String[] args) { 37 | // TODO Auto-generated method stub 38 | Scanner s = new Scanner(System.in); 39 | int n = s.nextInt(); 40 | ListNode A = new ListNode(s.nextInt()); 41 | ListNode tail = A; 42 | while (n-- > 1) { 43 | ListNode tmp = new ListNode(s.nextInt()); 44 | tail.next = tmp; 45 | tail = tmp; 46 | } 47 | A = new SwapPairs().swapPairs(A); 48 | while (A != null) { 49 | System.out.print(A.val + " "); 50 | A = A.next; 51 | } 52 | s.close(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Linked List/RemoveNthNodeFromEnd.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day50_Mar27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class RemoveNthNodeFromEnd { 6 | 7 | public ListNode removeNthFromEnd(ListNode A, int B) { 8 | int n = 0; 9 | ListNode tmp = A; 10 | 11 | while (tmp != null) { 12 | n++; 13 | tmp = tmp.next; 14 | } 15 | 16 | if (B > n) 17 | return A.next; 18 | 19 | n = n - B + 1; 20 | if (n == 1) 21 | return A.next; 22 | 23 | tmp = A; 24 | while (n-- > 2) 25 | tmp = tmp.next; 26 | 27 | tmp.next = tmp.next.next; 28 | 29 | return A; 30 | } 31 | 32 | public static void main(String[] args) { 33 | // TODO Auto-generated method stub 34 | Scanner s = new Scanner(System.in); 35 | int n = s.nextInt(); 36 | RemoveNthNodeFromEnd rn = new RemoveNthNodeFromEnd(); 37 | ListNode A = new ListNode(s.nextInt()); 38 | ListNode tail = A; 39 | while (n-- > 1) { 40 | ListNode tmp = new ListNode(s.nextInt()); 41 | tail.next = tmp; 42 | tail = tmp; 43 | } 44 | int B = s.nextInt(); 45 | A = rn.removeNthFromEnd(A, B); 46 | while (A != null) { 47 | System.out.print(A.val + " "); 48 | A = A.next; 49 | } 50 | s.close(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Tree Data Structure/LevelOrderTraversal.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day54_Apr1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | 7 | public class LevelOrderTraversal { 8 | 9 | public ArrayList> levelOrder(TreeNode A) { 10 | ArrayList> ans = new ArrayList<>(); 11 | Queue q = new LinkedList<>(); 12 | ArrayList al; 13 | TreeNode end, tmp, last, top; 14 | 15 | q.add(A); 16 | end = A; 17 | last = A; 18 | 19 | al = new ArrayList(); 20 | while (!q.isEmpty()) { 21 | top = q.peek(); 22 | tmp = q.remove(); 23 | if (tmp.left != null) { 24 | q.add(tmp.left); 25 | last = tmp.left; 26 | } 27 | if (tmp.right != null) { 28 | q.add(tmp.right); 29 | last = tmp.right; 30 | } 31 | al.add(tmp.val); 32 | if (end == top) { 33 | ans.add(al); 34 | end = last; 35 | al = new ArrayList(); 36 | } 37 | } 38 | 39 | return ans; 40 | } 41 | 42 | public static void main(String[] args) { 43 | // TODO Auto-generated method stub 44 | System.out.println(new LevelOrderTraversal().levelOrder(new GetInput().getInput())); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Greedy Algorithm/ShipCompany.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day72_Apr20; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.PriorityQueue; 6 | import java.util.Scanner; 7 | 8 | public class ShipCompany { 9 | 10 | public int[] solve(int A, int B, int[] C) { 11 | PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); 12 | int min = 0, max = 0, i = 0; 13 | 14 | for (i = 0; i < C.length; i++) 15 | maxHeap.add(C[i]); 16 | 17 | Arrays.parallelSort(C); 18 | 19 | i = 0; 20 | while (A > 0) { 21 | max += maxHeap.peek(); 22 | maxHeap.add(maxHeap.remove() - 1); 23 | 24 | if (C[i] == 0) 25 | i++; 26 | 27 | min += C[i]--; 28 | A--; 29 | } 30 | return new int[] { max, min }; 31 | } 32 | 33 | public static void main(String[] args) { 34 | // TODO Auto-generated method stub 35 | Scanner s = new Scanner(System.in); 36 | int n, A, B, C[]; 37 | A = s.nextInt(); 38 | B = s.nextInt(); 39 | n = s.nextInt(); 40 | C = new int[n]; 41 | for (int i = 0; i < n; i++) { 42 | C[i] = s.nextInt(); 43 | } 44 | System.out.print(Arrays.toString(new ShipCompany().solve(A, B, C))); 45 | s.close(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Dynamic Programming/EditDistance.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day78_Apr27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class EditDistance { 6 | 7 | public int minDistance(String A, String B) { 8 | int m = A.length(); 9 | int n = B.length(); 10 | int x = 0, y; 11 | 12 | if (n < m) { 13 | String tmp = A; 14 | A = B; 15 | B = tmp; 16 | int t = n; 17 | n = m; 18 | m = t; 19 | } 20 | 21 | int[][] dp = new int[2][m + 1]; 22 | 23 | for (int i = 0; i <= n; i++) { 24 | x = i & 1; 25 | y = (x + 1) & 1; 26 | for (int j = 0; j <= m; j++) { 27 | 28 | if (i == 0 && j == 0) 29 | dp[x][j] = 0; 30 | 31 | else if (i == 0 || j == 0) 32 | dp[x][j] = Math.max(i, j); 33 | 34 | else if (B.charAt(i - 1) == A.charAt(j - 1)) 35 | dp[x][j] = dp[y][j - 1]; 36 | 37 | else 38 | dp[x][j] = 1 + Math.min(Math.min(dp[y][j - 1], dp[y][j]), dp[x][j - 1]); 39 | } 40 | } 41 | 42 | return dp[x][m]; 43 | } 44 | 45 | public static void main(String[] args) { 46 | // TODO Auto-generated method stub 47 | Scanner s = new Scanner(System.in); 48 | System.out.println(new EditDistance().minDistance(s.next(), s.next())); 49 | s.close(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Two Pointers/ThreeSumClosest.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day33_Mar6; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class ThreeSumClosest { 7 | 8 | public int threeSumClosest(int[] A, int B) { 9 | 10 | int i, j, k, d, sum, l, min, ans = 0; 11 | l = A.length; 12 | min = Integer.MAX_VALUE; 13 | Arrays.sort(A); 14 | 15 | for (i = 0; i < l - 2; i++) { 16 | j = i + 1; 17 | k = l - 1; 18 | while (j < k) { 19 | sum = A[i] + A[j] + A[k]; 20 | d = Math.abs(sum - B); 21 | if (d == 0) { 22 | return sum; 23 | } 24 | if (d < min) { 25 | min = d; 26 | ans = sum; 27 | } 28 | if (sum <= B) 29 | j++; 30 | else 31 | k--; 32 | } 33 | } 34 | 35 | return ans; 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | Scanner s = new Scanner(System.in); 41 | int n, A[]; 42 | 43 | n = s.nextInt(); 44 | A = new int[n]; 45 | for (int i = 0; i < n; i++) { 46 | A[i] = s.nextInt(); 47 | } 48 | int b = s.nextInt(); 49 | ThreeSumClosest tsc = new ThreeSumClosest(); 50 | System.out.print(tsc.threeSumClosest(A, b)); 51 | s.close(); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Tree Data Structure/SymmetricTree.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day56_Apr3; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class SymmetricTree { 7 | 8 | public int isSymmetric(TreeNode A) { 9 | ArrayList q = new ArrayList<>(); 10 | TreeNode end, tmp, last; 11 | 12 | q.add(A); 13 | end = A; 14 | last = A; 15 | 16 | while (!q.isEmpty()) { 17 | tmp = q.remove(0); 18 | if (tmp.left != null) { 19 | q.add(tmp.left); 20 | last = tmp.left; 21 | } 22 | if (tmp.right != null) { 23 | q.add(tmp.right); 24 | last = tmp.right; 25 | } 26 | if (end == tmp) { 27 | end = last; 28 | 29 | int size = q.size(); 30 | if (size % 2 == 1) 31 | return 0; 32 | int hd = 0, tl = size - 1; 33 | 34 | while (hd < tl) { 35 | if (q.get(hd).val != q.get(tl).val) 36 | return 0; 37 | hd++; 38 | tl--; 39 | } 40 | } 41 | } 42 | 43 | return 1; 44 | } 45 | 46 | public static void main(String[] args) { 47 | // TODO Auto-generated method stub 48 | Scanner s = new Scanner(System.in); 49 | System.out.println(new SymmetricTree().isSymmetric(new GetInput().getInput(s))); 50 | s.close(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Tree Data Structure/DifferenceBwlevels.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day56_Apr3; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class DifferenceBwlevels { 7 | 8 | public int solve(TreeNode A) { 9 | 10 | ArrayList q = new ArrayList<>(); 11 | TreeNode end, tmp, last; 12 | int sum = 0, oddsum = A.val, evensum = 0, lvl = 2; 13 | 14 | q.add(A); 15 | end = A; 16 | last = A; 17 | 18 | while (!q.isEmpty()) { 19 | tmp = q.remove(0); 20 | if (tmp.left != null) { 21 | q.add(tmp.left); 22 | last = tmp.left; 23 | sum += tmp.left.val; 24 | } 25 | if (tmp.right != null) { 26 | q.add(tmp.right); 27 | last = tmp.right; 28 | sum += tmp.right.val; 29 | } 30 | if (end == tmp) { 31 | end = last; 32 | if (lvl % 2 == 1) 33 | oddsum += sum; 34 | else 35 | evensum += sum; 36 | sum = 0; 37 | lvl++; 38 | } 39 | } 40 | 41 | return oddsum - evensum; 42 | } 43 | 44 | public static void main(String[] args) { 45 | // TODO Auto-generated method stub 46 | Scanner s = new Scanner(System.in); 47 | System.out.println(new DifferenceBwlevels().solve(new GetInput().getInput(s))); 48 | s.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/LargestNumber.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day31_Mar4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | import java.util.Scanner; 6 | 7 | public class LargestNumber { 8 | 9 | public String largestNumber(final int[] A) { 10 | 11 | String[] ans = new String[A.length]; 12 | 13 | for (int i = 0; i < A.length; i++) { 14 | ans[i] = String.valueOf(A[i]); 15 | } 16 | Arrays.sort(ans, new Comparator() { 17 | public int compare(String X, String Y) { 18 | String XY = X + Y; 19 | String YX = Y + X; 20 | 21 | return YX.compareTo(XY); 22 | } 23 | }); 24 | 25 | StringBuilder sb = new StringBuilder(); 26 | for (int i = 0; i < A.length; i++) 27 | sb.append(ans[i]); 28 | 29 | while (sb.charAt(0) == '0' && sb.length() > 0) 30 | sb.deleteCharAt(0); 31 | 32 | return sb.toString(); 33 | } 34 | 35 | public static void main(String[] args) { 36 | // TODO Auto-generated method stub 37 | Scanner s = new Scanner(System.in); 38 | int n, A[]; 39 | 40 | n = s.nextInt(); 41 | A = new int[n]; 42 | for (int i = 0; i < n; i++) { 43 | A[i] = s.nextInt(); 44 | } 45 | System.out.print(new LargestNumber().largestNumber(A)); 46 | s.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Binary Search/MatrixSearch.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day21_Feb19; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MatrixSearch { 6 | 7 | public int binarySearch(int[] A, int B) { 8 | int piv, lo, hi; 9 | 10 | lo = 0; 11 | hi = A.length - 1; 12 | 13 | while (lo <= hi) { 14 | piv = (lo + hi) / 2; 15 | if (B == A[piv]) 16 | return 1; 17 | else if (B > A[piv]) 18 | lo = piv + 1; 19 | else 20 | hi = piv - 1; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | public int searchMatrix(int[][] A, int B) { 27 | 28 | int r = A.length; 29 | 30 | for (int i = r - 1; i >= 0; i--) { 31 | if (B >= A[i][0]) { 32 | return binarySearch(A[i], B); 33 | } 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | public static void main(String[] args) { 40 | // TODO Auto-generated method stub 41 | Scanner s = new Scanner(System.in); 42 | int m = s.nextInt(); 43 | int n = s.nextInt(); 44 | int A[][] = new int[m][n]; 45 | 46 | for (int i = 0; i < m; i++) 47 | for (int j = 0; j < n; j++) 48 | A[i][j] = s.nextInt(); 49 | 50 | int b = s.nextInt(); 51 | 52 | MatrixSearch ms = new MatrixSearch(); 53 | System.out.println(ms.searchMatrix(A, b)); 54 | s.close(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/SrchRowColWiseSortedMatrix.md: -------------------------------------------------------------------------------- 1 | # Search in a row wise and column wise sorted matrix 2 | ### Problem Description 3 | 4 | Given a matrix of integers A of size N x M and an integer B. 5 | In the given matrix every row and column is sorted in increasing order. Find and return the position of B in the matrix in the given form: 6 | If A[i][j] = B then return (i * 1009 + j) 7 | If B is not present return -1. 8 | 9 | Note 1: Rows are numbered from top to bottom and columns are numbered from left to right. 10 | Note 2: If there are multiple B in A then return the smallest value of i*1009 +j such that A[i][j]=B. 11 | 12 | 13 | ### Problem Constraints 14 | 1 <= N, M <= 1000 15 | -100000 <= A[i] <= 100000 16 | -100000 <= B <= 100000 17 | 18 | 19 | ### Input Format 20 | The first argument given is the integer matrix A. 21 | The second argument given is the integer B. 22 | 23 | 24 | ### Output Format 25 | Return the position of B and if it is not present in A return -1 instead. 26 | 27 | 28 | ### Example Input 29 | A = [ [1, 2, 3] 30 | [4, 5, 6] 31 | [7, 8, 9] ] 32 | B = 2 33 | 34 | 35 | ### Example Output 36 | 1011 37 | 38 | 39 | ### Example Explanation 40 | A[1][2]= 2 41 | 1*1009 + 2 =1011 42 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ReversePairs.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day29_Mar2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class ReversePairs { 7 | 8 | int cnt = 0; 9 | 10 | void divide(int[] A, int l, int r) { 11 | if (l >= r) 12 | return; 13 | int m = (l + r) / 2; 14 | 15 | divide(A, l, m); 16 | divide(A, m + 1, r); 17 | conquer(A, l, m, r); 18 | } 19 | 20 | void conquer(int[] A, int l, int m, int r) { 21 | int s = l, e = m + 1; 22 | long val; 23 | 24 | while (s <= m && e <= r) { 25 | if (A[s] < A[e]) { 26 | s++; 27 | } else { 28 | val = (long) 2 * A[e]; 29 | if (A[s] > val) { 30 | cnt += m - s + 1; 31 | e++; 32 | } else 33 | s++; 34 | } 35 | } 36 | Arrays.sort(A, l, r + 1); 37 | } 38 | 39 | public int solve(int[] A) { 40 | divide(A, 0, A.length - 1); 41 | return cnt; 42 | } 43 | 44 | public static void main(String[] args) { 45 | // TODO Auto-generated method stub 46 | Scanner s = new Scanner(System.in); 47 | int n, A[]; 48 | 49 | n = s.nextInt(); 50 | A = new int[n]; 51 | for (int i = 0; i < n; i++) { 52 | A[i] = s.nextInt(); 53 | } 54 | System.out.print(new ReversePairs().solve(A)); 55 | s.close(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Tree Data Structure/LargestSubtree.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LargestSubtree { 6 | 7 | class Triplet { 8 | int min; 9 | int max; 10 | int size; 11 | 12 | Triplet(int x, int y, int z) { 13 | min = x; 14 | max = y; 15 | size = z; 16 | } 17 | } 18 | 19 | int maxSize = 1; 20 | 21 | Triplet Max_Min(TreeNode root) { 22 | 23 | if (root == null) 24 | return new Triplet(Integer.MAX_VALUE, Integer.MIN_VALUE, 0); 25 | 26 | Triplet L = Max_Min(root.left); 27 | Triplet R = Max_Min(root.right); 28 | 29 | int size; 30 | if (root.val <= L.max || root.val >= R.min || L.size == -1 || R.size == -1) { 31 | size = -1; 32 | } else { 33 | size = L.size + R.size + 1; 34 | maxSize = Math.max(maxSize, size); 35 | } 36 | 37 | return new Triplet(Math.min(root.val, L.min), Math.max(root.val, R.max), size); 38 | } 39 | 40 | public int solve(TreeNode A) { 41 | Max_Min(A); 42 | 43 | return maxSize; 44 | } 45 | 46 | public static void main(String[] args) { 47 | // TODO Auto-generated method stub 48 | Scanner s = new Scanner(System.in); 49 | System.out.println(new LargestSubtree().solve(new GetInput().getInput(s))); 50 | s.close(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/SrchRowColWiseSortedMatrix.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day07_Feb3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SrchRowColWiseSortedMatrix { 6 | 7 | public int solve(int[][] A, int B) { 8 | 9 | int n, m, i, j; 10 | n = A.length; 11 | m = A[0].length; 12 | 13 | for (i = 0; i < n; i++) { 14 | if (B <= A[i][m - 1]) { 15 | break; 16 | } 17 | } 18 | 19 | j = m - 1; 20 | 21 | while (i < n && j >= 0) { 22 | if (A[i][j] > B) 23 | j--; 24 | else if (A[i][j] < B) 25 | i++; 26 | else if (j > 0 && A[i][j - 1] == B) 27 | j--; 28 | else 29 | break; 30 | } 31 | 32 | if (i == n || j < 0) 33 | return -1; 34 | 35 | return ((i + 1) * 1009 + j + 1); 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | Scanner s = new Scanner(System.in); 41 | int n, m, A[][], B; 42 | 43 | n = s.nextInt(); 44 | m = s.nextInt(); 45 | A = new int[n][m]; 46 | for (int i = 0; i < n; i++) 47 | for (int j = 0; j < m; j++) 48 | A[i][j] = s.nextInt(); 49 | 50 | B = s.nextInt(); 51 | 52 | SrchRowColWiseSortedMatrix c = new SrchRowColWiseSortedMatrix(); 53 | System.out.println(c.solve(A, B)); 54 | s.close(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Heaps/MinLargestElement.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day70_Apr17; 2 | 3 | import java.util.Comparator; 4 | import java.util.PriorityQueue; 5 | import java.util.Scanner; 6 | 7 | public class MinLargestElement { 8 | 9 | class QueueComparator implements Comparator { 10 | public int compare(int[] o1, int[] o2) { 11 | return (o1[0] + o1[1]) - (o2[0] + o2[1]); 12 | } 13 | } 14 | 15 | public int solve(int[] A, int B) { 16 | 17 | PriorityQueue pQ = new PriorityQueue<>(new QueueComparator()); 18 | int x[]; 19 | 20 | for (int i = 0; i < A.length; i++) 21 | pQ.add(new int[] { A[i], A[i] }); 22 | 23 | for (int i = 0; i < B; i++) { 24 | x = pQ.remove(); 25 | pQ.add(new int[] { x[0] + x[1], x[1] }); 26 | } 27 | 28 | int max = pQ.remove()[0]; 29 | while (pQ.size() > 0) { 30 | max = Math.max(max, pQ.remove()[0]); 31 | } 32 | 33 | return max; 34 | } 35 | 36 | public static void main(String[] args) { 37 | // TODO Auto-generated method stub 38 | Scanner s = new Scanner(System.in); 39 | int n = s.nextInt(); 40 | int A[] = new int[n]; 41 | for (int i = 0; i < n; i++) 42 | A[i] = s.nextInt(); 43 | int B = s.nextInt(); 44 | System.out.println(new MinLargestElement().solve(A, B)); 45 | s.close(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Dynamic Programming/NDigitNumbers.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day83_May4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class NDigitNumbers { 7 | int mod = 1000000007; 8 | int[][] dp; 9 | 10 | int find(int remDigits, int remSum) { 11 | 12 | if (remDigits == 0 && remSum == 0) 13 | return 1; 14 | 15 | if (remDigits == 0 || remSum < 0) 16 | return 0; 17 | 18 | if (dp[remDigits][remSum] != -1) 19 | return dp[remDigits][remSum]; 20 | 21 | dp[remDigits][remSum] = 0; 22 | for (int i = 0; i <= 9 && i <= remSum; i++) 23 | dp[remDigits][remSum] = (dp[remDigits][remSum] + find(remDigits - 1, remSum - i)) % mod; 24 | 25 | return dp[remDigits][remSum]; 26 | } 27 | 28 | public int solve(int A, int B) { 29 | 30 | dp = new int[A + 1][B + 1]; 31 | for (int i = 0; i <= A; i++) 32 | Arrays.fill(dp[i], -1); 33 | 34 | dp[A][B] = 0; 35 | for (int i = 1; i <= 9 && i <= B; i++) 36 | dp[A][B] = (dp[A][B] + find(A - 1, B - i)) % mod; 37 | 38 | return dp[A][B]; 39 | } 40 | 41 | public static void main(String[] args) { 42 | // TODO Auto-generated method stub 43 | Scanner s = new Scanner(System.in); 44 | System.out.println(new NDigitNumbers().solve(s.nextInt(), s.nextInt())); 45 | s.close(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Tree Data Structure/DistanceBwNodes.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DistanceBwNodes { 6 | 7 | public int solve(TreeNode A, int B, int C) { 8 | int lcnt = 0, rcnt = 0; 9 | TreeNode tmp = A, troot; 10 | 11 | while (tmp != null) { 12 | if (B < tmp.val && C < tmp.val) 13 | tmp = tmp.left; 14 | else if (B > tmp.val && C > tmp.val) 15 | tmp = tmp.right; 16 | else 17 | break; 18 | } 19 | 20 | troot = tmp; 21 | while (tmp != null) { 22 | if (B < tmp.val) 23 | tmp = tmp.left; 24 | else if (B == tmp.val) 25 | break; 26 | else 27 | tmp = tmp.right; 28 | 29 | lcnt++; 30 | } 31 | 32 | tmp = troot; 33 | while (tmp != null) { 34 | if (C < tmp.val) 35 | tmp = tmp.left; 36 | else if (C == tmp.val) 37 | break; 38 | else 39 | tmp = tmp.right; 40 | 41 | rcnt++; 42 | } 43 | 44 | return lcnt + rcnt; 45 | } 46 | 47 | public static void main(String[] args) { 48 | // TODO Auto-generated method stub 49 | Scanner s = new Scanner(System.in); 50 | TreeNode A = new GetInput().getInput(s); 51 | int B = s.nextInt(); 52 | int C = s.nextInt(); 53 | System.out.println(new DistanceBwNodes().solve(A, B, C)); 54 | s.close(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Tree Data Structure/SumOfCommonNodes.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class SumOfCommonNodes { 7 | 8 | public void inorder(ArrayList ans, TreeNode A) { 9 | if (A != null) { 10 | inorder(ans, A.left); 11 | ans.add(A.val); 12 | inorder(ans, A.right); 13 | } 14 | } 15 | 16 | public int solve(TreeNode A, TreeNode B) { 17 | ArrayList X = new ArrayList<>(); 18 | ArrayList Y = new ArrayList<>(); 19 | long sum = 0; 20 | int i = 0, j = 0; 21 | 22 | inorder(X, A); 23 | inorder(Y, B); 24 | int l1 = X.size(); 25 | int l2 = Y.size(); 26 | 27 | while (i < l1 && j < l2) { 28 | if (X.get(i) < Y.get(j)) 29 | i++; 30 | else if (X.get(i) > Y.get(j)) 31 | j++; 32 | else { 33 | sum += X.get(i); 34 | i++; 35 | j++; 36 | } 37 | } 38 | 39 | return (int) (sum % 1000000007); 40 | } 41 | 42 | public static void main(String[] args) { 43 | // TODO Auto-generated method stub 44 | Scanner s = new Scanner(System.in); 45 | GetInput gi = new GetInput(); 46 | TreeNode A = gi.getInput(s); 47 | TreeNode B = gi.getInput(s); 48 | System.out.println(new SumOfCommonNodes().solve(A, B)); 49 | s.close(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Stacks & Queues/EvaluateExpression.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day46_Mar23; 2 | 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class EvaluateExpression { 7 | 8 | public int evalRPN(String[] A) { 9 | Stack S = new Stack<>(); 10 | int l = A.length; 11 | int a, b; 12 | 13 | for (int i = 0; i < l; i++) { 14 | if (A[i].equals("+") || A[i].equals("-") || A[i].equals("*") || A[i].equals("/")) { 15 | a = Integer.parseInt(S.pop()); 16 | b = Integer.parseInt(S.pop()); 17 | switch (A[i]) { 18 | case "+": 19 | a = a + b; 20 | break; 21 | case "-": 22 | a = b - a; 23 | break; 24 | case "*": 25 | a = a * b; 26 | break; 27 | case "/": 28 | a = b / a; 29 | break; 30 | } 31 | S.push("" + a); 32 | } else { 33 | S.push(A[i]); 34 | } 35 | } 36 | return Integer.parseInt(S.pop()); 37 | } 38 | 39 | public static void main(String[] args) { 40 | // TODO Auto-generated method stub 41 | Scanner s = new Scanner(System.in); 42 | int n = s.nextInt(); 43 | String A[] = new String[n]; 44 | for (int i = 0; i < n; i++) 45 | A[i] = s.next(); 46 | EvaluateExpression ee = new EvaluateExpression(); 47 | System.out.println(ee.evalRPN(A)); 48 | s.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Hashing/CountRightTriangles.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day38_Mar13; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class CountRightTriangles { 7 | 8 | int mod = 1000000007; 9 | 10 | public int solve(int[] A, int[] B) { 11 | HashMap x = new HashMap<>(); 12 | HashMap y = new HashMap<>(); 13 | 14 | long contribution = 0; 15 | 16 | for (int i = 0; i < A.length; i++) { 17 | if (x.containsKey(A[i])) 18 | x.replace(A[i], x.get(A[i]) + 1); 19 | else 20 | x.put(A[i], 1); 21 | 22 | if (y.containsKey(B[i])) 23 | y.replace(B[i], y.get(B[i]) + 1); 24 | else 25 | y.put(B[i], 1); 26 | } 27 | 28 | for (int i = 0; i < A.length; i++) 29 | contribution += ((long) (x.get(A[i]) - 1) * (y.get(B[i]) - 1)) % mod; 30 | 31 | return (int) contribution; 32 | } 33 | 34 | public static void main(String[] args) { 35 | // TODO Auto-generated method stub 36 | Scanner s = new Scanner(System.in); 37 | int n = s.nextInt(); 38 | int A[] = new int[n]; 39 | for (int i = 0; i < n; i++) 40 | A[i] = s.nextInt(); 41 | int B[] = new int[n]; 42 | for (int i = 0; i < n; i++) 43 | B[i] = s.nextInt(); 44 | System.out.println(new CountRightTriangles().solve(A, B)); 45 | s.close(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Hashing/Permutations.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day36_Mar11; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class Permutations { 7 | 8 | public int solve(String A, String B) { 9 | HashMap hm1 = new HashMap<>(); 10 | HashMap hm2 = new HashMap<>(); 11 | char c; 12 | int cnt = 0, l1 = A.length(), l2 = B.length(); 13 | 14 | for (int i = 0; i < 26; i++) { 15 | c = (char) ('a' + i); 16 | hm1.put(c, 0); 17 | hm2.put(c, 0); 18 | } 19 | 20 | for (int i = 0; i < l1; i++) { 21 | c = A.charAt(i); 22 | hm1.replace(c, hm1.get(c) + 1); 23 | c = B.charAt(i); 24 | hm2.replace(c, hm2.get(c) + 1); 25 | } 26 | 27 | if (hm1.equals(hm2)) 28 | cnt++; 29 | 30 | for (int i = l1; i < l2; i++) { 31 | c = B.charAt(i - l1); 32 | hm2.replace(c, hm2.get(c) - 1); 33 | c = B.charAt(i); 34 | hm2.replace(c, hm2.get(c) + 1); 35 | if (hm1.equals(hm2)) 36 | cnt++; 37 | } 38 | 39 | return cnt; 40 | } 41 | 42 | public static void main(String[] args) { 43 | // TODO Auto-generated method stub 44 | Scanner s = new Scanner(System.in); 45 | String A = s.next(); 46 | String B = s.next(); 47 | 48 | System.out.print(new Permutations().solve(A, B)); 49 | s.close(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Dynamic Programming/MinSumPathTriangle.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day83_May4; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class MinSumPathTriangle { 7 | 8 | public int minimumTotal(ArrayList> a) { 9 | int n = a.size(); 10 | int[][] dp = new int[n][n]; 11 | dp[0][0] = a.get(0).get(0); 12 | 13 | for (int i = 1; i < n; i++) { 14 | dp[i][0] = dp[i - 1][0] + a.get(i).get(0); 15 | dp[i][i] = dp[i - 1][i - 1] + a.get(i).get(i); 16 | } 17 | 18 | for (int i = 2; i < n; i++) 19 | for (int j = 1; j < i; j++) 20 | dp[i][j] = a.get(i).get(j) + Math.min(dp[i - 1][j], dp[i - 1][j - 1]); 21 | 22 | int min = dp[n - 1][0]; 23 | for (int j = 1; j < n; j++) 24 | min = Math.min(min, dp[n - 1][j]); 25 | 26 | return min; 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | Scanner s = new Scanner(System.in); 32 | int n = s.nextInt(); 33 | ArrayList> A = new ArrayList<>(); 34 | for (int i = 0; i < n; i++) { 35 | ArrayList tmp = new ArrayList<>(); 36 | for (int j = 0; j <= i; j++) 37 | tmp.add(s.nextInt()); 38 | A.add(tmp); 39 | } 40 | System.out.println(new MinSumPathTriangle().minimumTotal(A)); 41 | s.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Heaps/GoodRanges.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day66_Apr13; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.Scanner; 6 | 7 | public class GoodRanges { 8 | 9 | public int[] solve(int A, int[] B) { 10 | int lastMin = B[0], lastMax = B[0]; 11 | long sum = A + 1; 12 | int mod = 1000000007; 13 | HashSet hs = new HashSet<>(); 14 | hs.add(B[0]); 15 | 16 | int ans[] = new int[B.length]; 17 | ans[0] = A + 1; 18 | 19 | for (int i = 1; i < B.length; i++) { 20 | if (!hs.contains(B[i])) { 21 | hs.add(B[i]); 22 | if (B[i] > lastMax) { 23 | sum = (sum + lastMax + B[i]) % mod; 24 | lastMax = B[i]; 25 | } else if (B[i] < lastMin) { 26 | sum = (sum + lastMin + B[i]) % mod; 27 | lastMin = B[i]; 28 | } else 29 | sum = (sum + 2 * B[i]) % mod; 30 | } 31 | ans[i] = (int) sum; 32 | } 33 | 34 | return ans; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n, A[]; 41 | 42 | n = s.nextInt(); 43 | A = new int[n]; 44 | for (int i = 0; i < n; i++) { 45 | A[i] = s.nextInt(); 46 | } 47 | System.out.print(Arrays.toString(new GoodRanges().solve(s.nextInt(), A))); 48 | s.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/Carotenemia.md: -------------------------------------------------------------------------------- 1 | # Carotenemia 2 | You are given an Array of boxes A, where each box consists of oranges. You really love oranges, and you want to eat atleast B oranges. You start from the 0th index of the array, and keep eating oranges until you eat B oranges. If oranges from a box at ith index get depleted, you start eating from the (i+1)th box. 3 | 4 | Determine index of the box where you finish eating B number of oranges. If you don't eat B oranges even after eating from all the boxes, return -1. 5 | 6 | ### Input: 7 | 8 | A: Array of integers. 9 | B: Integer 10 | ### Output: 11 | 12 | Integer 13 | ### Constraints: 14 | 15 | 1 <= size(A) <= 10^5 16 | 1 <= Integers in A <= 10^4 17 | 1 <= B <= 10^9 18 | ### Example: 19 | 20 | ### Input 21 | 22 | A: [1, 3, 2, 4] 23 | B: 5 24 | ### Output 25 | 26 | 2 27 | 28 | You eat 1 orange from box 0. 29 | You eat 3 oranges from box 1, hence a total of 4 oranges. 30 | You eat 1 orange from box 2, hence a total of 5 oranges, and you stop. 31 | ### Input 32 | 33 | A: [1, 1, 1, 4] 34 | B: 10 35 | ### Output 36 | 37 | -1 38 | 39 | You eat 1 orange from box 0. 40 | You eat 1 orange from box 1, hence a total of 2 oranges. 41 | You eat 1 orange from box 2, hence a total of 3 oranges. 42 | You eat 4 oranges from box 3, hence a total of 7 oranges. 43 | As you never complete 10 oranges, return -1. 44 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/MinOperationMatrixEqual.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day07_Feb3; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class MinOperationMatrixEqual { 7 | 8 | public int solve(int[][] A, int B) { 9 | int n, m, mod, X[], median, sum = 0; 10 | n = A.length; 11 | m = A[0].length; 12 | X = new int[m * n]; 13 | mod = Math.floorMod(A[0][0], B); 14 | int x = 0; 15 | 16 | for (int i = 0; i < n; i++) { 17 | for (int j = 0; j < m; j++) { 18 | if (Math.floorMod(A[i][j], B) != mod) 19 | return -1; 20 | X[x++] = A[i][j]; 21 | } 22 | } 23 | Arrays.sort(X); 24 | median = X[x / 2]; 25 | for (int i = 0; i < x; i++) { 26 | sum += Math.abs(X[i] - median) / B; 27 | } 28 | return sum; 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | int b, A[][], x, y; 35 | 36 | x = s.nextInt(); 37 | y = s.nextInt(); 38 | A = new int[x][y]; 39 | 40 | for (int i = 0; i < x; i++) { 41 | for (int j = 0; j < y; j++) { 42 | A[i][j] = s.nextInt(); 43 | } 44 | } 45 | b = s.nextInt(); 46 | 47 | MinOperationMatrixEqual c = new MinOperationMatrixEqual(); 48 | System.out.println(c.solve(A, b)); 49 | 50 | s.close(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Backtracking/KthPermutationSequence.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day25_Feb26; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class KthPermutationSequence { 7 | 8 | int cnt = 0; 9 | int freq[]; 10 | String ans = ""; 11 | 12 | void solve(ArrayList curr, int A, int B) { 13 | 14 | if (curr.size() == A) { 15 | cnt++; 16 | if (cnt == B) { 17 | while (!curr.isEmpty()) { 18 | ans += String.valueOf(curr.get(0)); 19 | curr.remove(0); 20 | } 21 | } 22 | return; 23 | } 24 | 25 | for (int i = 0; i < A; i++) { 26 | if (freq[i] == 0) { 27 | curr.add(i + 1); 28 | freq[i] = 1; 29 | solve(curr, A, B); 30 | if (cnt == B) 31 | return; 32 | freq[i] = 0; 33 | curr.remove(curr.size() - 1); 34 | } 35 | } 36 | } 37 | 38 | public String getPermutation(int A, int B) { 39 | 40 | ArrayList curr = new ArrayList<>(); 41 | freq = new int[A]; 42 | solve(curr, A, B); 43 | 44 | return ans; 45 | } 46 | 47 | public static void main(String[] args) { 48 | // TODO Auto-generated method stub 49 | Scanner s = new Scanner(System.in); 50 | int A = s.nextInt(); 51 | int B = s.nextInt(); 52 | System.out.println(new KthPermutationSequence().getPermutation(A, B)); 53 | s.close(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Strings/AddBinary.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day42_Mar18; 2 | 3 | import java.util.Scanner; 4 | 5 | public class AddBinary { 6 | 7 | public String addBinary(String A, String B) { 8 | String ans = ""; 9 | int carry = 0, x, y; 10 | int l1 = A.length(); 11 | int l2 = B.length(); 12 | 13 | x = l1 - 1; 14 | y = l2 - 1; 15 | 16 | while (x >= 0 && y >= 0) { 17 | int sum = Integer.parseInt("" + A.charAt(x)) + Integer.parseInt("" + B.charAt(y)) + carry; 18 | carry = (sum >= 2) ? 1 : 0; 19 | ans = (sum % 2) + ans; 20 | x--; 21 | y--; 22 | } 23 | while (y >= 0) { 24 | int sum = Integer.parseInt("" + B.charAt(y)) + carry; 25 | carry = (sum >= 2) ? 1 : 0; 26 | ans = (sum % 2) + ans; 27 | y--; 28 | } 29 | while (x >= 0) { 30 | int sum = Integer.parseInt("" + A.charAt(x)) + carry; 31 | carry = (sum >= 2) ? 1 : 0; 32 | ans = (sum % 2) + ans; 33 | x--; 34 | } 35 | if (carry == 1) { 36 | ans = carry + ans; 37 | } 38 | 39 | return ans; 40 | } 41 | 42 | public static void main(String[] args) { 43 | // TODO Auto-generated method stub 44 | Scanner s = new Scanner(System.in); 45 | String A = s.next(); 46 | String B = s.next(); 47 | AddBinary ab = new AddBinary(); 48 | System.out.print(ab.addBinary(A, B)); 49 | s.close(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/A_B.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day35_Mar8; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class A_B { 7 | 8 | public int solve(int[] A, int B, int C) { 9 | 10 | int freq, max = 1; 11 | HashMap hm = new HashMap<>(); 12 | int temp; 13 | 14 | for (int i = 0; i < A.length; i++) { 15 | temp = (int) Math.floorMod((long) A[i] * (Math.floorMod((long) A[i] * A[i], C) - B), C); 16 | if (hm.containsKey(temp)) { 17 | freq = hm.get(temp); 18 | freq++; 19 | max = Math.max(max, freq); 20 | hm.replace(temp, freq); 21 | } else 22 | hm.put(temp, 1); 23 | } 24 | 25 | return max; 26 | } 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | Scanner s = new Scanner(System.in); 31 | int n = s.nextInt(); 32 | int A[] = new int[n]; 33 | for (int i = 0; i < n; i++) 34 | A[i] = s.nextInt(); 35 | int B = s.nextInt(); 36 | int C = s.nextInt(); 37 | A_B ab = new A_B(); 38 | System.out.print(ab.solve(A, B, C)); 39 | s.close(); 40 | } 41 | 42 | } 43 | 44 | // A: 26, 31, 15, 38, 27, 7, 45, 10, 44, 52, 28, 33, 25, 41, 39, 29, 30, 42 45 | // B: 9 46 | // C: 53 47 | // OP: 2 48 | 49 | //A: 16, 22, 27, 14, 33, 15, 25, 31, 23 50 | //B: 10 51 | //C: 139 52 | //OP: 1 53 | -------------------------------------------------------------------------------- /Two Pointers/CountPairsGivenSum2.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day33_Mar6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CountPairsGivenSum2 { 6 | 7 | public int solve(int[] A, int B) { 8 | int i = 0, j = A.length - 1, sum, cnt1, cnt2; 9 | long n, cnt = 0; 10 | while (i < j) { 11 | sum = A[i] + A[j]; 12 | if (sum == B) { 13 | if (A[i] == A[j]) { 14 | n = j - i + 1; 15 | cnt += (n * (n - 1)) / 2; 16 | break; 17 | } else { 18 | cnt1 = 1; 19 | cnt2 = 1; 20 | while (A[i + 1] == A[i] && i + 1 < j) { 21 | i++; 22 | cnt1++; 23 | } 24 | while (A[j - 1] == A[j] && i < j - 1) { 25 | j--; 26 | cnt2++; 27 | } 28 | cnt += cnt1 * cnt2; 29 | i++; 30 | } 31 | } else if (sum > B) 32 | j--; 33 | else 34 | i++; 35 | } 36 | return (int) (cnt % 1000000007); 37 | } 38 | 39 | public static void main(String[] args) { 40 | // TODO Auto-generated method stub 41 | Scanner s = new Scanner(System.in); 42 | int n, A[]; 43 | 44 | n = s.nextInt(); 45 | A = new int[n]; 46 | for (int i = 0; i < n; i++) { 47 | A[i] = s.nextInt(); 48 | } 49 | int b = s.nextInt(); 50 | CountPairsGivenSum2 cp = new CountPairsGivenSum2(); 51 | System.out.print(cp.solve(A, b)); 52 | s.close(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Strings/KthCharacter.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day46_Mar23; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | import java.util.Stack; 6 | 7 | public class KthCharacter { 8 | 9 | public String solve(String A, int B) { 10 | Stack S = new Stack<>(); 11 | HashMap hm = new HashMap<>(); 12 | 13 | long n, l = 0; 14 | char c; 15 | for (int i = 0; i < A.length(); i++) { 16 | c = A.charAt(i); 17 | if (c >= 'a' && c <= 'z') { 18 | l++; 19 | S.push(l); 20 | hm.put(l, c); 21 | } else { 22 | n = 0; 23 | while (c >= '0' && c <= '9') { 24 | n = n * 10 + (c - '0'); 25 | i++; 26 | if (i >= A.length()) 27 | break; 28 | c = A.charAt(i); 29 | } 30 | i--; 31 | l *= n; 32 | } 33 | } 34 | l = B; 35 | while (l > 0) { 36 | while (l < S.peek()) { 37 | S.pop(); 38 | } 39 | n = S.pop(); 40 | l = l % n; 41 | if (l == 0) 42 | return "" + hm.get(n); 43 | } 44 | return ""; 45 | } 46 | 47 | public static void main(String[] args) { 48 | // TODO Auto-generated method stub 49 | Scanner s = new Scanner(System.in); 50 | String A = s.next(); 51 | int B = s.nextInt(); 52 | 53 | KthCharacter kc = new KthCharacter(); 54 | System.out.print(kc.solve(A, B)); 55 | s.close(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Strings/RomanToInteger.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day40_Mar16; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class RomanToInteger { 7 | 8 | public int romanToInt(String A) { 9 | String s1, s2; 10 | int val = 0; 11 | int l = A.length(); 12 | 13 | HashMap H = new HashMap<>(); 14 | H.put("I", 1); 15 | H.put("IV", 4); 16 | H.put("V", 5); 17 | H.put("IX", 9); 18 | H.put("X", 10); 19 | H.put("XL", 40); 20 | H.put("L", 50); 21 | H.put("XC", 90); 22 | H.put("C", 100); 23 | H.put("CD", 400); 24 | H.put("D", 500); 25 | H.put("CM", 900); 26 | H.put("M", 1000); 27 | 28 | int i; 29 | for (i = 0; i < l - 1; i++) { 30 | s1 = "" + A.charAt(i); 31 | s2 = "" + A.charAt(i + 1); 32 | 33 | if (H.get(s1) < H.get(s2)) { 34 | val += H.get(s1 + s2); 35 | i++; 36 | } else { 37 | val += H.get(s1); 38 | } 39 | } 40 | if (i == l - 1) { 41 | val += H.get("" + A.charAt(i)); 42 | } 43 | 44 | return val; 45 | } 46 | 47 | public static void main(String[] args) { 48 | // TODO Auto-generated method stub 49 | Scanner s = new Scanner(System.in); 50 | String A; 51 | 52 | A = s.nextLine(); 53 | RomanToInteger rti = new RomanToInteger(); 54 | System.out.print(rti.romanToInt(A)); 55 | s.close(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Tree Data Structure/RecoverBST.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day59_Apr6; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | import java.util.Stack; 6 | 7 | public class RecoverBST { 8 | 9 | public int[] recoverTree(TreeNode A) { 10 | int ans[] = new int[2]; 11 | ArrayList X = new ArrayList<>(); 12 | TreeNode tmp, curr; 13 | Stack S = new Stack<>(); 14 | curr = A; 15 | 16 | while (curr != null || !S.isEmpty()) { 17 | if (curr != null) { 18 | S.push(curr); 19 | curr = curr.left; 20 | } else { 21 | tmp = S.pop(); 22 | curr = tmp.right; 23 | X.add(tmp.val); 24 | } 25 | } 26 | 27 | int i = 1, l = X.size(); 28 | while (i < l) { 29 | if (X.get(i) < X.get(i - 1)) { 30 | ans[0] = X.get(i); 31 | ans[1] = X.get(i - 1); 32 | i++; 33 | break; 34 | } 35 | i++; 36 | } 37 | while (i < l) { 38 | if (X.get(i) < X.get(i - 1)) { 39 | ans[0] = X.get(i); 40 | break; 41 | } 42 | i++; 43 | } 44 | 45 | return ans; 46 | } 47 | 48 | public static void main(String[] args) { 49 | // TODO Auto-generated method stub 50 | Scanner s = new Scanner(System.in); 51 | int A[] = new RecoverBST().recoverTree(new GetInput().getInput(s)); 52 | System.out.println(A[0] + " " + A[1]); 53 | s.close(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Two Pointers/Array3Pointers.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day33_Mar6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Array3Pointers { 6 | 7 | public int minimize(final int[] A, final int[] B, final int[] C) { 8 | int min = Integer.MAX_VALUE, x = 0, y = 0, z = 0, max; 9 | 10 | while (x < A.length && y < B.length && z < C.length) { 11 | max = Math.max(Math.abs(A[x] - B[y]), Math.max(Math.abs(B[y] - C[z]), Math.abs(A[x] - C[z]))); 12 | min = Math.min(min, max); 13 | 14 | if (A[x] <= B[y] && A[x] <= C[z]) 15 | x++; 16 | 17 | else if (B[y] <= A[x] && B[y] <= C[z]) 18 | y++; 19 | 20 | else if (C[z] <= B[y] && C[z] <= A[x]) 21 | z++; 22 | } 23 | 24 | return min; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | Scanner s = new Scanner(System.in); 30 | int n, A[], B[], C[]; 31 | 32 | n = s.nextInt(); 33 | A = new int[n]; 34 | for (int i = 0; i < n; i++) { 35 | A[i] = s.nextInt(); 36 | } 37 | n = s.nextInt(); 38 | B = new int[n]; 39 | for (int i = 0; i < n; i++) { 40 | B[i] = s.nextInt(); 41 | } 42 | n = s.nextInt(); 43 | C = new int[n]; 44 | for (int i = 0; i < n; i++) { 45 | C[i] = s.nextInt(); 46 | } 47 | System.out.print(new Array3Pointers().minimize(A, B, C)); 48 | s.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/CoPrimeAgain.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day11_Feb7; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CoPrimeAgain { 6 | 7 | public int gcd(int A, int B) { 8 | if (A == 1 || B == 1) 9 | return 1; 10 | if (A == 0 || B == 0) 11 | return Math.max(A, B); 12 | 13 | if (A > B) 14 | return gcd(A % B, B); 15 | if (A < B) 16 | return gcd(A, B % A); 17 | 18 | if (A == B) 19 | return A; 20 | 21 | return -1; 22 | } 23 | 24 | public int solve(int[] A, int[] B, int C) { 25 | int c1 = 0, c2 = 0; 26 | 27 | for (int i = 0; i < A.length; i++) 28 | if (gcd(A[i], C) == 1) 29 | c1++; 30 | 31 | for (int i = 0; i < B.length; i++) 32 | if (gcd(B[i], C) == 1) 33 | c2++; 34 | 35 | return (A.length * B.length - c1 * c2); 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | Scanner s = new Scanner(System.in); 41 | 42 | int n, m, k; 43 | 44 | n = s.nextInt(); 45 | m = s.nextInt(); 46 | k = s.nextInt(); 47 | int A[] = new int[n]; 48 | int B[] = new int[m]; 49 | 50 | for (int i = 0; i < n; i++) 51 | A[i] = s.nextInt(); 52 | 53 | for (int i = 0; i < m; i++) 54 | B[i] = s.nextInt(); 55 | 56 | CoPrimeAgain cp = new CoPrimeAgain(); 57 | System.out.println(cp.solve(A, B, k)); 58 | s.close(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Two Pointers/SubArraySum.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day33_Mar6; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class SubArraySum { 7 | 8 | public int[] solve(int[] A, int B) { 9 | long sum = A[0]; 10 | int i = 0, j = 1; 11 | int ans[] = { -1 }; 12 | 13 | while (i < A.length && j < A.length) { 14 | if (i == j) { 15 | sum += A[i]; 16 | j = (i == A.length - 1) ? i : i + 1; 17 | } 18 | if (sum == B) { 19 | ans = Arrays.copyOfRange(A, i, j); 20 | break; 21 | } else if (sum < B) { 22 | sum += A[j++]; 23 | } else { 24 | sum -= A[i++]; 25 | } 26 | } 27 | 28 | while (ans[0] == -1 && i < j) { 29 | if (sum == B) { 30 | ans = Arrays.copyOfRange(A, i, j); 31 | break; 32 | } else { 33 | sum -= A[i++]; 34 | } 35 | } 36 | 37 | return ans; 38 | } 39 | 40 | public static void main(String[] args) { 41 | // TODO Auto-generated method stub 42 | Scanner s = new Scanner(System.in); 43 | int n, A[]; 44 | 45 | n = s.nextInt(); 46 | A = new int[n]; 47 | for (int i = 0; i < n; i++) { 48 | A[i] = s.nextInt(); 49 | } 50 | int b = s.nextInt(); 51 | SubArraySum sas = new SubArraySum(); 52 | A = sas.solve(A, b); 53 | for (int i = 0; i < A.length; i++) { 54 | System.out.print(A[i] + " "); 55 | } 56 | s.close(); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Binary Search/FindSmallestAgain.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day24_Feb25; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class FindSmallestAgain { 7 | 8 | public int calcNcR(int A, int B) { 9 | int min = (B < A - B) ? B : A - B; 10 | long prod = 1; 11 | 12 | for (int i = A; i > A - min; i--) 13 | prod *= i; 14 | 15 | for (int i = min; i > 1; i--) 16 | prod /= i; 17 | 18 | return (int) prod; 19 | } 20 | 21 | public int solve(int[] A, int B) { 22 | 23 | int l = A.length; 24 | if (l == 3) 25 | return A[0] + A[1] + A[2]; 26 | 27 | int n = calcNcR(l, 3); 28 | int C[] = new int[n]; 29 | int x = 0; 30 | 31 | for (int i = 0; i < l - 2; i++) { 32 | for (int j = i + 1; j < l - 1; j++) { 33 | for (int k = j + 1; k < l; k++) { 34 | C[x++] = A[i] + A[j] + A[k]; 35 | } 36 | } 37 | } 38 | 39 | Arrays.sort(C); 40 | 41 | return C[B - 1]; 42 | } 43 | 44 | public static void main(String[] args) { 45 | // TODO Auto-generated method stub 46 | Scanner s = new Scanner(System.in); 47 | int n, A[]; 48 | 49 | n = s.nextInt(); 50 | A = new int[n]; 51 | for (int i = 0; i < n; i++) { 52 | A[i] = s.nextInt(); 53 | } 54 | int b = s.nextInt(); 55 | FindSmallestAgain fsa = new FindSmallestAgain(); 56 | System.out.print(fsa.solve(A, b)); 57 | s.close(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Heaps/RunningMedian.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day70_Apr17; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.PriorityQueue; 6 | import java.util.Scanner; 7 | 8 | public class RunningMedian { 9 | 10 | public int[] solve(int[] A) { 11 | PriorityQueue maxH = new PriorityQueue<>(Collections.reverseOrder()); 12 | PriorityQueue minH = new PriorityQueue<>(); 13 | int ans[] = new int[A.length]; 14 | int lcnt = 1, rcnt = 0; 15 | 16 | maxH.add(A[0]); 17 | ans[0] = A[0]; 18 | 19 | for (int i = 1; i < A.length; i++) { 20 | if (A[i] <= maxH.peek()) { 21 | maxH.add(A[i]); 22 | lcnt++; 23 | } else { 24 | minH.add(A[i]); 25 | rcnt++; 26 | } 27 | 28 | if (lcnt < rcnt) { 29 | maxH.add(minH.remove()); 30 | rcnt--; 31 | lcnt++; 32 | } else if (lcnt - rcnt > 1) { 33 | minH.add(maxH.remove()); 34 | lcnt--; 35 | rcnt++; 36 | } 37 | ans[i] = maxH.peek(); 38 | } 39 | return ans; 40 | } 41 | 42 | public static void main(String[] args) { 43 | // TODO Auto-generated method stub 44 | Scanner s = new Scanner(System.in); 45 | int n = s.nextInt(); 46 | int A[] = new int[n]; 47 | for (int i = 0; i < n; i++) 48 | A[i] = s.nextInt(); 49 | System.out.println(Arrays.toString(new RunningMedian().solve(A))); 50 | s.close(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Binary Search/AggressiveCows.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day21_Feb19; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class AggressiveCows { 7 | 8 | public boolean check(int[] A, int B, int m) { 9 | int c = 1; 10 | int x = 0; 11 | 12 | for (int i = 1; i < A.length; i++) { 13 | if (A[i] - A[x] >= m) { 14 | c++; 15 | x = i; 16 | if (c >= B) 17 | return true; 18 | } 19 | } 20 | 21 | return false; 22 | } 23 | 24 | public int solve(int[] A, int B) { 25 | Arrays.sort(A); 26 | int l = A.length; 27 | int s, e, m, ans = 1; 28 | 29 | s = 1; 30 | e = A[l - 1]; 31 | m = (s + e) / 2; 32 | while (s <= e) { 33 | // Check for m, if returns true discard left else discard right 34 | if (check(A, B, m) == true) { 35 | s = m + 1; 36 | ans = m; 37 | } else { 38 | e = m - 1; 39 | } 40 | m = (s + e) / 2; 41 | } 42 | 43 | return ans; 44 | } 45 | 46 | public static void main(String[] args) { 47 | // TODO Auto-generated method stub 48 | Scanner s = new Scanner(System.in); 49 | int n = s.nextInt(); 50 | int A[] = new int[n]; 51 | 52 | for (int i = 0; i < n; i++) 53 | A[i] = s.nextInt(); 54 | 55 | int b = s.nextInt(); 56 | 57 | AggressiveCows ac = new AggressiveCows(); 58 | System.out.println(ac.solve(A, b)); 59 | s.close(); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Dynamic Programming/PallindromePartitioning2.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day80_Apr29; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PallindromePartitioning2 { 6 | 7 | public int minCut(String A) { 8 | 9 | int n = A.length(); 10 | int isPallindrome[][] = new int[n][n]; 11 | 12 | for (int x = 0; x < n; x++) { 13 | for (int i = 0; i + x < n; i++) { 14 | int j = i + x; 15 | 16 | if (i == j) 17 | isPallindrome[i][j] = 1; 18 | 19 | else if (j == i + 1) { 20 | isPallindrome[i][j] = A.charAt(i) == A.charAt(j) ? 1 : 0; 21 | 22 | } else 23 | isPallindrome[i][j] = (isPallindrome[i + 1][j - 1] == 1 && A.charAt(i) == A.charAt(j)) ? 1 : 0; 24 | } 25 | } 26 | 27 | int minCuts[] = new int[n]; 28 | 29 | for (int r = 0; r < n; r++) { 30 | if (isPallindrome[0][r] == 1) 31 | minCuts[r] = 0; 32 | else { 33 | minCuts[r] = Integer.MAX_VALUE; 34 | for (int l = 0; l < r; l++) { 35 | if (isPallindrome[l + 1][r] == 1) { 36 | minCuts[r] = Math.min(minCuts[r], 1 + minCuts[l]); 37 | } 38 | } 39 | } 40 | } 41 | 42 | return minCuts[n - 1]; 43 | } 44 | 45 | public static void main(String[] args) { 46 | // TODO Auto-generated method stub 47 | Scanner s = new Scanner(System.in); 48 | System.out.println(new PallindromePartitioning2().minCut(s.next())); 49 | s.close(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Tree Data Structure/ZigZagLevelTraversal.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day54_Apr1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Stack; 5 | 6 | public class ZigZagLevelTraversal { 7 | 8 | public ArrayList> zigzagLevelOrder(TreeNode A) { 9 | ArrayList> ans = new ArrayList<>(); 10 | int lvl = 0; 11 | Stack l2r = new Stack<>(); 12 | Stack r2l = new Stack<>(); 13 | TreeNode tmp; 14 | ArrayList a = new ArrayList<>(); 15 | l2r.push(A); 16 | 17 | while (!l2r.isEmpty() || !r2l.isEmpty()) { 18 | if (lvl % 2 == 0) { 19 | while (!l2r.isEmpty()) { 20 | tmp = l2r.pop(); 21 | a.add(tmp.val); 22 | if (tmp.left != null) 23 | r2l.push(tmp.left); 24 | if (tmp.right != null) 25 | r2l.push(tmp.right); 26 | } 27 | } else { 28 | while (!r2l.isEmpty()) { 29 | tmp = r2l.pop(); 30 | a.add(tmp.val); 31 | if (tmp.right != null) 32 | l2r.push(tmp.right); 33 | if (tmp.left != null) 34 | l2r.push(tmp.left); 35 | } 36 | } 37 | ans.add(a); 38 | a = new ArrayList<>(); 39 | lvl++; 40 | } 41 | 42 | return ans; 43 | } 44 | 45 | public static void main(String[] args) { 46 | // TODO Auto-generated method stub 47 | System.out.println(new ZigZagLevelTraversal().zigzagLevelOrder(new GetInput().getInput())); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/DeleteOne.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day09_Feb5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DeleteOne { 6 | 7 | public int gcd(int A, int B) { 8 | if (A == 1 || B == 1) 9 | return 1; 10 | if (A == 0 || B == 0) 11 | return Math.max(A, B); 12 | 13 | if (A > B) 14 | return gcd(A % B, B); 15 | if (A < B) 16 | return gcd(A, B % A); 17 | 18 | if (A == B) 19 | return A; 20 | 21 | return -1; 22 | } 23 | 24 | public int solve(int[] A) { 25 | int l = A.length; 26 | int pr[] = new int[l]; 27 | int sf[] = new int[l]; 28 | 29 | pr[0] = A[0]; 30 | sf[l - 1] = A[l - 1]; 31 | 32 | for (int i = 1; i < l; i++) { 33 | pr[i] = gcd(pr[i - 1], A[i]); 34 | } 35 | 36 | for (int i = l - 2; i >= 0; i--) { 37 | sf[i] = gcd(sf[i + 1], A[i]); 38 | } 39 | 40 | int max = 1; 41 | 42 | for (int i = 1; i < l - 1; i++) { 43 | max = Math.max(max, gcd(pr[i - 1], sf[i + 1])); 44 | } 45 | return max; 46 | } 47 | 48 | public static void main(String[] args) { 49 | // TODO Auto-generated method stub 50 | Scanner s = new Scanner(System.in); 51 | int n, A[]; 52 | 53 | n = s.nextInt(); 54 | A = new int[n]; 55 | for (int i = 0; i < n; i++) 56 | A[i] = s.nextInt(); 57 | 58 | DeleteOne c = new DeleteOne(); 59 | System.out.println(c.solve(A)); 60 | 61 | s.close(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Linked List/ReverseLinkedList.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day50_Mar27; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReverseLinkedList { 6 | 7 | public ListNode reverseBetween(ListNode A, int B, int C) { 8 | ListNode tmp = A, st, pre, nxt, curr; 9 | 10 | while (B > 2) { 11 | tmp = tmp.next; 12 | B--; 13 | C--; 14 | } 15 | st = tmp; 16 | while (C-- > 1) 17 | tmp = tmp.next; 18 | 19 | if (B == 1) 20 | curr = st; 21 | else { 22 | curr = st.next; 23 | st.next = tmp; 24 | } 25 | pre = tmp.next; 26 | 27 | while (curr != tmp) { 28 | nxt = curr.next; 29 | curr.next = pre; 30 | pre = curr; 31 | curr = nxt; 32 | } 33 | curr.next = pre; 34 | 35 | return (B == 1) ? tmp : A; 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | Scanner s = new Scanner(System.in); 41 | int n = s.nextInt(); 42 | ReverseLinkedList rll = new ReverseLinkedList(); 43 | ListNode A = new ListNode(s.nextInt()); 44 | ListNode tail = A; 45 | while (n-- > 1) { 46 | ListNode tmp = new ListNode(s.nextInt()); 47 | tail.next = tmp; 48 | tail = tmp; 49 | } 50 | int B = s.nextInt(); 51 | int C = s.nextInt(); 52 | A = rll.reverseBetween(A, B, C); 53 | while (A != null) { 54 | System.out.print(A.val + " "); 55 | A = A.next; 56 | } 57 | s.close(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/CountOfRangeSum.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day47_Mar24; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class CountOfRangeSum { 7 | 8 | int prefix[]; 9 | 10 | int mergeSort(int B, int C, int low, int high) { 11 | 12 | if (high - low <= 1) 13 | return 0; 14 | int mid = (low + high) / 2, m = mid, n = mid, count = 0; 15 | 16 | count = mergeSort(B, C, low, mid) + mergeSort(B, C, mid, high); 17 | for (int i = low; i < mid; i++) { 18 | while (m < high && prefix[m] - prefix[i] < B) 19 | m++; 20 | while (n < high && prefix[n] - prefix[i] <= C) 21 | n++; 22 | count += n - m; 23 | } 24 | Arrays.sort(prefix, low, high); 25 | return count; 26 | } 27 | 28 | public int solve(int[] A, int B, int C) { 29 | prefix = new int[A.length + 1]; 30 | 31 | for (int i = 0; i < A.length; i++) 32 | prefix[i + 1] = prefix[i] + A[i]; 33 | 34 | return mergeSort(B, C, 0, A.length + 1); 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n = s.nextInt(); 41 | 42 | int A[] = new int[n]; 43 | for (int i = 0; i < n; i++) 44 | A[i] = s.nextInt(); 45 | 46 | int B = s.nextInt(); 47 | int C = s.nextInt(); 48 | 49 | System.out.println(new CountOfRangeSum().solve(A, B, C)); 50 | s.close(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Heaps/ProductOf3.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day68_Apr15; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class ProductOf3 { 7 | 8 | void heapify(int[] A) { 9 | int tmp; 10 | 11 | if (A[0] > A[1]) { 12 | tmp = A[0]; 13 | A[0] = A[1]; 14 | A[1] = tmp; 15 | } 16 | if (A[0] > A[2]) { 17 | tmp = A[0]; 18 | A[0] = A[2]; 19 | A[2] = tmp; 20 | } 21 | } 22 | 23 | public int[] solve(int[] A) { 24 | int mH[] = new int[3]; 25 | int ans[] = new int[A.length]; 26 | int prod = 1; 27 | 28 | if (A.length < 3) { 29 | Arrays.fill(ans, -1); 30 | return ans; 31 | } 32 | 33 | for (int i = 0; i < 3; i++) { 34 | mH[i] = A[i]; 35 | prod *= A[i]; 36 | ans[i] = -1; 37 | } 38 | ans[2] = prod; 39 | heapify(mH); 40 | 41 | for (int i = 3; i < A.length; i++) { 42 | if (mH[0] < A[i]) { 43 | prod = (prod / mH[0]) * A[i]; 44 | mH[0] = A[i]; 45 | heapify(mH); 46 | } 47 | ans[i] = prod; 48 | } 49 | 50 | return ans; 51 | } 52 | 53 | public static void main(String[] args) { 54 | // TODO Auto-generated method stub 55 | Scanner s = new Scanner(System.in); 56 | int n = s.nextInt(); 57 | int A[] = new int[n]; 58 | for (int i = 0; i < n; i++) 59 | A[i] = s.nextInt(); 60 | System.out.println(Arrays.toString(new ProductOf3().solve(A))); 61 | 62 | s.close(); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Dynamic Programming/DungeonPrincess.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day80_Apr29; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DungeonPrincess { 6 | 7 | public int calculateMinimumHP(int[][] A) { 8 | 9 | int m = A.length; 10 | int n = A[0].length; 11 | 12 | int[][] dp = new int[m][n]; 13 | dp[m - 1][n - 1] = A[m - 1][n - 1] < 0 ? 1 - A[m - 1][n - 1] : 1; 14 | 15 | for (int i = m - 2; i >= 0; i--) { 16 | dp[i][n - 1] = dp[i + 1][n - 1] - A[i][n - 1]; 17 | dp[i][n - 1] = dp[i][n - 1] <= 0 ? 1 : dp[i][n - 1]; 18 | } 19 | 20 | for (int j = n - 2; j >= 0; j--) { 21 | dp[m - 1][j] = dp[m - 1][j + 1] - A[m - 1][j]; 22 | dp[m - 1][j] = dp[m - 1][j] <= 0 ? 1 : dp[m - 1][j]; 23 | } 24 | 25 | for (int i = m - 2; i >= 0; i--) { 26 | for (int j = n - 2; j >= 0; j--) { 27 | dp[i][j] = Math.min(dp[i + 1][j], dp[i][j + 1]) - A[i][j]; 28 | dp[i][j] = dp[i][j] <= 0 ? 1 : dp[i][j]; 29 | } 30 | } 31 | 32 | return dp[0][0]; 33 | } 34 | 35 | public static void main(String[] args) { 36 | // TODO Auto-generated method stub 37 | Scanner s = new Scanner(System.in); 38 | int m = s.nextInt(); 39 | int n = s.nextInt(); 40 | int[][] A = new int[m][n]; 41 | for (int i = 0; i < m; i++) 42 | for (int j = 0; j < n; j++) 43 | A[i][j] = s.nextInt(); 44 | System.out.println(new DungeonPrincess().calculateMinimumHP(A)); 45 | s.close(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Arrays & Math/Problems/MinOperationMatrixEqual.md: -------------------------------------------------------------------------------- 1 | # Minimum operations of given type to make all elements of a matrix equal 2 | ### Problem Description 3 | 4 | Given a matrix of integers A of size N x M and an integer B. 5 | 6 | In a single operation, B can be added to or subtracted from any element of the matrix. 7 | 8 | Find and return the minimum number of operations required to make all the elements of the matrix equal and if it impossible return -1 instead. 9 | 10 | NOTE: Rows are numbered from top to bottom and columns are numbered from left to right. 11 | 12 | 13 | 14 | ### Problem Constraints 15 | 1 <= N, M <= 1000 16 | -1000 <= A[i] <= 1000 17 | 1 <= B <= 1000 18 | 19 | 20 | 21 | ### Input Format 22 | The first argument given is the integer matrix A. The second argument given is the integer B. 23 | 24 | 25 | 26 | ### Output Format 27 | Return the minimum number of operations required to make all the elements of the matrix equal and if it impossible return -1 instead. 28 | 29 | 30 | 31 | ### Example Input 32 | A = [ 33 | [0, 2, 8] 34 | [8, 2, 0] 35 | [0, 2, 8] 36 | ] 37 | B = 2 38 | 39 | 40 | ### Example Output 41 | 12 42 | 43 | 44 | ### Example Explanation 45 | We can make all value equal to 2 by adding 2 one time to 0's and subtracting 2 three times from 8's. 46 | So there are all total 12 operations needed to be done. 47 | -------------------------------------------------------------------------------- /Arrays & Math/Solutions/ChristmasTree.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day47_Mar24; 2 | 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class ChristmasTree { 7 | 8 | public int solve(int[] A, int[] B) { 9 | HashMap hm = new HashMap<>(); 10 | long min; 11 | int val; 12 | 13 | for (int i = 0; i < A.length - 1; i++) { 14 | min = Integer.MAX_VALUE; 15 | 16 | for (int j = i + 1; j < A.length; j++) { 17 | if (A[j] > A[i]) 18 | min = Math.min(min, B[i] + B[j]); 19 | } 20 | hm.put(i, (int) min); 21 | } 22 | 23 | min = Long.MAX_VALUE; 24 | for (int i = 0; i < A.length - 2; i++) { 25 | if (hm.get(i) != Integer.MAX_VALUE) { 26 | for (int j = i + 1; j < A.length - 1; j++) { 27 | val = hm.get(j); 28 | if (val != Integer.MAX_VALUE && A[j] > A[i]) 29 | min = Math.min(min, B[i] + val); 30 | } 31 | } 32 | } 33 | 34 | return min == Long.MAX_VALUE ? -1 : (int) min; 35 | } 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n = s.nextInt(); 41 | int A[] = new int[n]; 42 | for (int i = 0; i < n; i++) 43 | A[i] = s.nextInt(); 44 | int B[] = new int[n]; 45 | for (int i = 0; i < n; i++) 46 | B[i] = s.nextInt(); 47 | System.out.println(new ChristmasTree().solve(A, B)); 48 | s.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Hashing/CountRectangles.java: -------------------------------------------------------------------------------- 1 | package ib.scaler.day38_Mar13; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | import java.util.Set; 6 | import java.util.TreeSet; 7 | 8 | public class CountRectangles { 9 | 10 | public int solve(int[] A, int[] B) { 11 | int count = 0; 12 | Set hs = new TreeSet<>((a1, a2) -> { 13 | if (Arrays.equals(a1, a2)) 14 | return 0; 15 | if (a1[0] != a2[0]) 16 | return Integer.valueOf(a1[0]).compareTo(Integer.valueOf(a2[0])); 17 | return Integer.valueOf(a1[1]).compareTo(Integer.valueOf(a2[1])); 18 | }); 19 | 20 | for (int i = 0; i < A.length; i++) 21 | hs.add(new int[] { A[i], B[i] }); 22 | 23 | for (int i = 0; i < A.length; i++) { 24 | for (int j = i + 1; j < A.length; j++) { 25 | if (A[i] != A[j] && B[i] != B[j] && hs.contains(new int[] { A[i], B[j] }) 26 | && hs.contains(new int[] { A[j], B[i] })) 27 | count++; 28 | } 29 | } 30 | 31 | return count / 2; 32 | } 33 | 34 | public static void main(String[] args) { 35 | // TODO Auto-generated method stub 36 | Scanner s = new Scanner(System.in); 37 | int n = s.nextInt(); 38 | int A[] = new int[n]; 39 | for (int i = 0; i < n; i++) 40 | A[i] = s.nextInt(); 41 | int B[] = new int[n]; 42 | for (int i = 0; i < n; i++) 43 | B[i] = s.nextInt(); 44 | System.out.println(new CountRectangles().solve(A, B)); 45 | s.close(); 46 | } 47 | 48 | } 49 | --------------------------------------------------------------------------------