├── dataStructures ├── Topic.txt ├── linkedlist │ ├── Client.java │ ├── ClientH.java │ ├── LinkedListG.java │ └── LinkedListH.java ├── genericTree │ ├── Client.java │ └── GenericTreeH.java └── binaryTree │ ├── Client.java │ └── BinaryTree.java ├── problemSolution ├── Topic.txt ├── interviewBit │ ├── level2 │ │ ├── math │ │ │ ├── SumofPairwiseHammingDistance.java │ │ │ ├── HammingDistance.java │ │ │ ├── ReverseInteger.java │ │ │ ├── PrimeSum.java │ │ │ └── PalindromeInteger.java │ │ └── array │ │ │ ├── MaximumConsecutiveGap.java │ │ │ ├── KthRowofPascalTriangle.java │ │ │ ├── TripletswithSumbetweengivenrange.java │ │ │ ├── FirstMissingInteger.java │ │ │ ├── WaveArray.java │ │ │ ├── MaxSumContiguousSubarray.java │ │ │ ├── PascalTriangle.java │ │ │ ├── NextPermutation.java │ │ │ ├── LargestNumber.java │ │ │ ├── FindDuplicateinArray.java │ │ │ ├── AddOneToNumber.java │ │ │ ├── Flip.java │ │ │ ├── RotateMatrix.java │ │ │ ├── MaximumAbsoluteDifference.java │ │ │ ├── SpiralOrderMatrixII.java │ │ │ ├── AntiDiagonals.java │ │ │ ├── RepeatAndMissingNumberArray.java │ │ │ └── MaxNonNegativeSubArray.java │ ├── level3 │ │ ├── bitManipulation │ │ │ ├── ReverseBits.java │ │ │ ├── SingleNumber.java │ │ │ ├── DivideIntegers.java │ │ │ ├── MinXORvalue.java │ │ │ ├── NumberOfOneBits.java │ │ │ ├── SingleNumberII.java │ │ │ └── DifferentBitsSumPairwise.java │ │ └── string │ │ │ └── PalindromeString.java │ └── level7 │ │ └── LengthofLongestSubsequence.java ├── way │ └── jan2019 │ │ ├── Pattern.java │ │ └── Balanced.java ├── interview_prepMY │ └── array │ │ ├── K_Concatenation.java │ │ └── Find_subarray_with_given_sum.java └── Parenthesis.java ├── coreJava ├── arrays │ ├── twoD │ │ ├── ExitPoint.java │ │ ├── README.md │ │ ├── WaveDisplay.java │ │ ├── SprialDisplay.java │ │ └── MatrixMultiplication.java │ └── oneD │ │ ├── LinearSearch.java │ │ ├── IsArrayMirrorInverse.java │ │ ├── InverseArray.java │ │ ├── ReverseArray.java │ │ ├── KadanesAlgorithm.java │ │ ├── BubbleSort.java │ │ ├── MaximumInArray.java │ │ ├── SelectionSort.java │ │ ├── BinarySearch.java │ │ ├── InsertionSort.java │ │ ├── RotateAnArray.java │ │ ├── MergeSort.java │ │ └── README.md ├── greedy │ └── greedy1 │ │ ├── Item.java │ │ ├── MaximumAdjacentSum.java │ │ ├── EgyptianFraction.java │ │ ├── MinimumStationRequired.java │ │ └── FractionalKnapsack.java ├── recursion │ ├── StartRecursion │ │ ├── PrintIncreasing.java │ │ ├── Power.java │ │ ├── PrintDecreasing.java │ │ ├── Factorial.java │ │ ├── PrintDecreasingIncreasing.java │ │ └── TowerOfHanoi.java │ ├── Array │ │ ├── ArrayDisplay.java │ │ ├── ReverseArrayDisplay.java │ │ ├── FindNumberInArray.java │ │ ├── MaximumValueInArray.java │ │ ├── FindLastIndex.java │ │ ├── AllIndex.java │ │ └── FindFirstIndex.java │ ├── memory │ │ ├── Permutation.java │ │ ├── SubsetWithTargetSum.java │ │ ├── SubsetsWithEqualSum.java │ │ ├── SubsetWithTargetSumArrayList.java │ │ └── SubsetsWithEqualSumArrayList.java │ ├── pAndC │ │ ├── PaymentCombination.java │ │ ├── PaymentPermutation.java │ │ ├── Combinations.java │ │ └── Permutations.java │ ├── arrayList │ │ ├── SubSequenceAscii.java │ │ └── SubSequence.java │ ├── nQueens │ │ ├── NKnight.java │ │ ├── NQueensBetter.java │ │ ├── NQueensSubSequence.java │ │ └── NQueens.java │ └── recursionQuesion │ │ ├── Sudoku.java │ │ └── CrossWord.java ├── string │ ├── PrintCharacter.java │ ├── PrintSubstring.java │ ├── IsPalindrome.java │ ├── ModifyString.java │ ├── ModifyEvenPandOddM.java │ ├── ToggleCharacters.java │ └── PrintPalindromeSubstring.java ├── bitManipulation │ ├── UniqueNumber.java │ ├── PrintBits.java │ ├── ToggleTheBit.java │ ├── TurnTheBitOFF.java │ ├── TurnTheBitON.java │ ├── IsKthBitONorOFF.java │ ├── CountOne.java │ ├── UniqueNumberTwo.java │ ├── TwoUniqueNumber.java │ └── README.md ├── dynamicProgramming │ ├── dynamicProgramming1 │ │ ├── Maze.java │ │ ├── MinCostPath.java │ │ ├── GoldMine.java │ │ ├── Fibonacci.java │ │ └── BoardCount.java │ ├── dynamicProgramming4 │ │ ├── CoinChangeCombination.java │ │ ├── CoinChangePermutation.java │ │ ├── RodCutting.java │ │ ├── MinJump.java │ │ ├── UnboundedKnapsack.java │ │ └── LongestBiotionicSubSequence.java │ ├── dynamicProgramming3 │ │ ├── LongestIncressingSubSequence.java │ │ ├── EggDrop.java │ │ ├── TargetSum.java │ │ └── KnapSack01.java │ ├── dynamicProgramming2 │ │ ├── TotalPalndromInString.java │ │ ├── FindMaxSquare.java │ │ ├── MatrixChainMultiplication.java │ │ └── MinimumPalindromicCut.java │ ├── dynamicProgramming5 │ │ ├── LongestCommenSubsequence.java │ │ ├── EditDistance.java │ │ └── LongestPalindropicSubsequence.java │ └── Try1.java └── README.md ├── README.md └── LICENSE /dataStructures/Topic.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /problemSolution/Topic.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /coreJava/arrays/twoD/ExitPoint.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.twoD; 2 | 3 | public class ExitPoint { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/math/SumofPairwiseHammingDistance.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.math; 2 | 3 | public class SumofPairwiseHammingDistance { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level3/bitManipulation/ReverseBits.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level3.bitManipulation; 2 | 3 | public class ReverseBits { 4 | 5 | public static void main(String[] args) { 6 | long ans = reverse(3); 7 | System.out.println(ans); 8 | } 9 | 10 | public static long reverse(long a) { 11 | long ans = 0; 12 | 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /coreJava/greedy/greedy1/Item.java: -------------------------------------------------------------------------------- 1 | package coreJava.greedy.greedy1; 2 | 3 | public class Item implements Comparable { 4 | 5 | int wt; 6 | int val; 7 | double ratio; 8 | 9 | @Override 10 | public int compareTo(Item o) { 11 | if (this.ratio > o.ratio) { 12 | return 1; 13 | } else if (this.ratio < o.ratio) { 14 | return -1; 15 | } else { 16 | return 0; 17 | } 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /coreJava/recursion/StartRecursion/PrintIncreasing.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.StartRecursion; 2 | 3 | public class PrintIncreasing { 4 | 5 | public static void main(String[] args) { 6 | printIncreasing(5); 7 | } 8 | 9 | public static void printIncreasing(int n) { 10 | if (n == 0) { 11 | return; 12 | } 13 | printIncreasing(n - 1); 14 | System.out.print(n + " "); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /coreJava/string/PrintCharacter.java: -------------------------------------------------------------------------------- 1 | package coreJava.string; 2 | 3 | public class PrintCharacter { 4 | 5 | public static void main(String[] args) { 6 | String input = "Sandip"; 7 | printCharacter(input); 8 | } 9 | 10 | public static void printCharacter(String input) { 11 | for (int i = 0; i < input.length(); i++) { 12 | char ch = input.charAt(i); 13 | System.out.println(ch); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /coreJava/recursion/StartRecursion/Power.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.StartRecursion; 2 | 3 | public class Power { 4 | 5 | public static void main(String[] args) { 6 | int ans = power(5, 3); 7 | System.out.println(ans); 8 | } 9 | 10 | public static int power(int x, int n) { 11 | if (n == 0) { 12 | return 1; 13 | } 14 | int ansPartial = x * power(x, n - 1); 15 | return ansPartial; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /coreJava/recursion/StartRecursion/PrintDecreasing.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.StartRecursion; 2 | 3 | public class PrintDecreasing { 4 | 5 | public static void main(String[] args) { 6 | printDecreasing(3); 7 | } 8 | 9 | public static void printDecreasing(int n) { 10 | if(n==0){ 11 | return ; 12 | } 13 | System.out.print(n + " "); 14 | printDecreasing(n-1); 15 | System.out.println("HI"); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /coreJava/recursion/StartRecursion/Factorial.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.StartRecursion; 2 | 3 | public class Factorial { 4 | 5 | public static void main(String[] args) { 6 | int ans = factorial(5); 7 | System.out.println(ans); 8 | } 9 | 10 | public static int factorial(int n) { 11 | if (n == 0 || n == 1) { 12 | return 1; 13 | } 14 | int ansParical = n * factorial(n - 1); 15 | return ansParical; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/math/HammingDistance.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.math; 2 | 3 | import java.util.*; 4 | 5 | public class HammingDistance { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | 10 | System.out.println(2&4); 11 | 12 | } 13 | 14 | public int hammingDistance(final List A) { 15 | 16 | 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Programming-in-Java 2 | This repository contains some useful codes, techniques, algorithms and problem solutions. 3 | 4 | ### Note 5 | I am still working on this I will be adding input/output formet and comment also. I will be adding more topics also. 6 | 7 | ### How to contribute 8 | #### Refer to the [Coding Standards Wiki Page](https://github.com/Sandip75/Programming-in-Java/wiki/Coding-Standards) for the coding standards to be followed. 9 | -------------------------------------------------------------------------------- /coreJava/arrays/twoD/README.md: -------------------------------------------------------------------------------- 1 | [Matrix Multiplication](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/MatrixMultiplication.java)
2 | [Wave Display](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/WaveDisplay.java)
3 | [Spiral Display](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/SprialDisplay.java)
4 | [Exit Point]()
5 | [90 degree rotate]()
6 | -------------------------------------------------------------------------------- /coreJava/recursion/Array/ArrayDisplay.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.Array; 2 | 3 | public class ArrayDisplay { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 5, 4, 9, 1, 18 }; 7 | arrayDisplay(arr, 0); 8 | } 9 | 10 | public static void arrayDisplay(int[] arr, int vidx) { 11 | if (vidx == arr.length) { 12 | return; 13 | } 14 | System.out.print(arr[vidx] + " "); 15 | arrayDisplay(arr, vidx + 1); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /coreJava/bitManipulation/UniqueNumber.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class UniqueNumber { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 9, 5, 3, 9, 3 }; 7 | int ans = uniqueNumber(arr); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int uniqueNumber(int[] arr) { 12 | int ans = 0; 13 | for (int i = 0; i < arr.length; i++) { 14 | ans = ans ^ arr[i]; 15 | } 16 | return ans; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /coreJava/string/PrintSubstring.java: -------------------------------------------------------------------------------- 1 | package coreJava.string; 2 | 3 | public class PrintSubstring { 4 | 5 | public static void main(String[] args) { 6 | String input = "abcd"; 7 | printSubstring(input); 8 | } 9 | 10 | public static void printSubstring(String input) { 11 | for (int i = 0; i < input.length(); i++) { 12 | for (int j = i + 1; j<=input.length(); j++) { 13 | System.out.println(input.substring(i, j)); 14 | } 15 | } 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /coreJava/recursion/StartRecursion/PrintDecreasingIncreasing.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion; 2 | 3 | public class PrintDecreasingIncreasing { 4 | 5 | public static void main(String[] args) { 6 | printDecreasingIncreasing(5); 7 | } 8 | 9 | public static void printDecreasingIncreasing(int n) { 10 | if(n==0){ 11 | return ; 12 | } 13 | System.out.print(n + " "); 14 | printDecreasingIncreasing(n-1); 15 | System.out.print(n + " "); 16 | } 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /coreJava/recursion/Array/ReverseArrayDisplay.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.Array; 2 | 3 | public class ReverseArrayDisplay { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 5, 4, 9, 1, 18 }; 7 | reverseArrayDisplay(arr, 0); 8 | } 9 | 10 | public static void reverseArrayDisplay(int[] arr, int vidx) { 11 | if (vidx == arr.length) { 12 | return; 13 | } 14 | reverseArrayDisplay(arr, vidx + 1); 15 | System.out.print(arr[vidx] + " "); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /coreJava/bitManipulation/PrintBits.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class PrintBits { 4 | 5 | public static void main(String[] args) { 6 | int n = 75; 7 | System.out.println(Integer.toBinaryString(n)); 8 | printBit(n); 9 | } 10 | 11 | public static void printBit(int n) { 12 | for (int i = 31; i >= 0; i--) { 13 | int mask = (1 << i); 14 | if ((mask & n) == mask) { 15 | System.out.print(1); 16 | } else { 17 | System.out.print(0); 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/LinearSearch.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class LinearSearch { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 10, 15, 55, 22, 11, 33 }; 7 | int data = 22; 8 | int index = linearSearch(arr, data); 9 | System.out.println(index); 10 | } 11 | 12 | private static int linearSearch(int[] arr, int data) { 13 | for (int i = 0; i < arr.length; i++) { 14 | if (arr[i] == data) { 15 | return i; 16 | } 17 | } 18 | return -1; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /coreJava/bitManipulation/ToggleTheBit.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class ToggleTheBit { 4 | 5 | public static void main(String[] args) { 6 | int n = 25; 7 | int k = 3; 8 | System.out.println(Integer.toBinaryString(n)); 9 | int ans = toggleTheBit(n, k); 10 | System.out.println(Integer.toBinaryString(ans)); 11 | System.out.println(ans); 12 | } 13 | 14 | public static int toggleTheBit(int n, int k) { 15 | int mask = (1 << k); 16 | n = n ^ mask; 17 | return n; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/IsArrayMirrorInverse.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class IsArrayMirrorInverse { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 3, 4, 2, 0, 1 }; 7 | boolean ans = isArrayMirrorInverse(arr); 8 | System.out.println(ans); 9 | } 10 | 11 | public static boolean isArrayMirrorInverse(int[] arr) { 12 | for (int i = 0; i < arr.length; i++) { 13 | int val = arr[i]; 14 | if (arr[val] != i) { 15 | return false; 16 | } 17 | } 18 | return true; 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /coreJava/bitManipulation/TurnTheBitOFF.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class TurnTheBitOFF { 4 | 5 | public static void main(String[] args) { 6 | int n = 75; 7 | int k = 3; 8 | System.out.println(Integer.toBinaryString(n)); 9 | int ans = turnTheBitOFF(n, k); 10 | System.out.println(Integer.toBinaryString(ans)); 11 | System.out.println(ans); 12 | } 13 | 14 | public static int turnTheBitOFF(int n, int k) { 15 | int mask = ( 1 << k ) ; 16 | n = n & (~ mask ); 17 | return 1; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /coreJava/bitManipulation/TurnTheBitON.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class TurnTheBitON { 4 | 5 | public static void main(String[] args) { 6 | int n = 25; 7 | int k = 1; 8 | System.out.println(Integer.toBinaryString(n)); 9 | int ans = turnTheBitON(n, k); 10 | System.out.println(Integer.toBinaryString(ans)); 11 | System.out.println(ans); 12 | } 13 | 14 | public static int turnTheBitON(int n, int k) { 15 | int mask = (1 << k); 16 | n = n | mask; 17 | return n; 18 | } 19 | 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/InverseArray.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class InverseArray { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 3, 0, 1, 4, 2 }; 7 | int[] ans = inverseOfAnArray(arr); 8 | for (int val : ans) { 9 | System.out.print(val + " "); 10 | } 11 | } 12 | 13 | public static int[] inverseOfAnArray(int[] arr) { 14 | int[] inverse = new int[arr.length]; 15 | for (int i = 0; i < arr.length; i++) { 16 | int val = arr[i]; 17 | inverse[val] = i; 18 | } 19 | return inverse; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /coreJava/string/IsPalindrome.java: -------------------------------------------------------------------------------- 1 | package coreJava.string; 2 | 3 | public class IsPalindrome { 4 | 5 | public static void main(String[] args) { 6 | String input = "naman"; 7 | boolean ans = isPalindrome(input); 8 | System.out.println(ans); 9 | } 10 | 11 | public static boolean isPalindrome(String input) { 12 | int left = 0; 13 | int right = input.length() - 1; 14 | while (left < right) { 15 | if (input.charAt(left) != input.charAt(right)) { 16 | return false; 17 | } 18 | left++; 19 | right--; 20 | } 21 | return true; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /coreJava/recursion/Array/FindNumberInArray.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.Array; 2 | 3 | public class FindNumberInArray { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 5, 44, 9, 1, 18 }; 7 | boolean ans = findNumberInArray(arr, 1, 0); 8 | System.out.println(ans); 9 | } 10 | 11 | public static boolean findNumberInArray(int[] arr, int data, int vidx) { 12 | if (vidx == arr.length) { 13 | return false; 14 | } 15 | if (arr[vidx] == data) { 16 | return true; 17 | } 18 | boolean ans = findNumberInArray(arr, data, vidx + 1); 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /coreJava/bitManipulation/IsKthBitONorOFF.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class IsKthBitONorOFF { 4 | 5 | public static void main(String[] args) { 6 | int n = 25; 7 | int k = 4; 8 | // System.out.println(Integer.toBinaryString(n)); 9 | isKthBitONorOFF(n, k); 10 | } 11 | 12 | public static void isKthBitONorOFF(int n, int k) { 13 | 14 | int mask = (1 << k); 15 | System.out.println(Integer.toBinaryString(mask)); 16 | 17 | if ((n & mask) == mask) { 18 | // System.out.println("ON"); 19 | } else { 20 | //System.out.println("OFF"); 21 | } 22 | 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /problemSolution/way/jan2019/Pattern.java: -------------------------------------------------------------------------------- 1 | package problemSolution.way.jan2019; 2 | import java.util.*; 3 | 4 | public class Pattern { 5 | 6 | public static void main(String[] args) { 7 | Scanner s = new Scanner(System.in); 8 | int n = s.nextInt(); 9 | 10 | //Your Code 11 | 12 | for (int i = 0; i < n; i++) { 13 | for (int j = 0; j < n; j++) { 14 | if(j==0 || j==n-1){ 15 | System.out.print("*"); 16 | }else if(i==j || i+j == n-1){ 17 | System.out.print("*"); 18 | }else{ 19 | System.out.print(" "); 20 | } 21 | } 22 | System.out.println(); 23 | } 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming1/Maze.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming1; 2 | 3 | public class Maze { 4 | 5 | public static void main(String[] args) { 6 | System.out.println(mazeHVpathT(3, 3)); 7 | } 8 | 9 | public static int mazeHVpathT(int dr, int dc) { 10 | 11 | int[][] ans = new int[dr + 1][dc + 1]; 12 | 13 | for (int i = dr; i >= 0; i--) { 14 | for (int j = dc; j >= 0; j--) { 15 | if (i == dr || j == dc) { 16 | ans[i][j] = 1; 17 | } else { 18 | ans[i][j] = ans[i + 1][j] + ans[i][j + 1]; 19 | } 20 | } 21 | } 22 | return ans[0][0]; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /coreJava/recursion/Array/MaximumValueInArray.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.Array; 2 | 3 | public class MaximumValueInArray { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 5, 44, 9, 1, 18 }; 7 | int ans = maximumValueInArray(arr, 0); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int maximumValueInArray(int[] arr, int vidx) { 12 | if (vidx == arr.length) { 13 | return Integer.MIN_VALUE; 14 | } 15 | 16 | int ansPartial = maximumValueInArray(arr, vidx + 1); 17 | if (arr[vidx] > ansPartial) { 18 | return arr[vidx]; 19 | } else { 20 | return ansPartial; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /coreJava/recursion/memory/Permutation.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.memory; 2 | 3 | public class Permutation { 4 | 5 | public static void main(String[] args) { 6 | String ques = "abc"; 7 | permutation(ques, ""); 8 | } 9 | 10 | public static void permutation(String ques, String ans) { 11 | 12 | if(ques.length()==0){ 13 | System.out.println(ans); 14 | return; 15 | } 16 | 17 | for (int i = 0; i < ques.length(); i++) { 18 | char ch = ques.charAt(i); 19 | 20 | String left = ques.substring(0, i); 21 | String right = ques.substring(i + 1); 22 | 23 | permutation(left + right, ans + ch); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level3/bitManipulation/SingleNumber.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level3.bitManipulation; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class SingleNumber { 6 | 7 | public static void main(String[] args) { 8 | ArrayList A = new ArrayList<>(); 9 | A.add(1); 10 | A.add(3); 11 | A.add(1); 12 | A.add(3); 13 | A.add(5); 14 | int ans = singleNumber(A); 15 | System.out.println(ans); 16 | } 17 | 18 | public static int singleNumber(final ArrayList A) { 19 | int ans = 0; 20 | 21 | for (int i : A) { 22 | ans = ans ^ i; 23 | } 24 | 25 | return ans; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /coreJava/recursion/Array/FindLastIndex.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.Array; 2 | 3 | public class FindLastIndex { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 5, 44, 9, 1, 44, 18 }; 7 | int ans = findLastIndex(arr, 44, 0); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int findLastIndex(int[] arr, int data, int vidx) { 12 | if (vidx == arr.length) { 13 | return -1; 14 | } 15 | int ans = findLastIndex(arr, data, vidx + 1); 16 | if (ans != -1) { 17 | return ans; 18 | } else { 19 | if (data == arr[vidx]) { 20 | return vidx; 21 | } else { 22 | return ans; 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /coreJava/string/ModifyString.java: -------------------------------------------------------------------------------- 1 | package coreJava.string; 2 | 3 | public class ModifyString { 4 | 5 | public static void main(String[] args) { 6 | String input = "abcde"; 7 | String ans = modifyString(input); 8 | System.out.println(ans); 9 | } 10 | 11 | public static String modifyString(String input) { 12 | StringBuilder ansP = new StringBuilder(); 13 | 14 | for (int i = 1; i < input.length()-1; i++) { 15 | char ch = input.charAt(i); 16 | char chM = input.charAt(i - 1); 17 | ansP.append(chM); 18 | ansP.append(ch - chM); 19 | } 20 | ansP.append(input.charAt(input.length()-1)); 21 | return ansP.toString(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /coreJava/bitManipulation/CountOne.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class CountOne { 4 | 5 | public static void main(String[] args) { 6 | int n = 15; 7 | int ans = countOne(n); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int countOne(int n) { 12 | int count = 0; 13 | while (n != 0) { 14 | int help = n - 1; 15 | // System.out.println(Integer.toBinaryString(n)); 16 | // System.out.println(Integer.toBinaryString(help)); 17 | n = n & help; 18 | // System.out.println(Integer.toBinaryString(n)); 19 | // System.out.println(); 20 | count++; 21 | } 22 | 23 | return count; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /coreJava/arrays/twoD/WaveDisplay.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.twoD; 2 | 3 | public class WaveDisplay { 4 | 5 | public static void main(String[] args) { 6 | int[][] arr = { { 11, 12, 13, 14 }, { 21, 22, 23, 24 }, { 31, 32, 33, 34 }, { 41, 42, 43, 44 } }; 7 | waveDisplay(arr); 8 | } 9 | 10 | public static void waveDisplay(int[][] arr) { 11 | for (int j = 0; j < arr[0].length; j++) { 12 | if (j % 2 == 0) { 13 | for (int i = 0; i < arr.length; i++) { 14 | System.out.print(arr[j][i] + " "); 15 | } 16 | } else { 17 | for (int i = arr.length - 1; i >= 0; i--) { 18 | System.out.print(arr[j][i] + " "); 19 | } 20 | } 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /coreJava/greedy/greedy1/MaximumAdjacentSum.java: -------------------------------------------------------------------------------- 1 | package coreJava.greedy.greedy1; 2 | 3 | public class MaximumAdjacentSum { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 5, 6, 15, 100, 25, 5 }; 8 | int ans = maximumAdjacentSum(arr); 9 | System.out.println(ans); 10 | } 11 | 12 | public static int maximumAdjacentSum(int[] arr) { 13 | int inc = arr[0]; 14 | int exc = 0; 15 | 16 | for (int i = 1; i < arr.length; i++) { 17 | int oinc = inc; 18 | int oexc = exc; 19 | inc = exc + arr[i]; 20 | exc = Math.max(oinc, oexc); 21 | } 22 | 23 | return Math.max(inc, exc); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/ReverseArray.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class ReverseArray { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 10, 20, 30, 40, 50, 60 }; 7 | reverseArray(arr); 8 | for (int val : arr) { 9 | System.out.print(val + " "); 10 | } 11 | } 12 | 13 | public static void reverseArray(int[] arr) { 14 | int left = 0; 15 | int right = arr.length - 1; 16 | 17 | while (left <= right) { 18 | swap(arr, left, right); 19 | left++; 20 | right--; 21 | } 22 | } 23 | 24 | private static void swap(int[] arr, int i, int j) { 25 | int temp = arr[i]; 26 | arr[i] = arr[j]; 27 | arr[j] = temp; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /coreJava/recursion/pAndC/PaymentCombination.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.pAndC; 2 | 3 | public class PaymentCombination { 4 | 5 | public static void main(String[] args) { 6 | int[] coins = { 2, 3, 5, 6 }; 7 | int amount = 10; 8 | paymentCombination(coins, amount, 0, ""); 9 | 10 | } 11 | 12 | public static void paymentCombination(int[] coins, int tAmount, int coinIdx, String asf) { 13 | 14 | if (tAmount == 0) { 15 | System.out.println(asf); 16 | return; 17 | } 18 | 19 | for (int i = coinIdx; i < coins.length; i++) { 20 | if (coins[i] <= tAmount) { 21 | paymentCombination(coins, tAmount - coins[i], i, asf + coins[i]); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/KadanesAlgorithm.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class KadanesAlgorithm { 4 | 5 | public static void main(String[] args) { 6 | int[] input = { 2, 3, -6, 1, 2, 3, -4, 5 }; 7 | int ans = kadanesAlgorithm(input); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int kadanesAlgorithm(int[] arr) { 12 | int current_sum = arr[0]; 13 | int overall_sum = arr[0]; 14 | 15 | for (int i = 1; i < arr.length; i++) { 16 | 17 | if(current_sum < 0){ 18 | current_sum = arr[i]; 19 | }else{ 20 | current_sum += arr[i]; 21 | } 22 | overall_sum = Math.max(current_sum, overall_sum); 23 | } 24 | return overall_sum; 25 | } 26 | } -------------------------------------------------------------------------------- /coreJava/recursion/memory/SubsetWithTargetSum.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.memory; 2 | 3 | public class SubsetWithTargetSum { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 1, 2, 3, 4, 5 }; 7 | int target = 6; 8 | subsetWithTargetSum(arr, 0, target, 0, ""); 9 | } 10 | 11 | public static void subsetWithTargetSum(int[] arr, int vidx, int target, int sum, String set) { 12 | if (vidx == arr.length) { 13 | if (target == sum) { 14 | System.out.println(set); 15 | } 16 | return; 17 | } 18 | subsetWithTargetSum(arr, vidx + 1, target, sum + arr[vidx], set + arr[vidx] + " "); 19 | subsetWithTargetSum(arr, vidx + 1, target, sum, set); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class BubbleSort { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 10, 15, 2, -1, 7 }; 7 | bubbleSort(arr); 8 | for (int val : arr) { 9 | System.out.print(val + " "); 10 | } 11 | } 12 | 13 | public static void bubbleSort(int[] arr) { 14 | for (int i = 1; i < arr.length ; i++) { 15 | for (int j = 0; j < (arr.length - i ); j++) { 16 | if (arr[j] > arr[j + 1]) { 17 | swap(arr, j, j + 1); 18 | } 19 | } 20 | } 21 | } 22 | 23 | private static void swap(int[] arr, int i, int j) { 24 | int temp = arr[i]; 25 | arr[i] = arr[j]; 26 | arr[j] = temp; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/MaximumInArray.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | import java.util.*; 4 | 5 | public class MaximumInArray { 6 | 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | int n = scanner.nextInt(); 10 | int[] arr = new int[n]; 11 | for (int i = 0; i < arr.length; i++) { 12 | arr[i] = scanner.nextInt(); 13 | } 14 | int ans = maxValueInArray(arr); 15 | System.out.println(ans); 16 | } 17 | 18 | public static int maxValueInArray(int[] arr) { 19 | int max = arr[0]; 20 | for (int i = 1; i < arr.length; i++) { 21 | if (max < arr[i]) { 22 | max = arr[i]; 23 | } 24 | } 25 | return max; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /coreJava/bitManipulation/UniqueNumberTwo.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class UniqueNumberTwo { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 9, 5, 3, 9, 9, 3, 3 }; 7 | int ans = uniqueNumber(arr); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int uniqueNumber(int[] arr) { 12 | int ans = 0; 13 | for (int i = 0; i < 32; i++) { 14 | int count = 0; 15 | int mask = 1 << i; 16 | for (int j = 0; j < arr.length; j++) { 17 | if ((arr[j] & mask) == mask) { 18 | count++; 19 | } 20 | } 21 | count = count % 3; 22 | if (count != 0) { 23 | ans = ans | mask; 24 | } 25 | } 26 | return ans; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /coreJava/recursion/pAndC/PaymentPermutation.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.pAndC; 2 | 3 | public class PaymentPermutation { 4 | 5 | public static void main(String[] args) { 6 | int[] coins = { 2, 3 ,5,6}; 7 | int amount = 10; 8 | paymentPermutation(coins, amount, 0, ""); 9 | 10 | } 11 | 12 | public static void paymentPermutation(int[] coins, int tAmount, int paid, String asf) { 13 | 14 | if (tAmount == paid) { 15 | System.out.println(asf); 16 | return; 17 | } 18 | 19 | for (int i = 0; i < coins.length; i++) { 20 | if ((paid + coins[i]) <= tAmount) { 21 | paymentPermutation(coins, tAmount, paid + coins[i], asf + coins[i]); 22 | } 23 | } 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/SelectionSort.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class SelectionSort { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 10, 15, -1, 2, 7 }; 7 | selectionSort(arr); 8 | for (int val : arr) { 9 | System.out.print(val + " "); 10 | } 11 | } 12 | 13 | public static void selectionSort(int[] arr) { 14 | for (int i = 0; i < arr.length - 1; i++) { 15 | for (int j = i + 1; j < arr.length; j++) { 16 | if (arr[i] > arr[j]) { 17 | swap(arr, i, j); 18 | } 19 | } 20 | } 21 | } 22 | 23 | private static void swap(int[] arr, int i, int j) { 24 | int temp = arr[i]; 25 | arr[i] = arr[j]; 26 | arr[j] = temp; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class BinarySearch { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 10, 20, 30, 40, 50, 60 }; 7 | int ans = binarySearch(arr, 50); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int binarySearch(int[] arr, int data) { 12 | int left = 0; 13 | int right = arr.length; 14 | int idx = -1; 15 | while (left <= right) { 16 | int mid = left + (right - left) / 2; 17 | if (arr[mid] > data) { 18 | right = mid - 1; 19 | } else if (arr[mid] < data) { 20 | left = mid + 1; 21 | } else { 22 | idx = mid; 23 | break; 24 | } 25 | } 26 | return idx; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/MaximumConsecutiveGap.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class MaximumConsecutiveGap { 6 | 7 | public static void main(String[] args) { 8 | List l = new ArrayList<>(); 9 | l.add(1); 10 | l.add(2); 11 | l.add(5); 12 | l.add(7); 13 | int ans = maximumGap(l); 14 | System.out.println(ans); 15 | } 16 | 17 | public static int maximumGap(final List A) { 18 | int ans =0; 19 | Collections.sort(A); 20 | for (int i = 1; i < A.size(); i++) { 21 | int diff = A.get(i) - A.get(i-1); 22 | if(diff > ans ){ 23 | ans = diff ; 24 | } 25 | } 26 | return ans; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/KthRowofPascalTriangle.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class KthRowofPascalTriangle { 6 | 7 | public static void main(String[] args) { 8 | 9 | int n = 5; 10 | ArrayList ans = getRow(n); 11 | for (int i = 0; i < ans.size(); i++) { 12 | System.out.print(ans.get(i) + " "); 13 | } 14 | 15 | } 16 | 17 | public static ArrayList getRow(int n) { 18 | 19 | ArrayList ans = new ArrayList(); 20 | 21 | int val = 1; 22 | for (int i = 0; i <= n; i++) { 23 | ans.add(val); 24 | val = (val * (n - i)) / (i + 1); 25 | } 26 | 27 | return ans; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level3/bitManipulation/DivideIntegers.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level3.bitManipulation; 2 | 3 | public class DivideIntegers { 4 | 5 | public static void main(String[] args) { 6 | int A = 15; 7 | int B = 4; 8 | int ans = divide(A, B); 9 | System.out.println(ans); 10 | } 11 | 12 | public static int divide(int A, int B) { 13 | 14 | int ans = A & ~(B - 1); 15 | int temp = B - 1; 16 | int count = 0; 17 | while (temp != 0) { 18 | temp = temp & (temp - 1); 19 | count++; 20 | } 21 | 22 | int i = 1; 23 | while (i <= count) { 24 | System.out.println("hii"); 25 | ans = ans >>> 1; 26 | i++; 27 | } 28 | 29 | return ans; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/TripletswithSumbetweengivenrange.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class TripletswithSumbetweengivenrange { 6 | 7 | public static void main(String[] args) { 8 | ArrayList al = new ArrayList(); 9 | al.add("0.6"); 10 | al.add("0.7"); 11 | al.add("0.8"); 12 | al.add("1.2"); 13 | al.add("0.4"); 14 | 15 | int ans = solve(al); 16 | System.out.println(ans); 17 | } 18 | 19 | public static int solve(ArrayList A) { 20 | 21 | for(int i=0 ; i < A.size() ; i++){ 22 | String t = A.get(i); 23 | int temp = (int)(t - "0"); 24 | } 25 | 26 | 27 | return 0; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming4/CoinChangeCombination.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming4; 2 | 3 | public class CoinChangeCombination { 4 | 5 | public static void main(String[] args) { 6 | int[] coin = { 2, 3, 5, 6 }; 7 | int target = 10; 8 | int ans = coinChangeCombination(coin, target); 9 | System.out.println(ans); 10 | } 11 | 12 | private static int coinChangeCombination(int[] coin, int target) { 13 | 14 | int[] ans = new int[target + 1]; 15 | ans[0] = 1; 16 | for (int i = 0; i < coin.length; i++) { 17 | for (int j = coin[i]; j < ans.length; j++) { 18 | ans[j] = ans[j] + ans[j - coin[i]]; 19 | } 20 | } 21 | 22 | return ans[target]; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /coreJava/string/ModifyEvenPandOddM.java: -------------------------------------------------------------------------------- 1 | package coreJava.string; 2 | 3 | public class ModifyEvenPandOddM { 4 | 5 | public static void main(String[] args) { 6 | String input = "abcde"; 7 | String ans = modifyEvenPandOddM(input); 8 | System.out.println(ans); 9 | } 10 | 11 | public static String modifyEvenPandOddM(String input) { 12 | StringBuilder ansP = new StringBuilder(input); 13 | boolean flag = true; 14 | for (int i = 0; i < input.length(); i++) { 15 | char ch = input.charAt(i); 16 | if (flag) { 17 | ch = (char) (ch + 1); 18 | flag = false; 19 | } else { 20 | ch = (char) (ch - 1); 21 | flag = true; 22 | } 23 | ansP.setCharAt(i, ch); 24 | } 25 | return ansP.toString(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /coreJava/string/ToggleCharacters.java: -------------------------------------------------------------------------------- 1 | package coreJava.string; 2 | 3 | public class ToggleCharacters { 4 | 5 | public static void main(String[] args) { 6 | String input = "Sandip"; 7 | String ans = toggleCharacters(input); 8 | System.out.println(ans); 9 | } 10 | 11 | public static String toggleCharacters(String input) { 12 | StringBuilder ansP = new StringBuilder(input); 13 | 14 | for (int i = 0; i < input.length(); i++) { 15 | char ch = input.charAt(i); 16 | if (ch >= 'A' && ch <= 'Z') { 17 | ch = (char) ( ch + 'a' - 'A' ); 18 | } else if (ch >= 'a' && ch <= 'z') { 19 | ch = (char) ( ch + 'A' - 'a' ); 20 | } 21 | ansP.setCharAt(i, ch); 22 | } 23 | return ansP.toString(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/InsertionSort.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class InsertionSort { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 10, 2, 17, -1, 5, 3 }; 7 | insertionSort(arr); 8 | for (int val : arr) { 9 | System.out.print(val + " "); 10 | } 11 | } 12 | 13 | public static void insertionSort(int[] arr) { 14 | for (int i = 1; i < arr.length; i++) { 15 | for (int j = i; j > 0; j--) { 16 | if (arr[j] < arr[j - 1]) { 17 | swap(arr, j, j - 1); 18 | } else { 19 | break; 20 | } 21 | } 22 | } 23 | } 24 | 25 | private static void swap(int[] arr, int i, int j) { 26 | arr[i] = arr[i] + arr[j]; 27 | arr[j] = arr[i] - arr[j]; 28 | arr[i] = arr[i] - arr[j]; 29 | } 30 | } -------------------------------------------------------------------------------- /coreJava/bitManipulation/TwoUniqueNumber.java: -------------------------------------------------------------------------------- 1 | package coreJava.bitManipulation; 2 | 3 | public class TwoUniqueNumber { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 9, 5, 3, 9, 3, 4, 9, 9 }; 7 | twoUniqueNumber(arr); 8 | } 9 | 10 | public static void twoUniqueNumber(int[] arr) { 11 | int ans = 0; 12 | for (int i = 0; i < arr.length; i++) { 13 | ans = ans ^ arr[i]; 14 | } 15 | 16 | int changingBit = ans & -ans; 17 | int n1 = 0; 18 | int n2 = 0; 19 | 20 | for (int i = 0; i < arr.length; i++) { 21 | if ((arr[i] & changingBit) != 0) { 22 | n1 = n1 ^ arr[i]; 23 | } else { 24 | n2 = n2 ^ arr[i]; 25 | } 26 | } 27 | 28 | System.out.println(n1); 29 | System.out.println(n2); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /coreJava/recursion/Array/AllIndex.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.Array; 2 | 3 | public class AllIndex { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 5, 44, 44, 1, 44, 18, 44 }; 7 | int ans[] = allIndex(arr, 44, 0, 0); 8 | for (int val : ans) { 9 | System.out.print(val + "-->>"); 10 | } 11 | } 12 | 13 | public static int[] allIndex(int[] arr, int data, int vidx, int count) { 14 | if (vidx == arr.length) { 15 | int[] baseArray = new int[count]; 16 | return baseArray; 17 | } 18 | if (arr[vidx] == data) { 19 | count = count + 1; 20 | } 21 | int[] ansP = allIndex(arr, data, vidx + 1, count); 22 | if (arr[vidx] == data) { 23 | ansP[count - 1] = vidx; 24 | } 25 | return ansP; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /coreJava/recursion/memory/SubsetsWithEqualSum.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.memory; 2 | 3 | public class SubsetsWithEqualSum { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 10, 20, 30, 40, 50, 60, 70 }; 7 | subsetsWithEqualSum(arr, 0, 0, "", 0, ""); 8 | } 9 | 10 | public static void subsetsWithEqualSum(int[] arr, int vidx, int sum1, String set1, int sum2, String set2) { 11 | if (arr.length == vidx) { 12 | if (sum1 == sum2) { 13 | System.out.println(set1 + " && " + set2); 14 | } 15 | return; 16 | } 17 | subsetsWithEqualSum(arr, vidx + 1, sum1 + arr[vidx], set1 + arr[vidx] + " ", sum2, set2); 18 | subsetsWithEqualSum(arr, vidx + 1, sum1, set1, sum2 + arr[vidx], set2 + arr[vidx] + " "); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming4/CoinChangePermutation.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming4; 2 | 3 | public class CoinChangePermutation { 4 | 5 | public static void main(String[] args) { 6 | int[] coin = { 2, 3, 5, 6 }; 7 | int target = 10; 8 | int ans = coinChangePermutation(coin, target); 9 | System.out.println(ans); 10 | } 11 | 12 | public static int coinChangePermutation(int[] coin, int target) { 13 | 14 | int[] ans = new int[target + 1]; 15 | ans[0] = 1; 16 | for (int i = 1; i < ans.length; i++) { 17 | for (int j = 0; j < coin.length; j++) { 18 | if (i >= coin[j]) { 19 | ans[i] = ans[i] + ans[i - coin[j]]; 20 | } 21 | } 22 | } 23 | return ans[target]; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /coreJava/greedy/greedy1/EgyptianFraction.java: -------------------------------------------------------------------------------- 1 | package coreJava.greedy.greedy1; 2 | 3 | public class EgyptianFraction { 4 | 5 | public static void main(String[] args) { 6 | egyptianFraction(3, 7); 7 | } 8 | 9 | public static void egyptianFraction(int num, int dem) { 10 | if (num == 0) { 11 | return; 12 | } 13 | 14 | if (num == 1) { 15 | System.out.print(num + "/" + dem); 16 | return; 17 | } 18 | 19 | int dem1 = demB(num, dem); 20 | dem1 = dem1 / num; 21 | System.out.print("1" + "/" + dem1 + " " + " + "); 22 | egyptianFraction(num * dem1 - dem, dem * dem1); 23 | 24 | } 25 | 26 | private static int demB(int num, int dem) { 27 | while ((dem % num) != 0) { 28 | dem = dem + 1; 29 | } 30 | return dem; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level3/bitManipulation/MinXORvalue.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level3.bitManipulation; 2 | 3 | import java.util.*; 4 | 5 | public class MinXORvalue { 6 | 7 | public static void main(String[] args) { 8 | ArrayList A = new ArrayList<>(); 9 | A.add(0); 10 | A.add(4); 11 | A.add(7); 12 | A.add(9); 13 | int ans = findMinXor(A); 14 | System.out.println(ans); 15 | } 16 | 17 | public static int findMinXor(ArrayList A) { 18 | 19 | Collections.sort(A); 20 | 21 | int min = Integer.MAX_VALUE; 22 | for (int i = 0; i < A.size() - 1; i++) { 23 | int temp = A.get(i) ^ A.get(i + 1); 24 | if (min > temp) { 25 | min = temp; 26 | } 27 | } 28 | return min; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming4/RodCutting.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming4; 2 | 3 | public class RodCutting { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 0, 3, 5, 6, 15, 10, 25, 12, 24 }; 7 | int target = 8; 8 | int ans = rodCutting(arr, target); 9 | System.out.println(ans); 10 | } 11 | 12 | public static int rodCutting(int[] arr, int target) { 13 | int[] ans = new int[target + 1]; 14 | 15 | for (int i = 1; i < ans.length; i++) { 16 | ans[i] = arr[i]; 17 | int left = 1; 18 | int right = i - 1; 19 | while (left <= right) { 20 | ans[i] = Integer.max(ans[i], ans[left] + ans[right]); 21 | left++; 22 | right--; 23 | } 24 | } 25 | return ans[target]; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/math/ReverseInteger.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.math; 2 | 3 | public class ReverseInteger { 4 | 5 | public static void main(String[] args) { 6 | int n = -1146467285; 7 | int ans = reverse(n); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int reverse(int A) { 12 | boolean flag = false; 13 | if (A < 0) { 14 | flag = true; 15 | A = -A; 16 | } 17 | int rev = 0; 18 | while (A != 0) { 19 | int rem = A % 10; 20 | if (rev > Integer.MAX_VALUE / 10 || ((rev == Integer.MAX_VALUE / 10) && rem > Integer.MAX_VALUE % 10)) { 21 | return 0; 22 | } 23 | rev = rev * 10 + rem; 24 | A = A / 10; 25 | } 26 | 27 | if (flag == true) { 28 | rev = -rev; 29 | } 30 | 31 | return rev; 32 | } 33 | } -------------------------------------------------------------------------------- /problemSolution/interviewBit/level3/bitManipulation/NumberOfOneBits.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level3.bitManipulation; 2 | 3 | public class NumberOfOneBits { 4 | 5 | public static void main(String[] args) { 6 | int ans = numSetBits(429496729); 7 | System.out.println(ans); 8 | int ans2 = numSetBitsBetter(429496729); 9 | System.out.println(ans2); 10 | } 11 | 12 | public static int numSetBits(long a) { 13 | int count = 0; 14 | for (int i = 0; i < 32; i++) { 15 | int mask = 1 << i; 16 | if ((mask & a) == mask) { 17 | count = count + 1; 18 | } 19 | } 20 | return count; 21 | } 22 | 23 | public static int numSetBitsBetter(long a) { 24 | int count = 0; 25 | while (a != 0) { 26 | count = count + 1; 27 | a = a & (a - 1); 28 | } 29 | return count; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming4/MinJump.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming4; 2 | 3 | import java.util.*; 4 | 5 | public class MinJump { 6 | 7 | public static void main(String[] args) { 8 | int[] arr = { 3, 4, 0, 1, 2, 0, 3, 1, 0, 2, 1 }; 9 | int target = 10; 10 | int ans = minJump(arr, target); 11 | System.out.println(ans); 12 | } 13 | 14 | public static int minJump(int[] arr, int target) { 15 | Integer[] ans = new Integer[arr.length + 1]; 16 | 17 | ans[arr.length] = 0; 18 | for (int i = ans.length - 2; i >= 0; i--) { 19 | for (int j = i + 1; j <= i + arr[i] && j < ans.length; j++) { 20 | if (ans[j] != null) { 21 | if (ans[i] == null || ans[j] + 1 < ans[i]) { 22 | ans[i] = ans[j] + 1; 23 | } 24 | } 25 | } 26 | } 27 | return ans[0]; 28 | } 29 | } -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/math/PrimeSum.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.math; 2 | 3 | import java.util.*; 4 | 5 | public class PrimeSum { 6 | 7 | public static void main(String[] args) { 8 | int n = 40; 9 | System.out.println(primeSum(n)); 10 | } 11 | 12 | public static ArrayList primeSum(int A) { 13 | ArrayList ans = new ArrayList(); 14 | int l = 2; 15 | int r = A - 2; 16 | while (l <= r) { 17 | if (isPrime(l) && isPrime(r)) { 18 | ans.add(l); 19 | ans.add(r); 20 | break; 21 | } 22 | l++; 23 | r--; 24 | } 25 | return ans; 26 | } 27 | 28 | private static boolean isPrime(int a) { 29 | 30 | int i = 2; 31 | while ((i * i) <= a) { 32 | if (a % i == 0) { 33 | return false; 34 | } 35 | i++; 36 | } 37 | return true; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /coreJava/recursion/memory/SubsetWithTargetSumArrayList.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.memory; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class SubsetWithTargetSumArrayList { 6 | 7 | public static void main(String[] args) { 8 | int[] arr = { 10, 20, 30, 40, 50 }; 9 | int target = 50; 10 | subsetWithTargetSumAL(arr, target, 0, 0, new ArrayList<>()); 11 | } 12 | 13 | public static void subsetWithTargetSumAL(int[] arr, int target, int vidx, int sum, ArrayList ans) { 14 | if (vidx == arr.length || sum >= target) { 15 | if (target == sum) { 16 | System.out.println(ans); 17 | } 18 | return; 19 | } 20 | 21 | ans.add(arr[vidx]); 22 | subsetWithTargetSumAL(arr, target, vidx + 1, sum + arr[vidx], ans); 23 | ans.remove(ans.size() - 1); 24 | subsetWithTargetSumAL(arr, target, vidx + 1, sum, ans); 25 | } 26 | } -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/FirstMissingInteger.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class FirstMissingInteger { 6 | 7 | public static void main(String[] args) { 8 | 9 | ArrayList al = new ArrayList(); 10 | al.add(-7); 11 | al.add(3); 12 | al.add(2); 13 | al.add(9); 14 | 15 | int ans = firstMissingPositive(al); 16 | System.out.println(ans); 17 | 18 | } 19 | 20 | public static int firstMissingPositive(ArrayList A) { 21 | int sum = 0; 22 | int count = 0; 23 | 24 | for (int val : A) { 25 | if (val > 0 && val < (A.size() + 1)) { 26 | sum = sum + val; 27 | count = count + 1; 28 | } 29 | } 30 | count = count + 1; 31 | int totalSum = (count * (count + 1)) / 2; 32 | return totalSum - sum; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming4/UnboundedKnapsack.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming4; 2 | 3 | public class UnboundedKnapsack { 4 | 5 | public static void main(String[] args) { 6 | int[] wt = { 2, 5, 6, 3, 4 }; 7 | int[] price = { 15, 14, 150, 45, 30 }; 8 | int target = 7; 9 | int ans = unboundedKnapsack(wt, price, target); 10 | System.out.println(ans); 11 | 12 | } 13 | 14 | public static int unboundedKnapsack(int[] wt, int[] price, int target) { 15 | int[] ans = new int[target + 1]; 16 | 17 | int max = Integer.MIN_VALUE; 18 | for (int i = 0; i < ans.length; i++) { 19 | for (int j = 0; j < wt.length; j++) { 20 | if (i >= wt[j]) { 21 | ans[i] = Math.max(ans[i], ans[i - wt[j]] + price[j]); 22 | } 23 | } 24 | max = Math.max(max, ans[i]); 25 | } 26 | return max; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/RotateAnArray.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays; 2 | 3 | public class RotateAnArray { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 7 | int k = 3; 8 | rotateAnArray(arr, k); 9 | for (int val : arr) { 10 | System.out.print(val + " "); 11 | } 12 | } 13 | 14 | public static void rotateAnArray(int[] arr, int k) { 15 | reverse(arr, 0, arr.length - k - 1); 16 | reverse(arr, arr.length - k, arr.length-1); 17 | reverse(arr, 0, arr.length - 1); 18 | } 19 | 20 | private static void reverse(int[] arr, int left, int right) { 21 | while (left <= right) { 22 | swap(arr, left, right); 23 | left++; 24 | right--; 25 | } 26 | } 27 | 28 | private static void swap(int[] arr, int i, int j) { 29 | int temp = arr[i]; 30 | arr[i] = arr[j]; 31 | arr[j] = temp; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /dataStructures/linkedlist/Client.java: -------------------------------------------------------------------------------- 1 | package dataStructures.linkedlist; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) throws Exception { 6 | 7 | LinkedListG ll = new LinkedListG(); 8 | ll.addLast(10); 9 | ll.addLast(20); 10 | ll.addLast(30); 11 | ll.addLast(40); 12 | ll.addLast(50); 13 | ll.display(); 14 | 15 | // ll.addFirst(5); 16 | // ll.display(); 17 | // 18 | // System.out.println(ll.getFirst()); 19 | // System.out.println(ll.getLast()); 20 | // System.out.println(ll.getAt(4)); 21 | // 22 | // ll.addAt(35, 4); 23 | // ll.display(); 24 | // 25 | // System.out.println(ll.removeFirst()); 26 | // ll.display(); 27 | // System.out.println(ll.removeLast()); 28 | // ll.display(); 29 | // System.out.println(ll.removeAt(3)); 30 | // ll.display(); 31 | 32 | 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/math/PalindromeInteger.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.math; 2 | 3 | public class PalindromeInteger { 4 | 5 | public static void main(String[] args) { 6 | int ans = isPalindrome(121); 7 | System.out.println(ans); 8 | } 9 | 10 | public static int isPalindrome(int A) { 11 | int org = A; 12 | 13 | boolean flag = false; 14 | if (A < 0) { 15 | flag = true; 16 | A = -A; 17 | } 18 | int rev = 0; 19 | while (A != 0) { 20 | int rem = A % 10; 21 | if (rev > Integer.MAX_VALUE / 10 || ((rev == Integer.MAX_VALUE / 10) && rem > Integer.MAX_VALUE % 10)) { 22 | return 0; 23 | } 24 | rev = rev * 10 + rem; 25 | A = A / 10; 26 | } 27 | 28 | if (flag == true) { 29 | rev = -rev; 30 | } 31 | 32 | if (org - rev == 0) { 33 | return 1; 34 | } 35 | return 0; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/WaveArray.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class WaveArray { 7 | 8 | public static void main(String[] args) { 9 | ArrayList al = new ArrayList<>(); 10 | al.add(1); 11 | al.add(5); 12 | al.add(3); 13 | al.add(2); 14 | al.add(4); 15 | //al.add(6); 16 | 17 | al = wave(al); 18 | for (int val : al) { 19 | System.out.print(val + " "); 20 | } 21 | } 22 | 23 | public static ArrayList wave(ArrayList A) { 24 | ArrayList ans = new ArrayList<>(); 25 | Collections.sort(A); 26 | for (int i = 0; i < A.size(); i = i + 2) { 27 | if ((i + 1) < A.size()) { 28 | ans.add(A.get(i + 1)); 29 | } 30 | ans.add(A.get(i)); 31 | } 32 | return ans; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming3/LongestIncressingSubSequence.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming3; 2 | 3 | public class LongestIncressingSubSequence { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 10, 22, 9, 33, 21, 50, 41, 60, 80, 1 }; 8 | int ans = longestIncressingSubSequence(arr); 9 | System.out.println(ans); 10 | } 11 | 12 | public static int longestIncressingSubSequence(int[] arr) { 13 | int[] ans = new int[arr.length]; 14 | 15 | ans[0] = 1; 16 | int omax = 0; 17 | for (int i = 0; i < arr.length; i++) { 18 | ans[i] = 1; 19 | for (int j = 0; j < i; j++) { 20 | if (arr[i] > arr[j]) { 21 | ans[i] = Math.max(ans[i], ans[j] + 1); 22 | } 23 | } 24 | omax = Math.max(omax, ans[i]); 25 | } 26 | 27 | return omax; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming3/EggDrop.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming3; 2 | 3 | public class EggDrop { 4 | 5 | public static void main(String[] args) { 6 | int egg = 2; 7 | int floor = 10; 8 | int ans = eggDrop(egg, floor, new int[egg + 1][floor + 1]); 9 | System.out.println(ans); 10 | } 11 | 12 | public static int eggDrop(int egg, int floor, int[][] m) { 13 | 14 | if (floor == 0 || floor == 1 || egg == 1) { 15 | return floor; 16 | } 17 | 18 | if (m[egg][floor] != 0) { 19 | return m[egg][floor]; 20 | } 21 | 22 | int min = Integer.MAX_VALUE; 23 | for (int x = 1; x <= floor; x++) { 24 | int res = Math.max(eggDrop(egg, floor - x, m), eggDrop(egg - 1, x - 1, m)); 25 | if (res < min) { 26 | min = res; 27 | } 28 | } 29 | 30 | m[egg][floor] = min + 1; 31 | return min + 1; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /coreJava/recursion/StartRecursion/TowerOfHanoi.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.StartRecursion; 2 | 3 | public class TowerOfHanoi { 4 | 5 | public static void main(String[] args) { 6 | toh("S", "D", "H", 3); 7 | //int ans = tohCount("S", "D", "H", 3); 8 | //System.out.println(ans); 9 | } 10 | 11 | public static void toh(String src, String des, String help, int n) { 12 | if (n == 0) { 13 | return; 14 | } 15 | toh(src, help, des, n - 1); 16 | System.out.println(src + " to " + des + " --->>> " + n); 17 | toh(help, des, src, n - 1); 18 | } 19 | 20 | public static int tohCount(String src, String des, String help, int n) { 21 | if (n == 0) { 22 | return 0; 23 | } 24 | int steps = tohCount(src, help, des, n - 1); 25 | System.out.println(src + " to " + des + " --->>> " + n); 26 | steps = steps + tohCount(help, des, src, n - 1); 27 | return steps + 1; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /problemSolution/way/jan2019/Balanced.java: -------------------------------------------------------------------------------- 1 | package problemSolution.way.jan2019; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Balanced { 6 | 7 | public static void main(String[] args) { 8 | Scanner scn = new Scanner(System.in); 9 | //String s = scn.next(); 10 | String s = ")("; 11 | 12 | int check = 0 ; 13 | boolean flag = false ; 14 | for(int i=0 ; i< s.length() ; i++){ 15 | if(s.charAt(i) == '('){ 16 | check +=1; 17 | }else { 18 | check -=1; 19 | } 20 | if(!flag && check==-1){ 21 | check +=1; 22 | flag = true; 23 | } 24 | if(check < -1){ 25 | System.out.println("No"); 26 | return ; 27 | } 28 | } 29 | if(flag && check==0){ 30 | System.out.println("Yes"); 31 | }else if(!flag && (check==0 || check==-1 || check==1)){ 32 | System.out.println("Yes"); 33 | } 34 | else{ 35 | System.out.println("No"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level3/bitManipulation/SingleNumberII.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level3.bitManipulation; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class SingleNumberII { 6 | 7 | public static void main(String[] args) { 8 | ArrayList A = new ArrayList<>(); 9 | A.add(1); 10 | A.add(3); 11 | A.add(1); 12 | A.add(3); 13 | A.add(5); 14 | A.add(1); 15 | A.add(3); 16 | int ans = singleNumberII(A); 17 | System.out.println(ans); 18 | } 19 | 20 | public static int singleNumberII(final ArrayList A) { 21 | int ans = 0; 22 | 23 | for (int i = 0; i < 32; i++) { 24 | int count = 0; 25 | int x = 1 << i; 26 | 27 | for (int val : A) { 28 | if ((val & x) == x) { 29 | count++; 30 | } 31 | } 32 | 33 | count = count % 3; 34 | if (count == 1) { 35 | ans = ans | x; 36 | } 37 | } 38 | 39 | return ans; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /coreJava/recursion/arrayList/SubSequenceAscii.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.arrayList; 2 | 3 | import java.util.*; 4 | 5 | public class SubSequenceAscii { 6 | 7 | public static void main(String[] args) { 8 | ArrayList ans = subSequenceAscii("bc"); 9 | System.out.println(ans); 10 | 11 | } 12 | 13 | public static ArrayList subSequenceAscii(String input) { 14 | 15 | if (input.length() == 0){ 16 | ArrayList news = new ArrayList<>(); 17 | news.add(""); 18 | return news; 19 | } 20 | 21 | char first= input.charAt(0); 22 | String nw= input.substring(1); 23 | 24 | ArrayList ans = new ArrayList(); 25 | ans = subSequenceAscii(nw); 26 | 27 | ArrayList fans = new ArrayList(); 28 | for(String str:ans){ 29 | fans.add(str); 30 | fans.add(first+str); 31 | fans.add((int)first+str); 32 | 33 | } 34 | return fans; 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /coreJava/string/PrintPalindromeSubstring.java: -------------------------------------------------------------------------------- 1 | package coreJava.string; 2 | 3 | public class PrintPalindromeSubstring { 4 | 5 | public static void main(String[] args) { 6 | String input = "naman"; 7 | printPalindromeSubstring(input); 8 | } 9 | 10 | public static void printPalindromeSubstring(String input) { 11 | for (int i = 0; i < input.length(); i++) { 12 | for (int j = i + 1; j <= input.length(); j++) { 13 | String substring = input.substring(i, j); 14 | boolean check = isPalindrome(substring); 15 | if (check) { 16 | System.out.println(substring); 17 | } 18 | } 19 | } 20 | } 21 | 22 | private static boolean isPalindrome(String substring) { 23 | int left = 0; 24 | int right = substring.length() - 1; 25 | while (left < right) { 26 | if (substring.charAt(left) != substring.charAt(right)) { 27 | return false; 28 | } 29 | left++; 30 | right--; 31 | } 32 | return true; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming2/TotalPalndromInString.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming2; 2 | 3 | public class TotalPalndromInString { 4 | 5 | public static void main(String[] args) { 6 | 7 | String ques = "abccbc"; 8 | tps(ques); 9 | } 10 | 11 | public static void tps(String ques) { 12 | 13 | boolean[][] ans = new boolean[ques.length()][ques.length()]; 14 | 15 | for (int gap = 0; gap < ques.length(); gap++) { 16 | for (int i = 0; i + gap < ans.length; i++) { 17 | 18 | int j = i + gap; 19 | 20 | if (gap == 0) { 21 | ans[i][j] = true; 22 | } else if (gap == 1) { 23 | ans[i][j] = ques.charAt(i) == ques.charAt(j); 24 | } else { 25 | ans[i][j] = (ques.charAt(i) == ques.charAt(j)) && (ans[i + 1][j - 1] == true); 26 | } 27 | 28 | if (ans[i][j] == true) { 29 | System.out.println(ques.substring(i, j + 1)); 30 | } 31 | } 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /coreJava/recursion/Array/FindFirstIndex.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.Array; 2 | 3 | public class FindFirstIndex { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 5, 44, 9, 1, 44, 18 }; 7 | int ans = findFirstIndex(arr, 44, 0); 8 | System.out.println(ans); 9 | int ans2 = findFirstIndexBetter(arr, 44, 0); 10 | System.out.println(ans2); 11 | } 12 | 13 | public static int findFirstIndex(int[] arr, int data, int vidx) { 14 | if (vidx == arr.length) { 15 | return -1; 16 | } 17 | 18 | int ans = findFirstIndex(arr, data, vidx + 1); 19 | if (data == arr[vidx]) { 20 | return vidx; 21 | } else { 22 | return ans; 23 | } 24 | } 25 | 26 | public static int findFirstIndexBetter(int[] arr, int data, int vidx) { 27 | if (vidx == arr.length) { 28 | return -1; 29 | } 30 | if (data == arr[vidx]) { 31 | return vidx; 32 | } 33 | int ans = findFirstIndexBetter(arr, data, vidx + 1); 34 | return ans; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /coreJava/recursion/memory/SubsetsWithEqualSumArrayList.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.memory; 2 | 3 | import java.util.*; 4 | 5 | public class SubsetsWithEqualSumArrayList { 6 | 7 | public static void main(String[] args) { 8 | int[] arr = { 10, 20, 30, 40, 50, 60, 70 }; 9 | subsetsWithEqualSumAL(arr, 0, 0, new ArrayList<>(), 0, new ArrayList<>()); 10 | } 11 | 12 | public static void subsetsWithEqualSumAL(int[] arr, int vidx, int sum1, ArrayList set1, int sum2, 13 | ArrayList set2) { 14 | 15 | if (arr.length == vidx) { 16 | if (sum1 == sum2) { 17 | System.out.println(set1 + " & " + set2); 18 | } 19 | return; 20 | } 21 | set1.add(arr[vidx]); 22 | subsetsWithEqualSumAL(arr, vidx + 1, sum1 + arr[vidx], set1, sum2, set2); 23 | set1.remove(set1.size() - 1); 24 | 25 | set2.add(arr[vidx]); 26 | subsetsWithEqualSumAL(arr, vidx + 1, sum1, set1, sum2 + arr[vidx], set2); 27 | set2.remove(set2.size() - 1); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /coreJava/greedy/greedy1/MinimumStationRequired.java: -------------------------------------------------------------------------------- 1 | package coreJava.greedy.greedy1; 2 | 3 | import java.util.*; 4 | 5 | public class MinimumStationRequired { 6 | 7 | public static void main(String[] args) { 8 | int[] arrivals = { 900, 940, 950, 1100, 1500, 1800 }; 9 | int[] departures = { 910, 1200, 1120, 1130, 1900, 2000 }; 10 | int ans = minimumStationRequired(arrivals, departures); 11 | System.out.println(ans); 12 | 13 | } 14 | 15 | public static int minimumStationRequired(int[] arrivals, int[] departures) { 16 | 17 | Arrays.sort(arrivals); 18 | Arrays.sort(departures); 19 | int i = 0; 20 | int j = 0; 21 | int cars = 0; 22 | 23 | int max = Integer.MIN_VALUE; 24 | while (i < arrivals.length && j < departures.length) { 25 | if (arrivals[i] < departures[j]) { 26 | cars++; 27 | i++; 28 | } else { 29 | cars--; 30 | j++; 31 | } 32 | 33 | if (max < cars) { 34 | max = cars; 35 | } 36 | } 37 | 38 | return max; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/MaxSumContiguousSubarray.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class MaxSumContiguousSubarray { 6 | 7 | public static void main(String[] args) { 8 | 9 | List l = new ArrayList<>(); 10 | l.add(-11);// -2,1,-3,4,-1,2,1,-5,4 11 | // l.add(1); 12 | // l.add(-3); 13 | // l.add(4); 14 | // l.add(-1); 15 | // l.add(2); 16 | // l.add(1); 17 | // l.add(-5); 18 | l.add(-3); 19 | 20 | int ans = maxSubArray(l); 21 | System.out.println(ans); 22 | 23 | } 24 | 25 | public static int maxSubArray(final List A) { 26 | int current = A.get(0); 27 | int total = A.get(0); 28 | 29 | for (int i = 1; i < A.size(); i++) { 30 | if (current < 0) { 31 | current = A.get(i); 32 | } else { 33 | current = current + A.get(i); 34 | } 35 | 36 | if (current > total) { 37 | total = current; 38 | } 39 | } 40 | 41 | return total; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /problemSolution/interview_prepMY/array/K_Concatenation.java: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/JAN18/problems/KCON/ 2 | 3 | package problemSolution.interview_prepMY.array; 4 | 5 | import java.util.*; 6 | 7 | public class K_Concatenation { 8 | 9 | public static void main(String[] args) { 10 | Scanner s = new Scanner(System.in); 11 | int T = s.nextInt(); 12 | while (T != 0) { 13 | 14 | int n = s.nextInt(); 15 | int k = s.nextInt(); 16 | int[] arr = new int[n]; 17 | for (int i = 0; i < n; i++) { 18 | arr[i] = s.nextInt(); 19 | } 20 | System.out.println(K_Con(arr, k)); 21 | T--; 22 | } 23 | 24 | } 25 | 26 | public static int K_Con(int[] arr, int k) { 27 | int m = arr.length * k; 28 | 29 | int max = Integer.MIN_VALUE; 30 | int tmax = arr[0]; 31 | 32 | for (int i = 1; i < m; i++) { 33 | tmax = tmax + arr[i % arr.length]; 34 | if (tmax <= 0) { 35 | tmax = 0; 36 | } 37 | 38 | if(max < tmax){ 39 | max = tmax; 40 | } 41 | } 42 | 43 | return max; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming1/MinCostPath.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming1; 2 | 3 | public class MinCostPath { 4 | 5 | public static void main(String[] args) { 6 | int[][] arr = { { 1, 2, 5, 3, 5 }, { 4, 2, 2, 6, 0 }, { 0, 8, 1, 1, 4 }, { 4, 0, 2, 5, 100 }, 7 | { 0, 2, 6, 7, 1 } }; 8 | System.out.println(minCostPath(arr)); 9 | } 10 | 11 | public static int minCostPath(int[][] arr) { 12 | int[][] ans = new int[arr.length][arr[0].length]; 13 | 14 | for (int i = arr.length - 1; i >= 0; i--) { 15 | for (int j = arr[0].length - 1; j >= 0; j--) { 16 | if (i == arr.length - 1 && j == arr[0].length - 1) { 17 | ans[i][j] = arr[i][j]; 18 | } else if (i == arr.length - 1) { 19 | ans[i][j] = arr[i][j] + ans[i][j + 1]; 20 | } else if (j == arr[0].length - 1) { 21 | ans[i][j] = arr[i][j] + ans[i + 1][j]; 22 | } else { 23 | ans[i][j] = arr[i][j] + Math.min(ans[i][j + 1], ans[i + 1][j]); 24 | } 25 | } 26 | } 27 | 28 | return ans[0][0]; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /coreJava/greedy/greedy1/FractionalKnapsack.java: -------------------------------------------------------------------------------- 1 | package coreJava.greedy.greedy1; 2 | 3 | import java.util.*; 4 | 5 | public class FractionalKnapsack { 6 | 7 | public static void main(String[] args) { 8 | int[] wts = { 10, 40, 20, 30 }; 9 | int[] price = { 60, 40, 100, 120 }; 10 | int target = 50; 11 | fractionalKnapsack(wts, price, target); 12 | } 13 | 14 | public static void fractionalKnapsack(int[] wts, int[] price, int target) { 15 | Item[] items = new Item[wts.length]; 16 | 17 | for (int i = 0; i < items.length; i++) { 18 | items[i] = new Item(); 19 | items[i].wt = wts[i]; 20 | items[i].val = price[i]; 21 | items[i].ratio = price[i] * 1.0 / wts[i]; 22 | } 23 | 24 | Arrays.sort(items); 25 | int rc = target; 26 | int value = 0; 27 | int i = items.length - 1; 28 | while (rc != 0) { 29 | if (rc >= items[i].wt) { 30 | rc -= items[i].wt; 31 | value += items[i].val; 32 | i--; 33 | } else { 34 | value += rc * items[i].ratio; 35 | rc = 0; 36 | } 37 | } 38 | System.out.println(value); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /coreJava/arrays/twoD/SprialDisplay.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.twoD; 2 | 3 | public class SprialDisplay { 4 | 5 | public static void main(String[] args) { 6 | int[][] arr = { { 11, 12, 13, 14 }, { 21, 22, 23, 24 }, { 31, 32, 33, 34 }, { 41, 42, 43, 44 } }; 7 | sprialDisplay(arr); 8 | } 9 | 10 | public static void sprialDisplay(int[][] arr) { 11 | int rMin = 0; 12 | int cMin = 0; 13 | int rMax = arr.length - 1; 14 | int cMax = arr[0].length - 1; 15 | int count = arr.length * arr[0].length; 16 | 17 | while (count > 0) { 18 | 19 | for (int i = rMin; i <= rMax; i++) { 20 | System.out.print(arr[i][cMin]); 21 | count--; 22 | } 23 | cMin++; 24 | for (int i = cMin; i <= cMax; i++) { 25 | System.out.print(arr[rMax][i]); 26 | count--; 27 | } 28 | rMax--; 29 | for (int i = rMax; i <= rMin; i++) { 30 | System.out.print(arr[i][cMax]); 31 | count--; 32 | } 33 | cMax--; 34 | for (int i = cMax; i <= cMin; i++) { 35 | System.out.print(arr[rMin][i]); 36 | count--; 37 | } 38 | rMin++; 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /coreJava/recursion/arrayList/SubSequence.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.arrayList; 2 | 3 | import java.util.*; 4 | 5 | public class SubSequence { 6 | 7 | public static void main(String[] args) { 8 | // ArrayList ans = subSequence("abc"); 9 | // System.out.println(ans); 10 | 11 | ArrayList ans = getKPC("123"); 12 | System.out.println(ans); 13 | } 14 | 15 | public static ArrayList subSequence(String input) { 16 | 17 | if (input.length() == 0) { 18 | ArrayList ans = new ArrayList<>(); 19 | ans.add(""); 20 | System.out.println("end"); 21 | return ans; 22 | } 23 | 24 | char ch = input.charAt(0); 25 | String subString = input.substring(1); 26 | ArrayList ansS = subSequence(subString); 27 | ArrayList ans = new ArrayList<>(); 28 | for (String val : ansS) { 29 | ans.add(val); 30 | ans.add(ch + val); 31 | } 32 | return ans; 33 | } 34 | 35 | static String[] code = { ".", "abc", "def", "ghi", "jkl", "mno", "pqr", "st", "uvwx", "yz" }; 36 | 37 | public static ArrayList getKPC(String str) { 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/PascalTriangle.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class PascalTriangle { 6 | 7 | public static void main(String[] args) { 8 | 9 | int n = 4; 10 | ArrayList> ans = solve(n); 11 | for (int i = 0; i < ans.size(); i++) { 12 | for (int j = 0; j < ans.get(i).size(); j++) { 13 | System.out.print(ans.get(i).get(j) + " "); 14 | } 15 | System.out.println(); 16 | } 17 | 18 | } 19 | 20 | public static ArrayList> solve(int n) { 21 | 22 | ArrayList> ans = new ArrayList>(); 23 | 24 | for (int i = 0; i < n; i++) { 25 | ArrayList list = new ArrayList<>(); 26 | for (int j = 0; j <= i; j++) { 27 | list.add(0); 28 | } 29 | ans.add(list); 30 | } 31 | 32 | for (int i = 0; i < n; i++) { 33 | int val = 1 ; 34 | for (int j = 0; j <= i; j++) { 35 | ans.get(i).set(j, val); 36 | val = ( val * (i-j)) / (j+1); 37 | } 38 | } 39 | return ans; 40 | } 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/NextPermutation.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class NextPermutation { 6 | 7 | public static void main(String[] args) { 8 | 9 | ArrayList al = new ArrayList<>(); 10 | al.add(27); 11 | al.add(18); 12 | al.add(1); 13 | 14 | System.out.println(al); 15 | nextPermutation(al); 16 | System.out.println(al); 17 | } 18 | 19 | public static void nextPermutation(ArrayList a) { 20 | 21 | for (int i = a.size() - 1; i > 0; i--) { 22 | if (a.get(i) > a.get(i - 1)) { 23 | 24 | int k = i; 25 | while (k < a.size() - 1) { 26 | for (int m = a.size() - 1; m > k; m--) { 27 | if (a.get(m) < a.get(m-1)) { 28 | swap(a, k, k + 1); 29 | } 30 | } 31 | } 32 | 33 | swap(a, i, i - 1); 34 | 35 | return; 36 | } 37 | } 38 | swap(a, 0, a.size() - 1); 39 | } 40 | 41 | private static void swap(ArrayList a, int i, int j) { 42 | int temp1 = a.get(i); 43 | int temp2 = a.get(j); 44 | a.set(i, temp2); 45 | a.set(j, temp1); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Sandip Kumar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /coreJava/arrays/twoD/MatrixMultiplication.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.twoD; 2 | 3 | public class MatrixMultiplication { 4 | 5 | public static void main(String[] args) { 6 | int[][] one = { { 10, 0, 0 }, { 0, 5, 1 } }; 7 | int[][] two = { { 0, 5, 10, 2 }, { 20, 0, 1, 2 }, { 1, 1, 1, 53 } }; 8 | matrixMultiplication(one, two); 9 | } 10 | 11 | public static void matrixMultiplication(int[][] one, int[][] two) { 12 | int r1 = one.length; 13 | int c1 = one[0].length; 14 | int r2 = two.length; 15 | int c2 = two[0].length; 16 | 17 | if (c1 != r2) { 18 | System.out.println("Matrix Multiplication Not possible "); 19 | return; 20 | } 21 | int[][] ans = new int[r1][c2]; 22 | for (int i = 0; i < r1; i++) { 23 | for (int j = 0; j < c2; j++) { 24 | for (int k = 0; k < r1; k++) { 25 | ans[i][j] += one[i][k] * two[k][j]; 26 | } 27 | } 28 | } 29 | display(ans); 30 | } 31 | 32 | private static void display(int[][] ans) { 33 | for (int i = 0; i < ans.length; i++) { 34 | for (int j = 0; j < ans[0].length; j++) { 35 | System.out.print(ans[i][j] + " "); 36 | } 37 | System.out.println(); 38 | } 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/LargestNumber.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class LargestNumber { 6 | 7 | public static void main(String[] args) { 8 | List l = new ArrayList<>(); 9 | l.add(3); 10 | l.add(30); 11 | l.add(34); 12 | l.add(5); 13 | l.add(9); 14 | l.add(0); 15 | l.add(8); 16 | 17 | System.out.println(largestNumber(l)); 18 | } 19 | 20 | public static String largestNumber(final List a) { 21 | List strs = new ArrayList(); 22 | for (Integer i : a) { 23 | strs.add(String.valueOf(i)); 24 | } 25 | 26 | Collections.sort(strs, new MyCompartor()); 27 | 28 | StringBuffer ans = new StringBuffer(); 29 | for (String s : strs) { 30 | if ("0".equals(s) && ans.length() != 0) { 31 | continue; 32 | } 33 | ans.append(s); 34 | } 35 | 36 | return ans.toString(); 37 | } 38 | 39 | } 40 | 41 | class MyCompartor implements Comparator { 42 | @Override 43 | public int compare(String i, String j) { 44 | String temp1 = i + j; 45 | String temp2 = j + i; 46 | return temp2.compareTo(temp1); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/FindDuplicateinArray.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class FindDuplicateinArray { 6 | 7 | public static void main(String[] args) { 8 | 9 | List l = new ArrayList<>(); 10 | l.add(3); 11 | l.add(4); 12 | l.add(2); 13 | l.add(1); 14 | int ans = repeatedNumber(l); 15 | System.out.println(ans); 16 | 17 | } 18 | 19 | public static int repeatedNumber(final List a) { 20 | int ans = 0; 21 | for (int i = 0; i < a.size(); i++) { 22 | ans = ans ^ a.get(i) ^ (i + 1); 23 | } 24 | 25 | if (ans == 0) { 26 | return -1; 27 | } 28 | int mask = ans & (-ans); 29 | int ansL = 0, ansR = 0; 30 | for (int i = 0; i < a.size(); i++) { 31 | if ((a.get(i) & mask) == mask) { 32 | ansL = ansL ^ a.get(i); 33 | } else { 34 | ansR = ansR ^ a.get(i); 35 | } 36 | if (((i + 1) & mask) == mask) { 37 | ansL = ansL ^ (i + 1); 38 | } else { 39 | ansR = ansR ^ (i + 1); 40 | } 41 | } 42 | 43 | for (int i : a) { 44 | if ((i ^ ansL) == 0) { 45 | return ansL; 46 | } 47 | } 48 | return ansR; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/AddOneToNumber.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class AddOneToNumber { 6 | 7 | public static void main(String[] args) { 8 | ArrayList al = new ArrayList<>(); 9 | al.add(0); 10 | al.add(9); 11 | al.add(9); 12 | al.add(9); 13 | al.add(9); 14 | al = plusOne(al); 15 | for (int val : al) { 16 | System.out.print(val + " "); 17 | } 18 | } 19 | 20 | public static ArrayList plusOne(ArrayList A) { 21 | ArrayList al = new ArrayList<>(); 22 | 23 | int current = 1; 24 | for (int i = A.size() - 1; i >= 0; i--) { 25 | current = current + A.get(i); 26 | if (current / 10 != 0) { 27 | A.set(i, (current % 10)); 28 | current = current / 10; 29 | } else { 30 | A.set(i, current); 31 | current = current / 10; 32 | break; 33 | } 34 | } 35 | 36 | if (current != 0) { 37 | A.add(0, current); 38 | } 39 | 40 | for (int i = 0; i < A.size(); i++) { 41 | current = A.get(i); 42 | if (current != 0) { 43 | break; 44 | } else { 45 | A.remove(i); 46 | i--; 47 | } 48 | } 49 | return A; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/Flip.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class Flip { 6 | 7 | public static void main(String[] args) { 8 | String s = "0111000100010"; 9 | ArrayList ans = flip(s); 10 | for (int val : ans) { 11 | System.out.print(val + " "); 12 | } 13 | } 14 | 15 | public static ArrayList flip(String A) { 16 | 17 | int L = -1; 18 | int R = -1; 19 | int Ltemp = 0; 20 | int Rtemp = 0; 21 | int diffLR = 0; 22 | int zero = 0, one = 0; 23 | for (int i = 0; i < A.length(); i++) { 24 | if (A.charAt(i) == '0') { 25 | zero++; 26 | } else { 27 | one++; 28 | } 29 | 30 | if (zero < one) { 31 | Ltemp = i + 1; 32 | Rtemp = i + 1; 33 | zero = 0 ; 34 | one = 0 ; 35 | } else { 36 | if ((zero - one) > diffLR) { 37 | diffLR = zero - one; 38 | L = Ltemp; 39 | R = Rtemp; 40 | Rtemp++; 41 | } else { 42 | Rtemp++; 43 | } 44 | } 45 | } 46 | 47 | if (L >= 0) { 48 | ArrayList ans = new ArrayList<>(); 49 | ans.add(L+1); 50 | ans.add(R+1); 51 | return ans ; 52 | } 53 | return new ArrayList(); 54 | } 55 | } -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming1/GoldMine.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming1; 2 | 3 | public class GoldMine { 4 | 5 | public static void main(String[] args) { 6 | int[][] arr = { { 2, 1, 3, 10, 2 }, { 6, 0, 8, 1, 8 }, { 3, 9, 5, 6, 7 }, { 4, 2, 4, 7, 4 }, 7 | { 1, 5, 0, 2, 3 } }; 8 | System.out.println(goldMine(arr)); 9 | } 10 | 11 | public static int goldMine(int[][] arr) { 12 | 13 | int[][] ans = new int[arr.length][arr[0].length]; 14 | 15 | for (int j = arr[0].length - 1; j >= 0; j--) { 16 | for (int i = arr.length - 1; i >= 0; i--) { 17 | if (j == arr[0].length - 1) { 18 | ans[i][j] = arr[i][j]; 19 | } else if (i == arr.length - 1) { 20 | ans[i][j] = arr[i][j] + Math.max(ans[i - 1][j + 1], ans[i][j + 1]); 21 | } else if (i == 0) { 22 | ans[i][j] = arr[i][j] + Math.max(ans[i][j + 1], ans[i + 1][j + 1]); 23 | } else { 24 | ans[i][j] = arr[i][j] + Math.max(ans[i - 1][j + 1], Math.max(ans[i][j + 1], ans[i + 1][j + 1])); 25 | } 26 | } 27 | } 28 | int max = ans[0][0]; 29 | for (int i = 1; i < arr[0].length; i++) { 30 | if (ans[i][0] > max) { 31 | max = ans[i][0]; 32 | } 33 | } 34 | return max; 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming4/LongestBiotionicSubSequence.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming4; 2 | 3 | public class LongestBiotionicSubSequence { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 10, 22, 9, 33, 21, 50, 41, 60, 80, 1 }; 7 | int ans = longestBiotionicSubSequence(arr); 8 | System.out.println(ans); 9 | } 10 | 11 | private static int longestBiotionicSubSequence(int[] arr) { 12 | int[] anslis = new int[arr.length]; 13 | 14 | anslis[0] = 1; 15 | for (int i = 1; i < arr.length; i++) { 16 | anslis[i] = 1; 17 | for (int j = 0; j < i; j++) { 18 | if (arr[i] > arr[j]) { 19 | anslis[i] = Math.max(anslis[i], anslis[j] + 1); 20 | } 21 | } 22 | } 23 | 24 | int[] anslds = new int[arr.length]; 25 | for (int i = arr.length - 1; i >= 0; i--) { 26 | anslds[i] = 1; 27 | for (int j = i + 1; j < arr.length; j++) { 28 | if (arr[i] > arr[j]) { 29 | anslds[i] = Math.max(anslds[i], anslds[j] + 1); 30 | } 31 | } 32 | } 33 | 34 | int max = Integer.MIN_VALUE; 35 | for (int i = 0; i < arr.length; i++) { 36 | anslis[i] = anslis[i] + anslds[i] - 1; 37 | if (max < anslis[i]) { 38 | max = anslis[i]; 39 | } 40 | } 41 | return max; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level3/string/PalindromeString.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level3.string; 2 | 3 | public class PalindromeString { 4 | 5 | public static void main(String[] args) { 6 | String input = "1a2"; 7 | int ans = isPalindrome(input); 8 | System.out.println(ans); 9 | } 10 | 11 | public static int isPalindrome(String A) { 12 | int left = 0; 13 | int right = A.length() - 1; 14 | while (left < right) { 15 | char l = A.charAt(left); 16 | char r = A.charAt(right); 17 | if (((l >= 'a' && 'z' >= l) || (l >= 'A' && 'Z' >= l)) 18 | && ((r >= 'a' && 'z' >= r) || (r >= 'A' && 'Z' >= r))) { 19 | 20 | if (l != r) { 21 | if ((l >= 'a' && 'z' >= l)) { 22 | l = (char) (l + 'A' - 'a'); 23 | } else { 24 | l = (char) (l + 'a' - 'A'); 25 | } 26 | 27 | if (l != r) { 28 | return 0; 29 | } 30 | } 31 | left++; 32 | right--; 33 | } else if ((l >= 'a' && 'z' >= l) || (l >= 'A' && 'Z' >= l)) { 34 | right--; 35 | } else if ((r >= 'a' && 'z' >= r) || (r >= 'A' && 'Z' >= r)) { 36 | left++; 37 | } else if ((r >= 0 && 9 <= r) || (l >= 0 && 9 <= l)) { 38 | return 0; 39 | } else { 40 | left++; 41 | right--; 42 | } 43 | } 44 | return 1; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /coreJava/recursion/pAndC/Combinations.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.pAndC; 2 | 3 | public class Combinations { 4 | 5 | public static void main(String[] args) { 6 | boolean[] boxes = new boolean[4]; 7 | // combinations(boxes, 2, 0, -1, ""); 8 | combinationsUsingRecursion(4, 0, 2, 0, ""); 9 | } 10 | 11 | public static void combinations(boolean[] boxes, int titem, int citem, int lastbox, String asf) { 12 | 13 | if (titem == citem) { 14 | System.out.println(asf); 15 | return; 16 | } 17 | for (int i = lastbox + 1; i < boxes.length; i++) { 18 | if (!boxes[i]) { 19 | boxes[i] = true; 20 | combinations(boxes, titem, citem + 1, i, asf + "B" + i + "I" + (citem + 1) + " "); 21 | boxes[i] = false; 22 | } 23 | } 24 | } 25 | 26 | public static void combinationsUsingRecursion(int totalBox, int currentBox, int totalItem, int currentItem, 27 | String asf) { 28 | 29 | if (totalItem == currentItem) { 30 | System.out.println(asf); 31 | return; 32 | } 33 | 34 | if (totalBox == currentBox) { 35 | return; 36 | } 37 | 38 | combinationsUsingRecursion(totalBox, currentBox + 1, totalItem, currentItem + 1, 39 | asf + "q" + currentBox + "I" + currentItem + " "); 40 | combinationsUsingRecursion(totalBox, currentBox + 1, totalItem, currentItem, asf); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /problemSolution/Parenthesis.java: -------------------------------------------------------------------------------- 1 | package problemSolution; 2 | 3 | import java.util.*; 4 | 5 | public class Parenthesis { 6 | 7 | public static void main(String[] args) { 8 | ArrayList ans = new ArrayList(); 9 | String s = "311*63-1"; 10 | ans = Allpossible(s); 11 | for (int val : ans) { 12 | System.out.print(val + " "); 13 | } 14 | } 15 | 16 | public static ArrayList Allpossible(String input) { 17 | ArrayList ans = new ArrayList(); 18 | 19 | for (int i = 0; i < input.length(); i++) { 20 | char current = input.charAt(i); 21 | 22 | if (current == '-' || current == '+' || current == '*') { 23 | String left = input.substring(0, i); 24 | ArrayList leftList = Allpossible(left); 25 | 26 | String right = input.substring(i + 1); 27 | ArrayList rightList = Allpossible(right); 28 | 29 | // cartesian product 30 | for (int l : leftList) { 31 | for (int r : rightList) { 32 | if (current == '-') { 33 | ans.add(l - r); 34 | } else if (current == '+') { 35 | ans.add(l + r); 36 | } else if (current == '*') { 37 | ans.add(l * r); 38 | } 39 | } 40 | } 41 | } 42 | } 43 | if (ans.size() == 0) 44 | ans.add(Integer.valueOf(input)); 45 | 46 | return ans; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /coreJava/recursion/pAndC/Permutations.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.pAndC; 2 | 3 | public class Permutations { 4 | 5 | public static void main(String[] args) { 6 | boolean[] boxes = new boolean[4]; 7 | // permutations(boxes, 2, 0, ""); 8 | permutationUsingBitMasking(0, 4, 2, 0, ""); 9 | } 10 | 11 | public static void permutations(boolean[] boxes, int titem, int citem, String asf) { 12 | 13 | if (citem == titem) { 14 | System.out.println(asf); 15 | return; 16 | } 17 | for (int i = 0; i < boxes.length; i++) { 18 | if (!boxes[i]) { 19 | boxes[i] = true; 20 | permutations(boxes, titem, citem + 1, asf + "B" + i + "I" + (citem + 1) + " "); 21 | boxes[i] = false; 22 | } 23 | } 24 | } 25 | 26 | public static void permutationUsingBitMasking(int choicebm, int totalBox, int totalItems, int CurrentItem, 27 | String asf) { 28 | 29 | if (totalItems == CurrentItem) { 30 | System.out.println(Integer.toBinaryString(choicebm)); 31 | System.out.println(asf); 32 | return; 33 | } 34 | 35 | for (int i = 0; i < totalBox; i++) { 36 | if ((choicebm & (1 << i)) == 0) { 37 | choicebm = choicebm | (1 << i); 38 | permutationUsingBitMasking(choicebm, totalBox, totalItems, CurrentItem + 1, 39 | asf + "b" + (i) + "I" + (CurrentItem + 1) + " "); 40 | choicebm = choicebm & (~(1 << i)); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming1/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming1; 2 | 3 | public class Fibonacci { 4 | 5 | public static void main(String[] args) { 6 | int n = 10; 7 | int ansM = fibonacciMemoizatation(n, new int[n + 1]); 8 | System.out.println(ansM); 9 | 10 | int ansT = fibonacciTabulation(n); 11 | System.out.println(ansT); 12 | 13 | int ansS = fibonacciSilder(n); 14 | System.out.println(ansS); 15 | 16 | } 17 | 18 | public static int fibonacciMemoizatation(int n, int[] m) { 19 | if (n == 0 || n == 1) { 20 | return n; 21 | } 22 | 23 | if (m[n] != 0) { 24 | return m[n]; 25 | } 26 | 27 | int fm1 = fibonacciMemoizatation(n - 1, m); 28 | int fm2 = fibonacciMemoizatation(n - 2, m); 29 | int f = fm1 + fm2; 30 | 31 | m[n] = f; 32 | return f; 33 | } 34 | 35 | public static int fibonacciTabulation(int n) { 36 | int[] m = new int[n + 1]; 37 | m[0] = 0; 38 | m[1] = 1; 39 | 40 | for (int i = 2; i < m.length; i++) { 41 | m[i] = m[i - 1] + m[i - 2]; 42 | } 43 | 44 | return m[n]; 45 | } 46 | 47 | public static int fibonacciSilder(int n) { 48 | int[] s = new int[2]; 49 | 50 | s[0] = 0; 51 | s[1] = 1; 52 | 53 | for (int nos = 1; nos <= n; nos++) { 54 | int nf = s[0] + s[1]; 55 | s[0] = s[1]; 56 | s[1] = nf; 57 | } 58 | 59 | return s[0]; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/MergeSort.java: -------------------------------------------------------------------------------- 1 | package coreJava.arrays.oneD; 2 | 3 | public class MergeSort { 4 | 5 | public static void main(String[] args) { 6 | 7 | int[] arr = { 99, 45, 1, 555, 2, 13, 6, 19 }; 8 | int[] ans = mergeSort(arr, 0, arr.length - 1); 9 | for (int val : ans) { 10 | System.out.print(val + " "); 11 | } 12 | } 13 | 14 | public static int[] mergeSort(int[] arr, int left, int right) { 15 | 16 | if (left == right) { 17 | int[] bA = new int[1]; 18 | bA[0] = arr[left]; 19 | return bA; 20 | } 21 | 22 | int mid = (left + right) / 2; 23 | int[] leftA = mergeSort(arr, left, mid); 24 | int[] rightA = mergeSort(arr, mid + 1, right); 25 | int[] sorted = merge(leftA, rightA); 26 | return sorted; 27 | } 28 | 29 | private static int[] merge(int[] one, int[] two) { 30 | int[] ans = new int[one.length + two.length]; 31 | int oneCount = 0; 32 | int twoCount = 0; 33 | 34 | for (int i = 0; i < ans.length; i++) { 35 | if (oneCount == one.length) { 36 | ans[i] = two[twoCount]; 37 | twoCount++; 38 | } else if (twoCount == two.length) { 39 | ans[i] = one[oneCount]; 40 | oneCount++; 41 | } else { 42 | if (one[oneCount] < two[twoCount]) { 43 | ans[i] = one[oneCount]; 44 | oneCount++; 45 | } else { 46 | ans[i] = two[twoCount]; 47 | twoCount++; 48 | } 49 | } 50 | } 51 | return ans; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level7/LengthofLongestSubsequence.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level7; 2 | 3 | import java.util.*; 4 | 5 | public class LengthofLongestSubsequence { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | List A = new ArrayList<>(); 10 | A.add(1);// 1 11 2 10 4 5 2 1 11 | A.add(11); 12 | A.add(2); 13 | A.add(10); 14 | A.add(4); 15 | A.add(5); 16 | A.add(2); 17 | A.add(1); 18 | int ans = longestSubsequenceLength(A); 19 | System.out.println(ans); 20 | 21 | } 22 | 23 | public static int longestSubsequenceLength(final List A) { 24 | 25 | int[] ans = new int[A.size()]; 26 | 27 | for (int i = 0; i < ans.length; i++) { 28 | ans[i] = 1; 29 | for (int j = i - 1; j >= 0; j--) { 30 | if (A.get(i) >= A.get(j)) { 31 | ans[i] = Math.max(ans[i], 1 + ans[j]); 32 | } 33 | } 34 | } 35 | 36 | int[] ansD = new int[A.size()]; 37 | 38 | for (int i = ansD.length - 1; i >= 0; i--) { 39 | ansD[i] = 1; 40 | for (int j = i + 1; j < ansD.length; j++) { 41 | if (A.get(i) >= A.get(j)) { 42 | ansD[i] = Math.max(ansD[i], 1 + ansD[j]); 43 | } 44 | } 45 | } 46 | int max = -1; 47 | for (int i = 0; i < ans.length; i++) { 48 | ans[i] = ans[i] + ansD[i]; 49 | if (max < ans[i]) { 50 | max = ans[i]; 51 | } 52 | } 53 | return max - 1; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level3/bitManipulation/DifferentBitsSumPairwise.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level3.bitManipulation; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class DifferentBitsSumPairwise { 6 | 7 | public static void main(String[] args) { 8 | ArrayList A = new ArrayList<>(); 9 | A.add(1); 10 | A.add(3); 11 | A.add(5); 12 | int ans = differentBitsSumPairwise(A); 13 | System.out.println(ans); 14 | int ans2 = differentBitsSumPairwiseBest(A); 15 | System.out.println(ans2); 16 | } 17 | 18 | public static int differentBitsSumPairwise(ArrayList A) { 19 | int n = A.size(); 20 | int ans = 0; 21 | for (int i = 0; i < n; i++) { 22 | for (int j = i + 1; j < n; j++) { 23 | int temp = A.get(i) ^ A.get(j); 24 | int count = 0; 25 | while (temp != 0) { 26 | temp = temp & (temp - 1); 27 | count++; 28 | } 29 | ans += 2 * count; 30 | } 31 | } 32 | return ans; 33 | } 34 | 35 | public static int differentBitsSumPairwiseBest(ArrayList A) { 36 | int ans = 0; 37 | for (int i = 0; i < 32; i++) { 38 | int oneC = 0; 39 | int zeroC = 0; 40 | int mask = 1 << i; 41 | for (int j = 0; j < A.size(); j++) { 42 | int val = A.get(j); 43 | if ((val & mask) == mask) { 44 | oneC++; 45 | } else { 46 | zeroC++; 47 | } 48 | } 49 | ans = ans + 2 *oneC * zeroC; 50 | } 51 | return ans; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /coreJava/recursion/nQueens/NKnight.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.nQueens; 2 | 3 | public class NKnight { 4 | 5 | public static void main(String[] args) { 6 | boolean[][] board = new boolean[4][4]; 7 | nKnight(board, 1, 1, ""); 8 | } 9 | 10 | static int cou = 0; 11 | 12 | public static void nKnight(boolean[][] board, int cq, int cell_no, String asf) { 13 | if (cq > board.length) { 14 | cou++; 15 | System.out.println(cou + " " + asf); 16 | return; 17 | } 18 | if (cell_no > board.length * board.length) { 19 | return; 20 | } 21 | nKnight(board, cq, cell_no + 1, asf); 22 | int rno = (cell_no - 1) / board.length; 23 | int cno = (cell_no - 1) % board.length; 24 | if (board[rno][cno] == false && isTheKnightSafe(board, rno, cno) == true) { 25 | board[rno][cno] = true; 26 | nKnight(board, cq + 1, cell_no + 1, asf + " " + cell_no); 27 | board[rno][cno] = false; 28 | } 29 | } 30 | 31 | private static boolean isTheKnightSafe(boolean[][] board, int row, int col) { 32 | 33 | int[][] check = { { 1, -2 }, { 2, -1 }, { 2, 1 }, { 1, 2 }, { -1, 2 }, { -2, 1 }, { -1, -2 }, { -2, -1 } }; 34 | 35 | for (int ch = 0; ch < check.length; ch++) { 36 | int rc = row + check[ch][0]; 37 | int cc = col + check[ch][1]; 38 | if (rc >= 0 && cc >= 0 && rc < board.length && cc < board[0].length) { 39 | if (board[rc][cc] == true) { 40 | return false; 41 | } 42 | } 43 | } 44 | return true; 45 | } 46 | } -------------------------------------------------------------------------------- /dataStructures/genericTree/Client.java: -------------------------------------------------------------------------------- 1 | package dataStructures.genericTree; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | 7 | int[] arr = { 10, 20, 50, -1, 60, -1, -1, 30, 70, -1, 80, 110, -1, 120, -1, -1, 90, -1, -1, 40, 100, -1, 130, 8 | -1, -1, -1 }; 9 | 10 | GenericTreeH gt = new GenericTreeH(arr); 11 | 12 | int[] arr2 = { 1000, 20, 50, -1, 60, -1, -1, 30, 70, -1, 80, 110, -1, 120, -1, -1, 90, -1, -1, 40, 100, -1, 130, 13 | -1, -1, -1 }; 14 | 15 | GenericTreeH gt2 = new GenericTreeH(arr2); 16 | gt.display(); 17 | // System.out.println(gt.size2()); 18 | // System.out.println(gt.max()); 19 | // System.out.println(gt.height()); 20 | // System.out.println(gt.find(1000)); 21 | // System.out.println(gt.nodeToRootPath(120)); 22 | // gt.display(); 23 | System.out.println("-------------"); 24 | // gt.removeAllLeaf(); 25 | // gt.mirrorImage(); 26 | // gt.Linearize(); 27 | // gt.Linearize2(); 28 | // gt.display(); 29 | // System.out.println(gt.areSimilarShaped(gt, gt2)); 30 | // System.out.println(gt.areMirrorImage(gt, gt2)); 31 | 32 | // System.out.println(gt.isSymmetric()); 33 | // gt.multiSolver(); 34 | gt.display(); 35 | gt.multiSolve2(110); 36 | // gt.multiSolve3(70); 37 | gt.multiSolve4(110); 38 | gt.levelOrder(); 39 | gt.levelOrderLineWise(); 40 | System.out.println("<<<<----------->>>>"); 41 | // gt.levelOrderLineWise2(); 42 | gt.levelOrderZigZag(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/RotateMatrix.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class RotateMatrix { 6 | 7 | public static void main(String[] args) { 8 | 9 | ArrayList> ans = new ArrayList>(); 10 | int n = 2; 11 | for (int i = 0; i < n; i++) { 12 | ArrayList list = new ArrayList<>(); 13 | for (int j = 0; j < n; j++) { 14 | list.add((i + 1) * j); 15 | } 16 | ans.add(list); 17 | } 18 | 19 | for (int i = 0; i < ans.size(); i++) { 20 | for (int j = 0; j < ans.get(i).size(); j++) { 21 | System.out.print(ans.get(i).get(j) + " "); 22 | } 23 | System.out.println(); 24 | } 25 | 26 | rotate(ans); 27 | 28 | } 29 | 30 | public static void rotate(ArrayList> ans) { 31 | for (int i = 0; i < ans.size(); i++) { 32 | for (int j = i; j < ans.get(i).size(); j++) { 33 | swap(ans, i, j, j, i); 34 | } 35 | } 36 | 37 | int lc = 0; 38 | int rc = ans.get(0).size()-1; 39 | 40 | while (lc < rc) { 41 | for (int r = 0; r < ans.size(); r++) { 42 | swap(ans, r, lc, r, rc); 43 | } 44 | lc++; 45 | rc--; 46 | } 47 | } 48 | 49 | private static void swap(ArrayList> ans, int r1, int c1, int r2, int c2) { 50 | int temp1 = ans.get(r1).get(c1); 51 | int temp2 = ans.get(r2).get(c2); 52 | 53 | ans.get(r1).set(c1, temp2); 54 | ans.get(r2).set(c2, temp1); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /coreJava/recursion/nQueens/NQueensBetter.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.nQueens; 2 | 3 | public class NQueensBetter { 4 | 5 | public static void main(String[] args) { 6 | boolean[][] board = new boolean[4][4]; 7 | nQueensBetter(board, 1, 1, ""); 8 | } 9 | 10 | static int cou = 0; 11 | 12 | public static void nQueensBetter(boolean[][] board, int cq, int cell_no, String asf) { 13 | 14 | if (cq > board.length) { 15 | cou++; 16 | System.out.println(cou + " " + asf); 17 | 18 | return; 19 | } 20 | 21 | for (int i = cell_no; i <= board.length * board.length; i++) { 22 | 23 | int rno = (i - 1) / board.length; 24 | int cno = (i - 1) % board.length; 25 | 26 | if (board[rno][cno] == false && isTheQueenSafe(board, rno, cno) == true) { 27 | board[rno][cno] = true; 28 | nQueensBetter(board, cq + 1, i + 1, asf + i + " "); 29 | board[rno][cno] = false; 30 | } 31 | } 32 | } 33 | 34 | private static boolean isTheQueenSafe(boolean[][] board, int row, int col) { 35 | 36 | int[][] check = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 } }; 37 | 38 | for (int qr = 1; qr < board.length; qr++) { 39 | for (int ch = 0; ch < check.length; ch++) { 40 | 41 | int rc = row + qr * check[ch][0]; 42 | int cc = col + qr * check[ch][1]; 43 | 44 | if (rc >= 0 && cc >= 0 && rc < board.length && cc < board[0].length) { 45 | if (board[rc][cc] == true) { 46 | return false; 47 | } 48 | } 49 | 50 | } 51 | } 52 | return true; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming3/TargetSum.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming3; 2 | 3 | public class TargetSum { 4 | 5 | public static void main(String[] args) { 6 | int[] arr = { 5, 2, 4, 1, 3 }; 7 | int target = 10; 8 | boolean ans = tagetSum(arr, target); 9 | System.out.println(ans); 10 | 11 | } 12 | 13 | public static boolean tagetSum(int[] arr, int target) { 14 | boolean[][] ans = new boolean[arr.length + 1][target + 1]; 15 | 16 | for (int i = 0; i < ans.length; i++) { 17 | for (int j = 0; j < ans[0].length; j++) { 18 | if (i == 0 && j == 0) { 19 | ans[i][j] = true; 20 | } else if (((i - 1) >= 0) && (ans[i - 1][j] == true)) { 21 | ans[i][j] = true; 22 | } else if (((i - 1) >= 0) && ((j - arr[i - 1]) >= 0) && (ans[i - 1][j - arr[i - 1]] == true)) { 23 | ans[i][j] = true; 24 | } else { 25 | ans[i][j] = false; 26 | } 27 | } 28 | } 29 | printTagetSumPath(ans, arr, arr.length, target, ""); 30 | 31 | return ans[arr.length][target]; 32 | } 33 | 34 | public static void printTagetSumPath(boolean[][] ans, int[] arr, int i, int j, String asf) { 35 | if (j == 0) { 36 | System.out.println(asf); 37 | return; 38 | } 39 | 40 | if (i == 0) { 41 | return; 42 | } 43 | 44 | if (ans[i - 1][j] == true) { 45 | printTagetSumPath(ans, arr, i - 1, j, asf); 46 | } 47 | 48 | if ((j >= arr[i - 1]) && (ans[i - 1][j - arr[i - 1]] == true)) { 49 | printTagetSumPath(ans, arr, i - 1, j - arr[i - 1], asf + arr[i - 1] + " "); 50 | } 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming3/KnapSack01.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming3; 2 | 3 | public class KnapSack01 { 4 | 5 | public static void main(String[] args) { 6 | int[] wt = { 2, 5, 1, 3, 4 }; 7 | int[] price = { 15, 14, 10, 16, 30 }; 8 | int target = 7; 9 | int ans = knapSack01(wt, price, target); 10 | System.out.println(ans); 11 | } 12 | 13 | public static int knapSack01(int[] wt, int[] price, int target) { 14 | int[][] ans = new int[wt.length + 1][target + 1]; 15 | 16 | for (int i = 1; i < ans.length; i++) { 17 | for (int j = 1; j < ans[0].length; j++) { 18 | 19 | ans[i][j] = ans[i - 1][j]; 20 | 21 | if (j >= wt[i - 1]) { 22 | ans[i][j] = Math.max(ans[i][j], ans[i - 1][j - wt[i - 1]] + price[i - 1]); 23 | } 24 | } 25 | } 26 | 27 | for (int i = 1; i < ans.length; i++) { 28 | for (int j = 1; j < ans[0].length; j++) { 29 | System.out.print(ans[i][j] + " "); 30 | } 31 | System.out.println(); 32 | } 33 | printknapSack01Path(wt, ans, wt.length, target, ""); 34 | return ans[wt.length][target]; 35 | } 36 | 37 | public static void printknapSack01Path(int[] wt, int[][] ans, int i, int j, String asf) { 38 | 39 | if (j == 0) { 40 | System.out.println(asf); 41 | return; 42 | } 43 | 44 | if (j == 0) { 45 | return; 46 | } 47 | 48 | if (ans[i - 1][j] == ans[i][j]) { 49 | printknapSack01Path(wt, ans, i - 1, j, asf); 50 | } else { 51 | printknapSack01Path(wt, ans, i - 1, j - wt[i - 1], asf + wt[i - 1] + " "); 52 | } 53 | 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /coreJava/recursion/nQueens/NQueensSubSequence.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.nQueens; 2 | 3 | public class NQueensSubSequence { 4 | 5 | public static void main(String[] args) { 6 | boolean[][] board = new boolean[4][4]; 7 | nQueensSubSequence(board, 1, 1, ""); 8 | } 9 | 10 | public static void nQueensSubSequence(boolean[][] board, int cq, int cell_no, String asf) { 11 | 12 | if (cq > board.length) { 13 | System.out.println(asf); 14 | return; 15 | } 16 | 17 | if (cell_no == board.length * board.length) { 18 | return; 19 | } 20 | nQueensSubSequence(board, cq, cell_no + 1, asf); 21 | 22 | int rno = (cell_no - 1) / board.length; 23 | int cno = (cell_no - 1) % board.length; 24 | 25 | if (board[rno][cno] == false && isTheQueenSafe(board, rno, cno) == true) { 26 | board[rno][cno] = true; 27 | nQueensSubSequence(board, cq + 1, cell_no + 1, asf + cell_no + " "); 28 | board[rno][cno] = false; 29 | } 30 | 31 | } 32 | 33 | private static boolean isTheQueenSafe(boolean[][] board, int row, int col) { 34 | 35 | int[][] check = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 } }; 36 | 37 | for (int qr = 1; qr < board.length; qr++) { 38 | for (int ch = 0; ch < check.length; ch++) { 39 | 40 | int rc = row + qr * check[ch][0]; 41 | int cc = col + qr * check[ch][1]; 42 | 43 | if (rc >= 0 && cc >= 0 && rc < board.length && cc < board[0].length) { 44 | if (board[rc][cc] == true) { 45 | return false; 46 | } 47 | } 48 | 49 | } 50 | } 51 | return true; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/MaximumAbsoluteDifference.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class MaximumAbsoluteDifference { 6 | 7 | public static void main(String[] args) { 8 | ArrayList al = new ArrayList<>(); 9 | al.add(1); 10 | al.add(3); 11 | al.add(-1); 12 | int ans = maxArr(al); 13 | System.out.println(ans); 14 | int ans1 = maxArrBetter(al); 15 | System.out.println(ans1); 16 | } 17 | 18 | public static int maxArr(ArrayList A) { 19 | int max = Integer.MIN_VALUE; 20 | for (int i = 0; i < A.size(); i++) { 21 | for (int j = i + 1; j < A.size(); j++) { 22 | int temp = A.get(i) - A.get(j); 23 | if (temp < 0) { 24 | temp = -temp; 25 | } 26 | temp = temp + j - i; 27 | if (temp > max) { 28 | max = temp; 29 | } 30 | } 31 | } 32 | return max; 33 | } 34 | 35 | public static int maxArrBetter(ArrayList A) { 36 | int Mmax = Integer.MIN_VALUE; 37 | int Mmin = Integer.MAX_VALUE; 38 | int Pmax = Integer.MIN_VALUE; 39 | int Pmin = Integer.MAX_VALUE; 40 | for (int i = 0; i < A.size(); i++) { 41 | if ((A.get(i) - (i + 1)) > Mmax) { 42 | Mmax = A.get(i) - (i + 1); 43 | } 44 | 45 | if ((A.get(i) - (i + 1)) < Mmin) { 46 | Mmin = A.get(i) - (i + 1); 47 | } 48 | 49 | if ((A.get(i) + (i + 1)) > Pmax) { 50 | Pmax = A.get(i) + (i + 1); 51 | } 52 | 53 | if ((A.get(i) + (i + 1)) < Pmin) { 54 | Pmin = A.get(i) + (i + 1); 55 | } 56 | } 57 | return ((Mmax - Mmin) > (Pmax - Pmin)) ? (Mmax - Mmin) : (Pmax - Pmin); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming2/FindMaxSquare.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming2; 2 | 3 | public class FindMaxSquare { 4 | 5 | public static void main(String[] args) { 6 | 7 | int[][] arr = { { 1, 0, 1, 0, 1, 1, 1 }, { 1, 0, 1, 1, 0, 1, 1 }, { 1, 1, 1, 1, 0, 1, 0 }, 8 | { 0, 1, 1, 1, 1, 0, 0 }, { 1, 0, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 0 }, { 1, 0, 0, 1, 1, 0, 0 }, 9 | { 1, 0, 1, 1, 1, 0, 1 } }; 10 | 11 | findMaxSquare(arr); 12 | 13 | } 14 | 15 | public static void findMaxSquare(int[][] arr) { 16 | 17 | int[][] ans = new int[arr.length][arr[0].length]; 18 | 19 | int row = 0; 20 | int col = 0; 21 | 22 | for (int i = arr.length - 1; i >= 0; i--) { 23 | for (int j = arr[0].length - 1; j >= 0; j--) { 24 | 25 | if (i == arr.length - 1 || j == arr[0].length - 1) { 26 | ans[i][j] = arr[i][j]; 27 | } else { 28 | if (arr[i][j] == 1) { 29 | ans[i][j] = arr[i][j] + Math.min(ans[i][j + 1], Math.min(ans[i + 1][j], ans[i + 1][j + 1])); 30 | } 31 | } 32 | 33 | 34 | } 35 | } 36 | 37 | for (int i = 0; i < ans.length; i++) { 38 | for (int j = 0; j < ans[0].length; j++) { 39 | System.out.print(ans[i][j] + " "); 40 | } 41 | System.out.println(); 42 | } 43 | 44 | int max = 0; 45 | for (int i = 0; i < ans.length; i++) { 46 | for (int j = 0; j < ans[0].length; j++) { 47 | if (max <= ans[i][j]) { 48 | max = ans[i][j]; 49 | row = i + 1; 50 | col = j + 1; 51 | } 52 | } 53 | } 54 | System.out.println("Max Size square is " + max + " at Row and Col : " + row + " " + col); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /problemSolution/interview_prepMY/array/Find_subarray_with_given_sum.java: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/find-subarray-with-given-sum/ 2 | 3 | package problemSolution.interview_prepMY.array; 4 | 5 | public class Find_subarray_with_given_sum { 6 | 7 | public static void main(String[] args) { 8 | int[] arr = { 1, 4, 20, 3, 10, 5 }; 9 | int target = 18; 10 | sum(arr, target); 11 | sumBetter(arr, target); 12 | } 13 | 14 | public static void sum(int[] arr, int target) { 15 | for (int i = 0; i < arr.length; i++) { 16 | int tsum = 0; 17 | for (int j = i; j < arr.length; j++) { 18 | tsum += arr[j]; 19 | if (tsum == target) { 20 | System.out.println(i + " " + j); 21 | return; 22 | } 23 | } 24 | } 25 | 26 | System.out.println(-1); 27 | return; 28 | } 29 | 30 | public static void sumBetter(int[] arr, int target) { 31 | 32 | int[] ans = new int[arr.length]; 33 | ans[0] = arr[0]; 34 | for (int i = 1; i < arr.length; i++) { 35 | ans[i] = arr[i] + ans[i - 1]; 36 | } 37 | 38 | int traverse = 0; 39 | for (int i = 0; i < ans.length; i++) { 40 | if (target == ans[i]) { 41 | System.out.println(0 + " " + i); 42 | return; 43 | } else if (target < ans[i]) { 44 | traverse = i; 45 | break; 46 | } 47 | } 48 | 49 | while (traverse != ans.length) { 50 | for (int i = 0; i < traverse; i++) { 51 | int diff = ans[traverse] - ans[i]; 52 | if (diff == target) { 53 | System.out.println(i+1 + " " + traverse); 54 | return; 55 | } else if (diff < target) { 56 | break; 57 | } 58 | } 59 | traverse++; 60 | } 61 | System.out.println("NO subarray found"); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/SpiralOrderMatrixII.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class SpiralOrderMatrixII { 6 | 7 | public static void main(String[] args) { 8 | 9 | int n = 4; 10 | ArrayList> ans = generateMatrix(n); 11 | for (int i = 0; i < ans.size(); i++) { 12 | for (int j = 0; j < ans.get(i).size(); j++) { 13 | System.out.print(ans.get(i).get(j) + " "); 14 | } 15 | System.out.println(); 16 | } 17 | 18 | } 19 | 20 | public static ArrayList> generateMatrix(int n) { 21 | ArrayList> ans = new ArrayList>(); 22 | 23 | for (int i = 0; i < n; i++) { 24 | ArrayList list = new ArrayList<>(); 25 | for (int j = 0; j < n; j++) { 26 | list.add(0); 27 | } 28 | ans.add(list); 29 | } 30 | 31 | int rmin = 0; 32 | int rmax = n - 1; 33 | int cmin = 0; 34 | int cmax = n - 1; 35 | 36 | int total = n * n; 37 | int current = 1; 38 | 39 | while (current <= total) { 40 | for (int i = cmin; i <= cmax && current <= total; i++) { 41 | ans.get(rmin).set(i, current); 42 | current++; 43 | } 44 | rmin++; 45 | 46 | for (int i = rmin; i <= rmax && current <= total; i++) { 47 | ans.get(i).set(cmax, current); 48 | current++; 49 | } 50 | cmax--; 51 | 52 | for (int i = cmax; i >= cmin && current <= total; i--) { 53 | ans.get(rmax).set(i, current); 54 | current++; 55 | } 56 | rmax--; 57 | 58 | for (int i = rmax; i >= rmin && current <= total; i--) { 59 | ans.get(i).set(cmin, current); 60 | current++; 61 | } 62 | cmin++; 63 | } 64 | 65 | return ans; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/AntiDiagonals.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class AntiDiagonals { 6 | 7 | public static void main(String[] args) { 8 | int n = 4; 9 | ArrayList> ans = new ArrayList>(); 10 | 11 | int c = 1; 12 | for (int i = 0; i < n; i++) { 13 | ArrayList list = new ArrayList<>(); 14 | for (int j = 0; j < n; j++) { 15 | list.add(c); 16 | c++; 17 | } 18 | ans.add(list); 19 | } 20 | 21 | for (int i = 0; i < ans.size(); i++) { 22 | for (int j = 0; j < ans.get(i).size(); j++) { 23 | System.out.print(ans.get(i).get(j) + " "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | ans = diagonal(ans); 29 | 30 | for (int i = 0; i < ans.size(); i++) { 31 | for (int j = 0; j < ans.get(i).size(); j++) { 32 | System.out.print(ans.get(i).get(j) + " "); 33 | } 34 | System.out.println(); 35 | } 36 | 37 | } 38 | 39 | public static ArrayList> diagonal(ArrayList> A) { 40 | ArrayList> ans = new ArrayList>(); 41 | 42 | for (int i = 0; i < A.size(); i++) { 43 | int k = i; 44 | ArrayList list = new ArrayList<>(); 45 | for (int j = 0; j <= i; j++) { 46 | list.add(A.get(j).get(k)); 47 | k--; 48 | } 49 | ans.add(list); 50 | } 51 | 52 | for (int i = 0 ; i < A.size()-1; i++) { 53 | int k = A.size()-1; 54 | ArrayList list = new ArrayList<>(); 55 | for (int j = i+1; j <= (A.size() - 1) ; j++) { 56 | list.add(A.get(j).get(k)); 57 | k--; 58 | } 59 | ans.add(list); 60 | } 61 | return ans; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming2/MatrixChainMultiplication.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming2; 2 | 3 | public class MatrixChainMultiplication { 4 | 5 | public static void main(String[] args) { 6 | 7 | int[] arr = { 10, 20, 30, 40, 50, 60 }; 8 | int ans = matrixChainMultiplication(arr, 0, arr.length - 1); 9 | System.out.println(ans); 10 | } 11 | 12 | public static int matrixChainMultiplication(int[] arr, int i, int j) { 13 | 14 | if (i == j - 1) { 15 | return 0; 16 | } 17 | 18 | int min_coast = Integer.MAX_VALUE; 19 | for (int cut = i + 1; cut < j; cut++) { 20 | int c1 = matrixChainMultiplication(arr, i, cut); 21 | int c2 = matrixChainMultiplication(arr, cut, j); 22 | int muc = arr[i] * arr[cut] * arr[j]; 23 | int total_cost = c1 + c2 + muc; 24 | 25 | min_coast = Math.min(min_coast, total_cost); 26 | } 27 | 28 | return min_coast; 29 | } 30 | 31 | public static int matrixChainMultiplicationT(int[] arr) { 32 | int[][] ans = new int[arr.length - 1][arr.length - 1]; 33 | 34 | for (int gap = 0; gap < arr.length; gap++) { 35 | for (int i = 0; i + gap < arr.length; i++) { 36 | int j = i + gap; 37 | 38 | if (gap == 0) { 39 | ans[i][j] = 0; 40 | } else if (gap == 1) { 41 | ans[i][j] = arr[i - 1] * arr[i] * arr[i + 1]; 42 | } else { 43 | int mic = Integer.MAX_VALUE; 44 | 45 | for (int k = 1; k <= gap; k++) { 46 | 47 | int c1 = ans[i][i + k - 1]; 48 | int c2 = ans[i + k][j]; 49 | int muc = arr[i] * arr[i + k] * arr[j + 1]; 50 | int tc = c1 + c2 + muc; 51 | mic = Math.min(mic, tc); 52 | 53 | } 54 | 55 | ans[i][j] = mic; 56 | } 57 | } 58 | } 59 | return ans[0][arr.length - 1]; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming5/LongestCommenSubsequence.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming5; 2 | 3 | public class LongestCommenSubsequence { 4 | 5 | public static void main(String[] args) { 6 | String s1 = "acbg"; 7 | String s2 = "abcg"; 8 | int ans = longestCommenSubsequenceM(s1, s2, new Integer[s1.length() + 1][s2.length() + 1]); 9 | System.out.println(ans); 10 | 11 | int ans2 = longestCommenSubsequenceT(s1, s2); 12 | System.out.println(ans2); 13 | 14 | } 15 | 16 | public static int longestCommenSubsequenceM(String s1, String s2, Integer[][] qb) { 17 | 18 | if (s1.length() == 0 || s2.length() == 0) { 19 | return 0; 20 | } 21 | 22 | if (qb[s1.length()][s2.length()] != null) { 23 | return qb[s1.length()][s2.length()]; 24 | } 25 | 26 | char ch1 = s1.charAt(0); 27 | String s1rest = s1.substring(1); 28 | char ch2 = s2.charAt(0); 29 | String s2rest = s2.substring(1); 30 | 31 | int ans = 0; 32 | 33 | if (ch1 == ch2) { 34 | ans = 1 + longestCommenSubsequenceM(s1rest, s2rest, qb); 35 | } else { 36 | int f1 = longestCommenSubsequenceM(s1, s2rest, qb); 37 | int f2 = longestCommenSubsequenceM(s1rest, s2, qb); 38 | ans = Integer.max(f1, f2); 39 | } 40 | qb[s1.length()][s2.length()] = ans; 41 | return ans; 42 | } 43 | 44 | public static int longestCommenSubsequenceT(String s1, String s2) { 45 | int[][] ans = new int[s1.length() + 1][s2.length() + 1]; 46 | 47 | for (int i = ans.length - 2; i >= 0; i--) { 48 | for (int j = ans.length - 2; j >= 0; j--) { 49 | if (s1.charAt(i) == s2.charAt(j)) { 50 | ans[i][j] = 1 + ans[i + 1][j + 1]; 51 | } else { 52 | ans[i][j] = Integer.max(ans[i + 1][j], ans[i][j + 1]); 53 | } 54 | } 55 | } 56 | 57 | return ans[0][0]; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming2/MinimumPalindromicCut.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming2; 2 | 3 | public class MinimumPalindromicCut { 4 | 5 | public static void main(String[] args) { 6 | String ques = "abccbc"; 7 | int ans = minimumPalindromicCut(ques, 0, ques.length()); 8 | System.out.println(ans); 9 | 10 | } 11 | 12 | public static int minimumPalindromicCut(String ques, int i, int j) { 13 | 14 | if (isPalindrom(ques, i, j) == true) { 15 | return 0; 16 | } 17 | 18 | int min_cut = Integer.MAX_VALUE; 19 | for (int cut_point = i; cut_point < j; cut_point++) { 20 | int c1 = minimumPalindromicCut(ques, i, cut_point); 21 | int c2 = minimumPalindromicCut(ques, cut_point + 1, j); 22 | int total_cut = c1 + c2 + 1; 23 | 24 | min_cut = Math.min(total_cut, min_cut); 25 | } 26 | 27 | return min_cut; 28 | } 29 | 30 | public static int minimumPalindromicCutM(String ques, int i, int j, Integer[][] qb) { 31 | 32 | if (isPalindrom(ques, i, j) == true) { 33 | return 0; 34 | } 35 | 36 | if (qb[i][j] != null) { 37 | return qb[i][j]; 38 | } 39 | int min_cut = Integer.MAX_VALUE; 40 | for (int cut_point = i; cut_point < j; cut_point++) { 41 | int c1 = minimumPalindromicCutM(ques, i, cut_point ,qb); 42 | int c2 = minimumPalindromicCutM(ques, cut_point + 1, j,qb); 43 | int total_cut = c1 + c2 + 1; 44 | 45 | min_cut = Math.min(total_cut, min_cut); 46 | } 47 | 48 | qb[i][j]; 49 | return min_cut; 50 | } 51 | 52 | private static boolean isPalindrom(String ques, int i, int j) { 53 | 54 | int left = i; 55 | int right = j - 1; 56 | while (left < right) { 57 | if (ques.charAt(left) != ques.charAt(right)) { 58 | return false; 59 | } 60 | 61 | left++; 62 | right--; 63 | } 64 | return true; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /coreJava/recursion/nQueens/NQueens.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.nQueens; 2 | 3 | public class NQueens { 4 | 5 | public static void main(String[] args) { 6 | boolean[][] board = new boolean[4][4]; 7 | nQueens(board, 1, 1, ""); 8 | } 9 | 10 | static int cou = 0; 11 | 12 | public static void nQueens(boolean[][] board, int cq, int cell_no, String asf) { 13 | 14 | if (cq > board.length) { 15 | if (isTheBoardValid(board) == true) { 16 | cou++; 17 | System.out.println(cou + " " + asf); 18 | } 19 | return; 20 | } 21 | 22 | for (int i = cell_no; i <= board.length * board.length; i++) { 23 | 24 | int rno = (i - 1) / board.length; 25 | int cno = (i - 1) % board.length; 26 | 27 | if (board[rno][cno] == false) { 28 | board[rno][cno] = true; 29 | nQueens(board, cq + 1, i + 1, asf + i + " "); 30 | board[rno][cno] = false; 31 | } 32 | 33 | } 34 | } 35 | 36 | private static boolean isTheBoardValid(boolean[][] board) { 37 | for (int row = 0; row < board.length; row++) { 38 | for (int col = 0; col < board[0].length; col++) { 39 | if (board[row][col] == true) { 40 | if (isTheQueenSafe(board, row, col) == false) { 41 | return false; 42 | } 43 | } 44 | } 45 | } 46 | return true; 47 | } 48 | 49 | private static boolean isTheQueenSafe(boolean[][] board, int row, int col) { 50 | 51 | int[][] check = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 } }; 52 | 53 | for (int qr = 1; qr < board.length; qr++) { 54 | for (int ch = 0; ch < check.length; ch++) { 55 | 56 | int rc = row + qr * check[ch][0]; 57 | int cc = col + qr * check[ch][1]; 58 | 59 | if (rc >= 0 && cc >= 0 && rc < board.length && cc < board[0].length) { 60 | if (board[rc][cc] == true) { 61 | return false; 62 | } 63 | } 64 | 65 | } 66 | } 67 | return true; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/RepeatAndMissingNumberArray.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class RepeatAndMissingNumberArray { 6 | 7 | public static void main(String[] args) { 8 | 9 | /* 10 | * List l = new ArrayList<>(); l.add(1); l.add(1); l.add(2); 11 | * l.add(5); 12 | */// l.add(1); 13 | // l = repeatedNumber(l); 14 | // for (int val : l) { 15 | // System.out.print(val + " "); 16 | // } 17 | 18 | List l = new ArrayList<>(); 19 | l.add(1); 20 | l.add(3); 21 | l.add(2); 22 | int ans = solve(l); 23 | System.out.println(ans); 24 | 25 | } 26 | 27 | public static int solve(List A) { 28 | 29 | int count = 0; 30 | for (int val : A) { 31 | boolean flag = checkPrime(val); 32 | if (flag == true) { 33 | count += 1; 34 | } 35 | } 36 | 37 | int ans = (int) Math.pow(2, count); 38 | ans = ans - 1; 39 | return 1; 40 | 41 | } 42 | 43 | public static boolean checkPrime(int a) { 44 | int i = 2; 45 | while ((i * i) <= a) { 46 | if ((a % i) == 0) { 47 | return false; 48 | } 49 | i++; 50 | } 51 | return true; 52 | } 53 | 54 | public static ArrayList repeatedNumber(final List A) { 55 | ArrayList ans = new ArrayList<>(); 56 | int a = 0, b = 0; 57 | int tempAns = 0; 58 | for (int i = 0; i < A.size(); i++) { 59 | tempAns = tempAns ^ A.get(i); 60 | tempAns = tempAns ^ (i + 1); 61 | } 62 | 63 | int mask = tempAns & (-tempAns); 64 | for (int i = 0; i < A.size(); i++) { 65 | if ((A.get(i) & mask) == mask) { 66 | a = a ^ A.get(i); 67 | } else { 68 | b = b ^ A.get(i); 69 | } 70 | 71 | if (((i + 1) & mask) == mask) { 72 | a = a ^ (i + 1); 73 | } else { 74 | b = b ^ (i + 1); 75 | } 76 | } 77 | boolean flag = false; 78 | for (int i = 0; i < A.size(); i++) { 79 | if ((A.get(i) ^ a) == 0) { 80 | flag = true; 81 | } 82 | } 83 | 84 | if (flag) { 85 | ans.add(a); 86 | ans.add(b); 87 | } else { 88 | ans.add(b); 89 | ans.add(a); 90 | } 91 | return ans; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming5/EditDistance.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming5; 2 | 3 | public class EditDistance { 4 | 5 | public static void main(String[] args) { 6 | String s1 = "abcdeghi"; 7 | String s2 = "acdfgkhi"; 8 | int ans = editDistanceM(s1, s2, new Integer[s1.length() + 1][s2.length() + 1]); 9 | System.out.println(ans); 10 | 11 | int ans2 = editDistanceT(s1, s2); 12 | System.out.println(ans2); 13 | } 14 | 15 | public static int editDistanceM(String s1, String s2, Integer[][] qb) { 16 | 17 | if (s1.length() == 0) { 18 | return s2.length(); // delete 19 | } else if (s2.length() == 0) { 20 | return s1.length(); // add 21 | } 22 | 23 | if (qb[s1.length()][s2.length()] != null) { 24 | return qb[s1.length()][s2.length()]; 25 | } 26 | 27 | char ch1 = s1.charAt(0); 28 | String restS1 = s1.substring(1); 29 | char ch2 = s2.charAt(0); 30 | String restS2 = s2.substring(1); 31 | 32 | int ans = 0; 33 | 34 | if (ch1 == ch2) { 35 | ans = editDistanceM(restS1, restS2, qb); 36 | } else { 37 | // Add 38 | int f1 = editDistanceM(restS1, s2, qb); 39 | // Delete 40 | int f2 = editDistanceM(s1, restS2, qb); 41 | // Replace 42 | int f3 = editDistanceM(restS1, restS2, qb); 43 | 44 | ans = 1 + Math.min(f1, Math.min(f2, f3)); 45 | } 46 | qb[s1.length()][s2.length()] = ans; 47 | return ans; 48 | } 49 | 50 | public static int editDistanceT(String s1, String s2) { 51 | int[][] ans = new int[s1.length() + 1][s2.length() + 1]; 52 | 53 | for (int i = ans.length - 1; i >= 0; i--) { 54 | for (int j = ans[0].length - 1; j >= 0; j--) { 55 | if (i == (ans.length - 1) && j == (ans[0].length - 1)) { 56 | ans[i][j] = 0; 57 | } else if (i == (ans.length - 1)) { 58 | ans[i][j] = ans[i][j + 1] + 1; 59 | } else if (j == (ans[0].length - 1)) { 60 | ans[i][j] = ans[i + 1][j] + 1; 61 | } else if (s1.charAt(i) == s2.charAt(j)) { 62 | ans[i][j] = ans[i + 1][j + 1]; 63 | } else { 64 | ans[i][j] = 1 + Math.min(ans[i + 1][j], Math.min(ans[i][j + 1], ans[i + 1][j + 1])); 65 | } 66 | } 67 | } 68 | return ans[0][0]; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /dataStructures/linkedlist/ClientH.java: -------------------------------------------------------------------------------- 1 | package dataStructures.linkedlist; 2 | 3 | public class ClientH { 4 | 5 | public static void main(String[] args) { 6 | LinkedListH ll = new LinkedListH(); 7 | ll.addLast(10); 8 | // ll.addLast(10); 9 | // ll.addLast(10); 10 | ll.addLast(21); 11 | // ll.addLast(20); 12 | // ll.addLast(30); 13 | ll.addLast(30); 14 | ll.addLast(41); 15 | // ll.addLast(40); 16 | // ll.addLast(40); 17 | ll.addLast(50); 18 | // ll.addFirst(5); 19 | 20 | // System.out.println(ll.getFirst()); 21 | // System.out.println(ll.getLast()); 22 | // System.out.println(ll.getAt(3)); 23 | // 24 | // System.out.println(ll.removeFirst()); 25 | // System.out.println(ll.removeLast()); 26 | 27 | // ll.addAt(0, 45); 28 | // ll.addAt(7, 45); 29 | // ll.display(); 30 | // System.out.println(ll.removeAt(1)); 31 | 32 | // Check 33 | // ll.addFirst(10); 34 | // ll.display(); 35 | // ll.addLast(20); 36 | // ll.display(); 37 | // ll.addLast(30); 38 | // ll.display(); 39 | // ll.addFirst(40); 40 | // ll.display(); 41 | // 42 | // ll.addAt(2, 100); 43 | // ll.display(); 44 | // 45 | // System.out.println(ll.removeAt(3)); 46 | // ll.display(); 47 | // 48 | // while(!ll.isEmpty()){ 49 | // System.out.println(ll.removeLast()); 50 | // ll.display(); 51 | // } 52 | 53 | // ll.reverseDataItertive(); 54 | // ll.display(); 55 | // 56 | // ll.reversePointerItertive(); 57 | // ll.display(); 58 | // 59 | // System.out.println(ll.IsPalindrome()); 60 | // 61 | // ll.addLast(60); 62 | // ll.addLast(60); 63 | // ll.addLast(60); 64 | // ll.addLast(60); 65 | 66 | ll.display(); 67 | // 68 | // ll.fold(); 69 | // ll.display(); 70 | 71 | // System.out.println(ll.mid()); 72 | // System.out.println(ll.kthFromLast(3)); 73 | 74 | // LinkedListH l2 = new LinkedListH(); 75 | // l2.addLast(5); 76 | // l2.addLast(15); 77 | // l2.addLast(25); 78 | // l2.display(); 79 | // LinkedListH l3 = ll.mergeTwoSortedLists(ll, l2); 80 | // l3.display(); 81 | // LinkedListH l4 = ll.mergSort(l3); 82 | // l4.display(); 83 | // l3.display(); 84 | 85 | // ll.display(); 86 | // ll.kthReverse(2); 87 | // ll.display(); 88 | // ll.display(); 89 | // ll.removeDuplicates(); 90 | ll.display(); 91 | 92 | // ll.oddEven(); 93 | // ll.display(); 94 | ll.reverseDataRecursive(); 95 | ll.display(); 96 | 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/Try1.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming; 2 | 3 | public class Try1 { 4 | 5 | public static void main(String[] args) { 6 | // System.out.println(a(85)); 7 | // System.out.println(checkperfectnumber(49)); 8 | 9 | String[] q = { "abcd", "hfgl", "mijk" }; 10 | // System.out.println(check(q)); 11 | 12 | // patteren(3, 4); 13 | patteren2(4); 14 | 15 | } 16 | 17 | public static int a(int n) { 18 | int[] ans = new int[n + 1]; 19 | 20 | int count = 0; 21 | 22 | for (int i = 1; i < ans.length; i++) { 23 | if (((count + 1) * (count + 1)) == i) { 24 | count = count + 1; 25 | } 26 | ans[i] = 1 + ans[i - 1]; 27 | for (int j = 2; j <= count; j++) { 28 | ans[i] = Math.min(ans[i], 1 + ans[i - (j * j)]); 29 | } 30 | } 31 | 32 | return ans[n]; 33 | } 34 | 35 | public static int checkperfectnumber(int a) { 36 | int sum = 1; 37 | 38 | for (int i = 2; i <= (a / 2); i++) { 39 | if ((a % i) == 0) { 40 | sum += i; 41 | } 42 | } 43 | if (sum == a) { 44 | return 1; 45 | } 46 | return 0; 47 | } 48 | 49 | public static int check(String[] a) { 50 | 51 | int[] ans = new int[26]; 52 | 53 | for (int i = 0; i < a.length; i++) { 54 | String temp = a[i]; 55 | for (int k = 0; k < temp.length(); k++) { 56 | char current = temp.charAt(k); 57 | ans[(int) (current - 'a')] += 1; 58 | } 59 | } 60 | 61 | for (int i = 0; i < 26; i++) { 62 | if (ans[i] > 1) { 63 | return -1; 64 | } 65 | } 66 | 67 | return 1; 68 | } 69 | 70 | public static void patteren(int i, int j) { 71 | 72 | int temp = 0; 73 | for (int k = 1; k < (2 * j); k++) { 74 | if (k <= j) { 75 | temp = temp + 1; 76 | for (int l = 1; l <= k; l++) { 77 | System.out.print(temp); 78 | } 79 | } else { 80 | temp = temp - 1; 81 | for (int l = 1; l <= (j - (k % j)); l++) { 82 | System.out.print(temp); 83 | } 84 | } 85 | System.out.println(); 86 | } 87 | } 88 | 89 | public static void patteren2(int n) { 90 | 91 | int temp = 1; 92 | for (int i = 1; i <= (2 * n); i++) { 93 | if (i <= n) { 94 | for (int k = 1; k < i; k++) { 95 | System.out.print(temp + "*"); 96 | } 97 | System.out.print(temp); 98 | temp = temp + 1; 99 | } else { 100 | temp = temp - 1; 101 | for (int k = 1; k < (n - ((i - 1) % n)); k++) { 102 | System.out.print(temp + "*"); 103 | } 104 | System.out.print(temp); 105 | } 106 | System.out.println(); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming1/BoardCount.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming1; 2 | 3 | public class BoardCount { 4 | 5 | public static void main(String[] args) { 6 | // System.out.println(boardCountPath(0, 10)); 7 | // System.out.println(boardCountPathMemoizatation(0, 10, new int[11])); 8 | // System.out.println(boardCountPathTabulation(0, 30)); 9 | System.out.println(boardCountPathTabulationBetter(0, 10)); 10 | System.out.println(boardCountPathTabulationBest(0, 10)); 11 | } 12 | 13 | public static int boardCountPath(int src, int des) { 14 | if (src == des) { 15 | return 1; 16 | } 17 | int total_count = 0; 18 | for (int dice = 1; dice <= 6; dice++) { 19 | int inter = src + dice; 20 | if (inter <= des) { 21 | int curr_count = boardCountPath(inter, des); 22 | total_count += curr_count; 23 | } 24 | } 25 | return total_count; 26 | } 27 | 28 | public static int boardCountPathMemoizatation(int src, int des, int[] m) { 29 | if (src == des) { 30 | return 1; 31 | } 32 | if (m[src] != 0) { 33 | return m[src]; 34 | } 35 | 36 | int total_count = 0; 37 | for (int dice = 1; dice <= 6; dice++) { 38 | int inter = src + dice; 39 | if (inter <= des) { 40 | int curr_count = boardCountPathMemoizatation(inter, des, m); 41 | total_count += curr_count; 42 | } 43 | } 44 | 45 | m[src] = total_count; 46 | return total_count; 47 | } 48 | 49 | public static int boardCountPathTabulation(int src, int des) { 50 | int[] ans = new int[des + 1]; 51 | ans[des] = 1; 52 | 53 | for (int i = des - 1; i >= src; i--) { 54 | for (int k = i + 1; k <= 6 + i && k <= des; k++) { 55 | ans[i] = ans[i] + ans[k]; 56 | } 57 | } 58 | return ans[0]; 59 | } 60 | 61 | public static int boardCountPathTabulationBetter(int src, int des) { 62 | int[] ans = new int[6]; 63 | ans[(des - 1) % 6] = 1; 64 | 65 | for (int i = des - 1; i >= src; i--) { 66 | int temp = 0; 67 | for (int k = i + 1; k <= 6 + i && k <= des; k++) { 68 | temp += ans[(k - 1) % 6]; 69 | } 70 | if (i == 0) { 71 | ans[0] = temp; 72 | } else { 73 | ans[(i - 1) % 6] = temp; 74 | } 75 | } 76 | return ans[0]; 77 | } 78 | 79 | public static int boardCountPathTabulationBest(int src, int des) { 80 | int[] ans = new int[6]; 81 | ans[0] = 1; 82 | 83 | for (int i = des-1; i >= src; i--) { 84 | int temp = ans[0] + ans[1] + ans[2] + ans[3] + ans[4] + ans[5]; 85 | ans[5] = ans[4]; 86 | ans[4] = ans[3]; 87 | ans[3] = ans[2]; 88 | ans[2] = ans[1]; 89 | ans[1] = ans[0]; 90 | ans[0] = temp; 91 | } 92 | return ans[0]; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /coreJava/recursion/recursionQuesion/Sudoku.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.recursionQuesion; 2 | 3 | public class Sudoku { 4 | 5 | 6 | public static void main(String[] args) { 7 | int[][] arr = { { 3, 0, 6, 5, 0, 8, 4, 0, 0 }, { 5, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 8, 7, 0, 0, 0, 0, 3, 1 }, 8 | { 0, 0, 3, 0, 1, 0, 0, 8, 0 }, { 9, 0, 0, 8, 6, 3, 0, 0, 5 }, { 0, 5, 0, 0, 9, 0, 6, 0, 0 }, 9 | { 1, 3, 0, 0, 0, 0, 2, 5, 0 }, { 0, 0, 0, 0, 0, 0, 0, 7, 4 }, { 0, 0, 5, 2, 0, 6, 3, 0, 0 } }; 10 | 11 | int[] rownos = new int[9]; 12 | int[] colnos = new int[9]; 13 | int[] subM = new int[9]; 14 | 15 | for (int i = 0; i < arr.length; i++) { 16 | for (int j = 0; j < arr[0].length; j++) { 17 | if (arr[i][j] != 0) { 18 | makeChoice(arr, i * arr.length + j + 1, rownos, colnos, subM, arr[i][j]); 19 | } 20 | } 21 | } 22 | sudoku(arr, 1, rownos, colnos, subM); 23 | } 24 | 25 | public static void sudoku(int[][] arr, int cellNo, int[] rownos, int[] colnos, int[] subM) { 26 | 27 | if (cellNo == 82) { 28 | for (int i = 0; i < arr.length; i++) { 29 | for (int j = 0; j < arr[0].length; j++) { 30 | System.out.print(arr[i][j] + " "); 31 | } 32 | System.out.println(); 33 | } 34 | return; 35 | } 36 | 37 | int rno = (cellNo - 1) / arr.length; 38 | int cno = (cellNo - 1) % arr.length; 39 | 40 | if (arr[rno][cno] == 0) { 41 | int rowNo = rownos[rno]; 42 | int colNo = colnos[cno]; 43 | int subNo = subM[(rno / 3) * 3 + (cno / 3)]; 44 | int no = rowNo | colNo | subNo; 45 | for (int choice = 1; choice <= 9; choice++) { 46 | if ((no & (1 << choice)) == 0) { 47 | makeChoice(arr, cellNo, rownos, colnos, subM, choice); 48 | sudoku(arr, cellNo + 1, rownos, colnos, subM); 49 | unMakeChoice(arr, cellNo, rownos, colnos, subM, choice); 50 | } 51 | } 52 | } else { 53 | sudoku(arr, cellNo + 1, rownos, colnos, subM); 54 | } 55 | } 56 | 57 | private static void makeChoice(int[][] arr, int cellNo, int[] rownos, int[] colnos, int[] subM, int choice) { 58 | 59 | int rno = (cellNo - 1) / arr.length; 60 | int cno = (cellNo - 1) % arr.length; 61 | 62 | arr[rno][cno] = choice; 63 | rownos[rno] ^= (1 << choice); 64 | colnos[cno] ^= (1 << choice); 65 | subM[(rno / 3) * 3 + (cno / 3)] ^= (1 << choice); 66 | } 67 | 68 | private static void unMakeChoice(int[][] arr, int cellNo, int[] rownos, int[] colnos, int[] subM, int choice) { 69 | int rno = (cellNo - 1) / arr.length; 70 | int cno = (cellNo - 1) % arr.length; 71 | 72 | arr[rno][cno] = 0; 73 | rownos[rno] ^= (1 << choice); 74 | colnos[cno] ^= (1 << choice); 75 | subM[(rno / 3) * 3 + (cno / 3)] ^= (1 << choice); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /dataStructures/binaryTree/Client.java: -------------------------------------------------------------------------------- 1 | package dataStructures.binaryTree; 2 | 3 | import java.util.LinkedList; 4 | 5 | public class Client { 6 | 7 | public static void main(String[] args) { 8 | int[] arr = { 50, 25, 12, -1, 37, 30, -1, 40, -1, -1, -1, 75, 62, 60, -1, 70, -1, -1, 87, -1, -1, -1 }; 9 | BinaryTree bt = new BinaryTree(arr); 10 | // bt.display(); 11 | // System.out.println(bt.Size()); 12 | // System.out.println(bt.height()); 13 | // System.out.println(bt.max()); 14 | // System.out.println(bt.find(87)); 15 | // bt.removeLeaf(); 16 | // bt.display(); 17 | int[] arr1 = { 50, 25, 12, 10, -1, 20, -1, -1, -1, 75, 62, 60, -1, -1, 87, -1, -1, -1 }; 18 | BinaryTree bt1 = new BinaryTree(arr1); 19 | // bt1.SingleChild(); 20 | // bt.rootToNodePathWithTarget(150, 250); 21 | // bt.NodetoKth(25, 2); 22 | 23 | int[] arr2 = { 0, 11, 21, 31, 41, -1, 42, -1, -1, 32, 43, 51, 61, -1, -1, 52, 62, -1, 63, 71, -1, 72, -1, -1, 24 | -1, -1, 44, 53, 64, -1, 65, -1, -1, 54, -1, -1, -1, -1, 22, 33, 45, -1, 46, -1, -1, 34, 47, -1, 48, -1, 25 | -1, -1, -1, 12, 23, -1, 24, -1, -1, -1 }; 26 | // BinaryTree bt2 = new BinaryTree(arr2); 27 | // bt2.NodetoKth(43, 3); 28 | 29 | int[] pre = { 50, 25, 12, 37, 75, 62, 87 }; 30 | int[] in = { 12, 25, 37, 50, 62, 75, 87 }; 31 | int[] post = { 12, 37, 25, 62, 87, 75, 50 }; 32 | // BinaryTree bt3 = new BinaryTree(pre, in); 33 | // bt3.display(); 34 | BinaryTree bt4 = new BinaryTree(post, in); 35 | // bt4.display(); 36 | 37 | // Q-->>>Binary Number 38 | // printBinary(15); 39 | 40 | // Q-->> Itertive 41 | // bt4.preoi(); 42 | // bt4.inoi(); 43 | // bt4.postoi(); 44 | 45 | int[] arr5 = { 50, 25, 12, -1, 37, 30, -1, 46, 45, -1, -1, -1, -1, 75, 62, 60, -1, 70, -1, -1, 87, -1, -1, -1 }; 46 | BinaryTree bt5 = new BinaryTree(arr5); 47 | System.out.println(bt5.baleneceHeight()); 48 | System.out.println(bt5.baleneceHeightON()); 49 | } 50 | 51 | public static void printBinary(int n) { 52 | 53 | LinkedList queue = new LinkedList<>(); 54 | queue.addLast(new Pair(1, "1")); 55 | 56 | while (queue.size() > 0) { 57 | Pair rm = queue.removeFirst(); 58 | System.out.println(rm.data + " == " + rm.bin); 59 | 60 | Pair left = new Pair(2 * rm.data, rm.bin + "0"); 61 | Pair right = new Pair(2 * rm.data + 1, rm.bin + "1"); 62 | 63 | if (left.data <= n) { 64 | queue.addLast(left); 65 | } 66 | if (right.data <= n) { 67 | queue.addLast(right); 68 | } 69 | } 70 | } 71 | 72 | private static class Pair { 73 | int data; 74 | String bin; 75 | 76 | public Pair(int data, String bin) { 77 | this.data = data; 78 | this.bin = bin; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /problemSolution/interviewBit/level2/array/MaxNonNegativeSubArray.java: -------------------------------------------------------------------------------- 1 | package problemSolution.interviewBit.level2.array; 2 | 3 | import java.util.*; 4 | 5 | public class MaxNonNegativeSubArray { 6 | 7 | public static void main(String[] args) { 8 | 9 | ArrayList l = new ArrayList<>(); 10 | l.add(-926); 11 | l.add(-16); 12 | l.add(-5); 13 | l.add(142980); 14 | 15 | ArrayList ans = maxset(l); 16 | for (int val : ans) { 17 | //System.out.print(val + " "); 18 | } 19 | 20 | ArrayList ans1 = maxsetBetter(l); 21 | for (Integer val : ans1) { 22 | System.out.print(val + " "); 23 | } 24 | } 25 | 26 | public static ArrayList maxset(ArrayList A) { 27 | 28 | long current = 0; 29 | int currentL = 0; 30 | int currentR = 0; 31 | 32 | long total = 0; 33 | int totalL = -1; 34 | int totalR = -1; 35 | 36 | int totalLF = -1; 37 | int totalRF = -1; 38 | 39 | int diff = -1; 40 | for (int i = 0; i < A.size(); i++) { 41 | long temp = A.get(i); 42 | if (temp < 0) { 43 | current = 0; 44 | currentL = i + 1; 45 | currentR = i + 1; 46 | continue; 47 | } else if ((current + temp) >= total) { 48 | current = current + temp; 49 | total = current; 50 | totalL = currentL; 51 | totalR = currentR; 52 | currentR = i + 1; 53 | } else { 54 | current = current + temp; 55 | currentR = i + 1; 56 | if (current < 0) { 57 | current = 0; 58 | currentL = i + 1; 59 | } 60 | } 61 | 62 | if (((totalR - totalL + 1) > diff) && ((totalR - totalL + 1) > 0)) { 63 | diff = totalR - totalL + 1; 64 | totalLF = totalL; 65 | totalRF = totalR; 66 | } 67 | } 68 | 69 | if (diff != -1) { 70 | ArrayList ans = new ArrayList<>(); 71 | for (int i = totalLF; i <= totalRF; i++) { 72 | ans.add(A.get(i)); 73 | } 74 | return ans; 75 | } 76 | return new ArrayList<>(); 77 | } 78 | 79 | public static ArrayList maxsetBetter(ArrayList A) { 80 | long maxSum = 0; 81 | long newSum = 0; 82 | 83 | ArrayList newAL = new ArrayList(); 84 | ArrayList maxAL = new ArrayList(); 85 | 86 | for (Integer i : A) { 87 | 88 | if (i >= 0) { 89 | newSum = newSum + i; 90 | newAL.add(i); 91 | } else { 92 | newSum = 0; 93 | newAL = new ArrayList(); 94 | } 95 | 96 | if ((maxSum < newSum) || ((maxSum == newSum) ) && (newAL.size() > maxAL.size())) { 97 | maxSum = newSum; 98 | maxAL = newAL; 99 | } 100 | 101 | } 102 | return maxAL; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /coreJava/dynamicProgramming/dynamicProgramming5/LongestPalindropicSubsequence.java: -------------------------------------------------------------------------------- 1 | package coreJava.dynamicProgramming.dynamicProgramming5; 2 | 3 | public class LongestPalindropicSubsequence { 4 | 5 | public static void main(String[] args) { 6 | String s1 = "abckczbycbhehfjeuhjkjkhajafhewkj"; 7 | // int ans = longestPalindropicSubsequence(s1); 8 | // System.out.println(ans); 9 | int ans2 = longestPalindropicSubsequenceM(s1, 0, s1.length() - 1, new int[s1.length() + 1][s1.length() + 1]); 10 | System.out.println(ans2); 11 | int ans3 = longestPalindropicSubsequenceT(s1); 12 | System.out.println(ans3); 13 | } 14 | 15 | public static int longestPalindropicSubsequence(String s1) { 16 | 17 | if (s1.length() == 0 || s1.length() == 1) { 18 | return s1.length(); 19 | } 20 | 21 | int ans = 0; 22 | char chF = s1.charAt(0); 23 | char chL = s1.charAt(s1.length() - 1); 24 | String rest = s1.substring(1, s1.length() - 1); 25 | 26 | if (chF == chL) { 27 | ans = 2 + longestPalindropicSubsequence(rest); 28 | } else { 29 | int f1 = longestPalindropicSubsequence(rest + chL); 30 | int f2 = longestPalindropicSubsequence(chF + rest); 31 | ans = Math.max(f1, f2); 32 | } 33 | return ans; 34 | } 35 | 36 | public static int longestPalindropicSubsequenceM(String s1, int i, int j, int[][] qb) { 37 | 38 | if (i > j || i == j) { 39 | return i == j ? 1 : 0; 40 | } 41 | 42 | if (qb[i][j] != 0) { 43 | return qb[i][j]; 44 | } 45 | 46 | int ans = 0; 47 | char chF = s1.charAt(i); 48 | char chL = s1.charAt(j); 49 | 50 | if (chF == chL) { 51 | ans = 2 + longestPalindropicSubsequenceM(s1, i + 1, j - 1, qb); 52 | } else { 53 | int f1 = longestPalindropicSubsequenceM(s1, i, j - 1, qb); 54 | int f2 = longestPalindropicSubsequenceM(s1, i + 1, j, qb); 55 | ans = Math.max(f1, f2); 56 | } 57 | qb[i][j] = ans; 58 | return ans; 59 | } 60 | 61 | public static int longestPalindropicSubsequenceT(String s1) { 62 | int[][] ans = new int[s1.length()][s1.length()]; 63 | 64 | for (int gap = 0; gap < s1.length(); gap++) { 65 | for (int i = 0; i + gap < s1.length(); i++) { 66 | int j = i + gap; 67 | if (gap == 0) { 68 | ans[i][j] = 1; 69 | } else if (gap == 1) { 70 | if (s1.charAt(i) == s1.charAt(j)) { 71 | ans[i][j] = 2; 72 | } else { 73 | ans[i][j] = 1; 74 | } 75 | } else { 76 | if (s1.charAt(i) == s1.charAt(j)) { 77 | ans[i][j] = 2 + ans[i + 1][j - 1]; 78 | } else { 79 | ans[i][j] = Math.max(ans[i][j - 1], ans[i + 1][j]); 80 | } 81 | } 82 | j++; 83 | 84 | } 85 | } 86 | 87 | for (int i = ans.length - 2; i >= 0; i--) { 88 | for (int j = ans.length - 2; j >= 0; j--) { 89 | if (s1.charAt(i) == s1.charAt(j)) { 90 | ans[i][j] = 2 + ans[i + 1][j + 1]; 91 | } else { 92 | ans[i][j] = Math.max(ans[i + 1][j], ans[i][j + 1]); 93 | } 94 | } 95 | } 96 | 97 | return ans[0][0]; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /coreJava/recursion/recursionQuesion/CrossWord.java: -------------------------------------------------------------------------------- 1 | package coreJava.recursion.recursionQuesion; 2 | 3 | public class CrossWord { 4 | 5 | public static void main(String[] args) { 6 | char[][] input = { { '+', '-', '+', '+', '+', '+', '+', '+', '+', '+' }, 7 | { '+', '-', '+', '+', '+', '+', '+', '+', '+', '+' }, 8 | { '+', '-', '+', '+', '+', '+', '+', '+', '+', '+' }, 9 | { '+', '-', '-', '-', '-', '-', '+', '+', '+', '+' }, 10 | { '+', '-', '+', '+', '+', '-', '+', '+', '+', '+' }, 11 | { '+', '-', '+', '+', '+', '-', '+', '+', '+', '+' }, 12 | { '+', '+', '+', '+', '+', '-', '+', '+', '+', '+' }, 13 | { '+', '+', '-', '-', '-', '-', '-', '-', '+', '+' }, 14 | { '+', '+', '+', '+', '+', '-', '+', '+', '+', '+' }, 15 | { '+', '+', '+', '+', '+', '-', '+', '+', '+', '+' } }; 16 | String[] words = { "DELHI", "ICELAND", "ANKARA", "LONDON" }; 17 | crossWord(input, words, 0); 18 | } 19 | 20 | public static void crossWord(char[][] input, String[] words, int widx) { 21 | 22 | if (words.length == widx) { 23 | for (int i = 0; i < input.length; i++) { 24 | for (int j = 0; j < input[0].length; j++) { 25 | System.out.print(input[i][j]); 26 | } 27 | System.out.println(); 28 | } 29 | return; 30 | } 31 | 32 | for (int r = 0; r < input.length; r++) { 33 | for (int c = 0; c < input[0].length; c++) { 34 | if (input[r][c] == '-' || input[r][c] == words[widx].charAt(0)) { 35 | if (canPlaceWordHorizontally(input, words[widx], r, c) == true) { 36 | boolean[] reset = placeTheWordHorizontally(input, words[widx], r, c); 37 | crossWord(input, words, widx + 1); 38 | unPlaceTheWordHorizontally(input, reset, r, c); 39 | } 40 | if (canPlaceWordVertically(input, words[widx], r, c) == true) { 41 | boolean[] reset = placeTheWordVertically(input, words[widx], r, c); 42 | crossWord(input, words, widx + 1); 43 | unPlaceTheWordVertically(input, reset, r, c); 44 | } 45 | } 46 | } 47 | } 48 | } 49 | 50 | private static boolean canPlaceWordHorizontally(char[][] input, String word, int r, int c) { 51 | if (c + word.length() > input[0].length) { 52 | return false; 53 | } 54 | 55 | for (int i = 0; i < word.length(); i++) { 56 | if (input[r][c + i] != '-' && input[r][c + i] != word.charAt(i)) { 57 | return false; 58 | } 59 | } 60 | return true; 61 | } 62 | 63 | private static boolean canPlaceWordVertically(char[][] input, String word, int r, int c) { 64 | if (r + word.length() > input.length) { 65 | return false; 66 | } 67 | 68 | for (int i = 0; i < word.length(); i++) { 69 | if (input[r + i][c] != '-' && input[r + i][c] != word.charAt(i)) { 70 | return false; 71 | } 72 | } 73 | return true; 74 | } 75 | 76 | public static boolean[] placeTheWordHorizontally(char[][] input, String word, int r, int c) { 77 | boolean[] reset = new boolean[word.length()]; 78 | for (int i = 0; i < word.length(); i++) { 79 | reset[i] = (input[r][c + i] != word.charAt(i)); 80 | input[r][c + i] = word.charAt(i); 81 | } 82 | return reset; 83 | } 84 | 85 | public static boolean[] placeTheWordVertically(char[][] input, String word, int r, int c) { 86 | boolean[] reset = new boolean[word.length()]; 87 | for (int i = 0; i < word.length(); i++) { 88 | reset[i] = (input[r + i][c] != word.charAt(i)); 89 | input[r + i][c] = word.charAt(i); 90 | } 91 | return reset; 92 | } 93 | 94 | public static void unPlaceTheWordHorizontally(char[][] input, boolean[] reset, int r, int c) { 95 | for (int i = 0; i < reset.length; i++) { 96 | if (reset[i]) { 97 | input[r][c + i] = '-'; 98 | } 99 | } 100 | } 101 | 102 | public static void unPlaceTheWordVertically(char[][] input, boolean[] reset, int r, int c) { 103 | for (int i = 0; i < reset.length; i++) { 104 | if (reset[i]) { 105 | input[r + i][c] = '-'; 106 | } 107 | } 108 | } 109 | } -------------------------------------------------------------------------------- /dataStructures/linkedlist/LinkedListG.java: -------------------------------------------------------------------------------- 1 | package dataStructures.linkedlist; 2 | 3 | public class LinkedListG { 4 | 5 | private class Node { 6 | int data; 7 | Node next; 8 | } 9 | 10 | private Node head; 11 | private Node tail; 12 | private int size; 13 | 14 | public int size() { 15 | return this.size; 16 | } 17 | 18 | public boolean isEmpty() { 19 | return this.size == 0; 20 | } 21 | 22 | public void addLast(int data) { 23 | // when list is empty 24 | if (this.size == 0) { 25 | this.handleAddWhenSize0(data); 26 | return; 27 | } 28 | 29 | // create 30 | Node node = new Node(); 31 | 32 | // set properties 33 | node.data = data; 34 | node.next = null; 35 | 36 | // attach 37 | tail.next = node; 38 | 39 | // summary update 40 | tail = node; 41 | this.size++; 42 | } 43 | 44 | private void handleAddWhenSize0(int data) { 45 | Node node = new Node(); 46 | 47 | node.data = data; 48 | node.next = null; 49 | 50 | this.head = this.tail = node; 51 | this.size++; 52 | } 53 | 54 | public void display() { 55 | Node temp = this.head; 56 | while (temp != null) { 57 | System.out.print(temp.data + " -->> "); 58 | temp = temp.next; 59 | } 60 | System.out.println(); 61 | } 62 | 63 | public void addFirst(int data) { 64 | if (this.size == 0) { 65 | this.handleAddWhenSize0(data); 66 | return; 67 | } 68 | 69 | // create 70 | Node node = new Node(); 71 | 72 | // set properties 73 | node.data = data; 74 | node.next = this.head; 75 | 76 | // attach 77 | 78 | // summary update 79 | this.head = node; 80 | this.size++; 81 | } 82 | 83 | public int getFirst() throws Exception { 84 | if (this.size == 0) { 85 | throw new Exception("Size is empty"); 86 | } 87 | return head.data; 88 | } 89 | 90 | public int getLast() throws Exception { 91 | if (this.size == 0) { 92 | throw new Exception("Size is empty"); 93 | } 94 | return tail.data; 95 | } 96 | 97 | public int getAt(int idx) throws Exception { 98 | if (this.size == 0) { 99 | throw new Exception("Size is empty"); 100 | } else if (idx < 0 || idx >= this.size) { 101 | throw new Exception("Index out of Bound"); 102 | } 103 | 104 | Node node = head; 105 | for (int i = 0; i < idx; i++) { 106 | node = node.next; 107 | } 108 | return node.data; 109 | } 110 | 111 | private Node getNode(int idx) throws Exception { 112 | if (this.size == 0) { 113 | throw new Exception("Size is empty"); 114 | } else if (idx < 0 || idx >= this.size) { 115 | throw new Exception("Index out of Bound"); 116 | } 117 | 118 | Node node = head; 119 | for (int i = 0; i < idx; i++) { 120 | node = node.next; 121 | } 122 | return node; 123 | } 124 | 125 | public void addAt(int data, int idx) throws Exception { 126 | if (idx < 0 || idx > this.size) { 127 | throw new Exception("Index out of Bound"); 128 | } else if (idx == 0) { 129 | addFirst(data); 130 | } else if (idx == this.size) { 131 | addLast(data); 132 | } else { 133 | 134 | Node nodeBefore = this.getNode(idx - 1); 135 | Node nodeafter = nodeBefore.next; 136 | 137 | Node node = new Node(); 138 | node.data = data; 139 | 140 | nodeBefore.next = node; 141 | node.next = nodeafter; 142 | 143 | this.size++; 144 | } 145 | } 146 | 147 | public int removeFirst() throws Exception { 148 | if (this.size == 0) { 149 | throw new Exception("Size is empty"); 150 | } else if (this.size == 1) { 151 | this.size--; 152 | this.head = this.tail = null; 153 | } 154 | 155 | int temp = head.data; 156 | 157 | Node second = head.next; 158 | head = second; 159 | this.size--; 160 | return temp; 161 | } 162 | 163 | public int removeLast() throws Exception { 164 | if (this.size == 0) { 165 | throw new Exception("Size is empty"); 166 | } else if (this.size == 1) { 167 | this.size--; 168 | this.head = this.tail = null; 169 | } 170 | 171 | int temp = tail.data; 172 | 173 | Node secondlast = this.getNode(this.size - 2); 174 | tail = secondlast; 175 | tail.next = null; 176 | this.size--; 177 | 178 | return temp; 179 | } 180 | 181 | public int removeAt(int idx) throws Exception { 182 | if (this.size == 0) { 183 | throw new Exception("Size is empty"); 184 | } else if (idx < 0 || idx >= this.size) { 185 | throw new Exception("Index out of Bound"); 186 | } else if (idx == 0) { 187 | return this.removeFirst(); 188 | } else if (idx == this.size - 1) { 189 | return this.removeLast(); 190 | } else { 191 | 192 | Node nodeBefore = this.getNode(idx - 1); 193 | Node node = nodeBefore.next; 194 | Node nodeAfter = node.next; 195 | 196 | nodeBefore.next = nodeAfter; 197 | this.size--; 198 | 199 | return node.data; 200 | 201 | } 202 | } 203 | 204 | } 205 | -------------------------------------------------------------------------------- /coreJava/README.md: -------------------------------------------------------------------------------- 1 | ### [Bit Manipulation]() 2 | [Is Kth Bit ON or OFF](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/IsKthBitONorOFF.java)
3 | [Turn the bit on](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/TurnTheBitON.java)
4 | [Turn the bit off](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/TurnTheBitOFF.java)
5 | [Toggle the bit](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/ToggleTheBit.java)
6 | [Print bits](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/PrintBits.java)
7 | 8 | ### [Arrays](https://github.com/Sandip75/Programming-in-Java/tree/master/coreJava/arrays) 9 | [Maximum in an array](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/MaximumInArray.java)
10 | [Linear Search](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/LinearSearch.java)
11 | [Binary Search](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/BinarySearch.java)
12 | [Bubble Sort](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/BubbleSort.java)
13 | [Selection Sort](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/SelectionSort.java)
14 | [Insertion Sort](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/InsertionSort.java)
15 | [Reverse array](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/ReverseArray.java)
16 | [Inverse of an array](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/InverseArray.java)
17 | [Is Array mirror-inverse](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/IsArrayMirrorInverse.java)
18 | [Rotate an array](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/RotateAnArray.java)
19 | [Kadane's algorithm](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/KadanesAlgorithm.java)
20 | [Matrix Multiplication](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/MatrixMultiplication.java)
21 | [Wave Display](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/WaveDisplay.java)
22 | [Spiral Display](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/SprialDisplay.java)
23 | [Exit Point]()
24 | [90 degree rotate]()
25 | 26 | ### [String](https://github.com/Sandip75/Programming-in-Java/tree/master/coreJava/string)
27 | [Print Characters](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/string/PrintCharacter.java)
28 | [Print Substrings](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/string/PrintSubstring.java)
29 | [IsPalindrome](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/string/IsPalindrome.java)
30 | [Print Palindromic susbtrings](https://goo.gl/aVnBJU)
31 | [Toggle Characters](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/string/ToggleCharacters.java)
32 | [Modify a string](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/string/ModifyString.java)
33 | [Modify a string2](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/string/ModifyEvenPandOddM.java)
34 | [Print subsequence]()
35 | [Compress a string]()
36 | 37 | ### [Recursion]()
38 | 39 | ### [Recursion basics]() 40 | [Print Decreasing]()
41 | [Print Increasing]()
42 | [Print Decreasing Increasing]()
43 | [Factorial]()
44 | [Power]()
45 | [Tower of Hanoi]()
46 | 47 | ### [Recursion with arrays]() 48 | [Display Array]()
49 | [Dislay Array in Reverse]()
50 | [Find largest element in an array]()
51 | [Find whether an element occurs in an array or not]()
52 | [Find first index where an element occurs in an array]()
53 | [Find last index where an element occurs in an array]()
54 | [Find all indices where an element occurs in an array]()
55 | 56 | ### [Recursion with arraylists]() 57 | [Get Subsequences]()
58 | [Get Subsequence with ascii]()
59 | [Get Keypad combinations]()
60 | 61 | ### [Recursion in board games]() 62 | [Paths in a board game]()
63 | [Paths in a board game - opening with 1 and 6 on dice]()
64 | [Paths in a board game with ladders]()
65 | [Paths in a board game with snakes and ladders]()
66 | 67 | ### [Recursion in maze games]() 68 | [Paths in a maze]()
69 | [Paths in a maze - diagonal move allowed]()
70 | [Paths in a maze - multistep moves allowed]()
71 | [Find any path in a maze - all direction moves allowed]()
72 | [Find all paths in a maze - all direction moves allowed]()
73 | 74 | ### [Recursion in permuations and combinations]() 75 | [Permutations of item in boxes]()
76 | [Combinations of item in boxes]()
77 | [NQueens]()
78 | [NKnights]()
79 | 80 | ### [Recursion and memory]() 81 | [Print Subset with items summing to a target ]()
82 | [Print pair of subsets with equal sum]()
83 | 84 | ### [Recursion Question]() 85 | [CrossWord]()
86 | [Sudoku]()
87 | 88 | -------------------------------------------------------------------------------- /coreJava/bitManipulation/README.md: -------------------------------------------------------------------------------- 1 | ## 1) Is Kth Bit ON or OFF
2 | You are give two input N and K. N is an integer number you need to check Kth bit is ON or OFF 3 | ### Input 4 | Enter a number N 5 | Enter the value of K 6 | ### Output 7 | "ON" or "OFF" 8 | 9 | ### Sample input 1 10 | 75 11 | 4 12 | ### Sample Output 1 13 | OFF 14 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/IsKthBitONorOFF.java) 15 |

16 | 17 | 18 | ## 2) Turn the bit ON
19 | You are give two input N and K. N is an integer number you need to set Kth bit. 20 | ### Input 21 | Enter a number N 22 | Enter the value of K 23 | ### Output 24 | return a number 25 | 26 | ### Sample input 1 27 | 75 28 | 2 29 | ### Sample Output 1 30 | 79 31 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/TurnTheBitON.java) 32 |

33 | 34 | ## 3) Turn the bit OFF
35 | You are give two input N and K. N is an integer number you need to unset Kth bit. 36 | ### Input 37 | Enter a number N 38 | Enter the value of K 39 | ### Output 40 | return a number 41 | 42 | ### Sample input 1 43 | 75 44 | 3 45 | ### Sample Output 1 46 | 67 47 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/TurnTheBitOFF.java) 48 |

49 | 50 | ## 4) Toggle the bit
51 | You are give two input N and K. N is an integer number you need to toggle( if it is Zero make it one or vice versa) Kth bit. 52 | ### Input 53 | Enter a number N 54 | Enter the value of K 55 | ### Output 56 | return a number 57 | 58 | ### Sample input 1 59 | 75 60 | 4 61 | ### Sample Output 1 62 | 91 63 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/ToggleTheBit.java) 64 |

65 | 66 | ## 5) Print bits
67 | You are give input N. N is an integer number you need to print in bits. 68 | ### Input 69 | Enter a number N 70 | ### Output 71 | print bit of a Number N 72 | 73 | ### Sample input 1 74 | 75 75 | ### Sample Output 1 76 | 00000000000000000000000001001011 77 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/PrintBits.java) 78 |

79 | 80 | ## 6) Count No. of One's
81 | You are give input N. N is an integer number you need to count number of one's bit in N. 82 | ### Input 83 | Enter a number N 84 | ### Output 85 | return number of one's bit in N 86 | 87 | ### Sample input 1 88 | 75 89 | ### Sample Output 1 90 | 4 91 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/CountOne.java) 92 |

93 | 94 | 95 | ## 7) Find unique number in array
96 | You are give an array of N size where N is odd number. All number are duplicate in an array except one number. You need to find that number 97 | ### Input 98 | Enter a number N 99 | There will be N numbers, ith number in the line is denoted by A[i] 100 | ### Output 101 | return unique number 102 | 103 | ### Sample input 1 104 | 7 105 | [ 3 , 7 ,9 , 11 , 7 ,3 ,9 ] 106 | ### Sample Output 1 107 | 11 108 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/UniqueNumber.java) 109 |

110 | 111 | ## 8) Find two unique number in array
112 | You are give an array of N size where N is even number. All number are duplicate in an array except two number. You need to find that number 113 | ### Input 114 | Enter a number N 115 | There will be N numbers, ith number in the line is denoted by A[i] 116 | ### Output 117 | print first unique number 118 | print other unique number 119 | 120 | ### Sample input 1 121 | 6 122 | [ 3 , 17 ,88 , 11 , 17 ,88 ] 123 | ### Sample Output 1 124 | 3 125 | 11 126 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/UniqueNumberTwo.java) 127 |

128 | 129 | 130 | ## 9) Find unique number in array
131 | You are give an array of N size where N is number. All number are thrice in an array except one number. You need to find that number 132 | ### Input 133 | Enter a number N 134 | There will be N numbers, ith number in the line is denoted by A[i] 135 | ### Output 136 | return unique number 137 | 138 | ### Sample input 1 139 | 7 140 | [ 3 , 7 ,9 , 3 , 9 ,3 ,9 ] 141 | ### Sample Output 1 142 | 7 143 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/bitManipulation/UniqueNumberTwo.java) 144 |

145 | 146 | ## 10) Subsequences
147 | Write a function that prints all its subsequences. 148 | ### Input 149 | Enter a String S 150 | ### Output 151 | print all possible output 152 | 153 | ### Sample input 1 154 | abc 155 | ### Sample Output 1 156 | 157 | c 158 | b 159 | bc 160 | a 161 | ac 162 | ab 163 | abc 164 | ### [Solution]() 165 |

166 | 167 | ## 11) Sudoku using bit
168 | -------------------------------------------------------------------------------- /coreJava/arrays/oneD/README.md: -------------------------------------------------------------------------------- 1 | ## 1) Maximum in an array
2 | You are give an array of size N. N is an integer number you need to find maximum value in given array. 3 | ### Input 4 | Enter an array 5 | ### Output 6 | return a number 7 | 8 | ### Sample input 1 9 | [ 3 , 7 , 1 , 6 , 4 ] 10 | ### Sample Output 1 11 | 7 12 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/MaximumInArray.java) 13 |

14 | 15 | ## 2) Linear Search
16 | You are give an array of size N. N is an integer number you need to find an number in given array. 17 | ### Input 18 | Enter an array 19 | Enter an number you want to search 20 | ### Output 21 | return index of an number 22 | 23 | ### Sample input 1 24 | [ 3 , 7 , 1 , 6 , 4 ] 25 | 6 26 | ### Sample Output 1 27 | 3 28 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/LinearSearch.java) 29 |

30 | 31 | 32 | ## 3) Binary Search
33 | You are give an sorted array of size N. N is an integer number you need to find an number in given array. 34 | ### Input 35 | Enter an array 36 | Enter an number you want to search 37 | ### Output 38 | return index of an number 39 | 40 | ### Sample input 1 41 | [ 3 , 6 , 7 , 16 , 24 ] 42 | 6 43 | ### Sample Output 1 44 | 1 45 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/BinarySearch.java) 46 |

47 | 48 | 49 | ## 4) Bubble Sort
50 | You are give an array of size N. N is an integer number you need to sort given array. 51 | ### Input 52 | Enter an array 53 | ### Output 54 | return an array number 55 | 56 | ### Sample input 1 57 | [ 3 , 7 , 1 , 6 , 4 ] 58 | ### Sample Output 1 59 | [ 1 , 3 , 4 , 6 , 7 ] 60 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/BubbleSort.java) 61 |

62 | 63 | 64 | ## 5) Selection Sort
65 | You are give an array of size N. N is an integer number you need to sort given array. 66 | ### Input 67 | Enter an array 68 | ### Output 69 | return an array number 70 | 71 | ### Sample input 1 72 | [ 3 , 7 , 1 , 6 , 4 ] 73 | ### Sample Output 1 74 | [ 1 , 3 , 4 , 6 , 7 ] 75 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/SelectionSort.java) 76 |

77 | 78 | ## 6) Insertion Sort
79 | You are give an array of size N. N is an integer number you need to sort given array. 80 | ### Input 81 | Enter an array 82 | ### Output 83 | return an array number 84 | 85 | ### Sample input 1 86 | [ 3 , 7 , 1 , 6 , 4 ] 87 | ### Sample Output 1 88 | [ 1 , 3 , 4 , 6 , 7 ] 89 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/InsertionSort.java) 90 |

91 | 92 | ## 7) Reverse array
93 | You are give an array of size N. N is an integer number you need to sort given array. 94 | ### Input 95 | Enter an array 96 | ### Output 97 | return an array number 98 | 99 | ### Sample input 1 100 | [ 3 , 7 , 1 , 6 , 4 ] 101 | ### Sample Output 1 102 | [ 4 , 6 , 1 , 7 , 3 ] 103 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/ReverseArray.java) 104 |

105 | 106 | 107 | ## 8) Inverse of an array
108 | You are give an array of size N. N is an integer number you need to Inverse given array. 109 | ### Input 110 | Enter an array 111 | ### Output 112 | return an array number 113 | 114 | ### Sample input 1 115 | [ 3 , 0 , 1 , 4 , 2 ] 116 | ### Sample Output 1 117 | [ 1 , 2 , 4 , 0 , 3 ] 118 |
119 | Index zero has three value So we will store zero at index three
120 | Index one has zero value So we will store one at index zero and So on... 121 | 122 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/InverseArray.java) 123 |

124 | 125 | 126 | ## 9) Is Array mirror-inverse
127 | You are give an array of size N. N is an integer number you need to check Inverse is possible or not. ( same as above question) 128 | ### Input 129 | Enter an array 130 | ### Output 131 | Yes or NO 132 | 133 | ### Sample input 1 134 | [ 3 , 0 , 1 , 4 , 2 ] 135 | ### Sample Output 1 136 | Yes 137 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/IsArrayMirrorInverse.java) 138 |

139 | 140 | ## 10) Rotate an array
141 | You are give an array of size N and K value. N and k is an integer number you need to do left rotation K times. 142 | ### Input 143 | Enter an array 144 | Enter value of k 145 | ### Output 146 | return an array number 147 | 148 | ### Sample input 1 149 | [ 3 , 7 , 1 , 6 , 4 ] 150 | 3 151 | ### Sample Output 1 152 | [ 1 , 6 , 4 , 3 , 7 ] 153 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/RotateAnArray.java) 154 |

155 | 156 | 157 | ## 2) Kadane's algorithm
158 | You are give an array of size N. N is an integer number you need to find subarray with maximum value in given array. 159 | ### Input 160 | Enter an array 161 | ### Output 162 | return a number 163 | 164 | ### Sample input 1 165 | [ 2 , 3 , -6 , 1 , 2 , 3 , -4 , 5 ] 166 | ### Sample Output 1 167 | 7 168 | ### [Solution](https://github.com/Sandip75/Programming-in-Java/blob/master/coreJava/arrays/KadanesAlgorithm.java) 169 |

170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /dataStructures/linkedlist/LinkedListH.java: -------------------------------------------------------------------------------- 1 | package dataStructures.linkedlist; 2 | 3 | public class LinkedListH { 4 | private class Node { 5 | int data; 6 | Node next; 7 | } 8 | 9 | Node head; 10 | Node tail; 11 | int size; 12 | 13 | public void addLast(int data) { 14 | Node node = new Node(); 15 | node.data = data; 16 | if (this.size == 0) { 17 | this.head = this.tail = node; 18 | this.size++; 19 | } else { 20 | tail.next = node; 21 | tail = node; 22 | this.size++; 23 | } 24 | } 25 | 26 | public int size() { 27 | return this.size; 28 | } 29 | 30 | public boolean isEmpty() { 31 | return this.size() == 0; 32 | } 33 | 34 | public void display() { 35 | for (Node node = head; node != null; node = node.next) { 36 | System.out.print(node.data + " "); 37 | } 38 | System.out.println("<<<------->>>"); 39 | } 40 | 41 | public void addFirst(int data) { 42 | if (this.size() == 0) { 43 | addLast(data); 44 | return; 45 | } 46 | Node node = new Node(); 47 | node.data = data; 48 | node.next = head; 49 | head = node; 50 | this.size++; 51 | } 52 | 53 | public int getFirst() { 54 | if (this.size() == 0) { 55 | System.out.println("No Data"); 56 | return -1; 57 | } 58 | return head.data; 59 | } 60 | 61 | public int getLast() { 62 | if (this.size() == 0) { 63 | System.out.println("No Data"); 64 | return -1; 65 | } 66 | return tail.data; 67 | } 68 | 69 | public int getAt(int idx) { 70 | if (idx < 0) { 71 | return -1; 72 | } else if (idx >= this.size) { 73 | return -1; 74 | } 75 | Node temp = head; 76 | for (int i = 0; i < idx; i++) { 77 | temp = temp.next; 78 | } 79 | return temp.data; 80 | } 81 | 82 | public int removeFirst() { 83 | if (this.size == 0) { 84 | return -1; 85 | } else if (this.size() == 1) { 86 | Node node = head; 87 | head = tail = null; 88 | return node.data; 89 | } 90 | int tempdata = head.data; 91 | Node temp = head; 92 | head = temp.next; 93 | temp.next = null; 94 | this.size--; 95 | return tempdata; 96 | } 97 | 98 | public int removeLast() { 99 | if (this.size == 0) { 100 | return -1; 101 | } else if (this.size() == 1) { 102 | Node node = head; 103 | head = tail = null; 104 | this.size--; 105 | return node.data; 106 | } 107 | int tempdata = tail.data; 108 | Node temp = head; 109 | for (int i = 0; i < (this.size() - 2); i++) { 110 | temp = temp.next; 111 | } 112 | tail = temp; 113 | temp.next = null; 114 | this.size--; 115 | return tempdata; 116 | } 117 | 118 | public void addAt(int idx, int data) { 119 | if (idx < 0 || idx > this.size()) { 120 | return; 121 | } 122 | if (idx == 0) { 123 | addFirst(data); 124 | return; 125 | } 126 | if (idx == this.size()) { 127 | addLast(data); 128 | return; 129 | } 130 | 131 | Node n = new Node(); 132 | n.data = data; 133 | Node m1 = getNode(idx - 1); 134 | Node p1 = m1.next; 135 | 136 | m1.next = n; 137 | n.next = p1; 138 | this.size++; 139 | } 140 | 141 | public int removeAt(int idx) { 142 | if (idx < 0 || idx >= this.size()) { 143 | return -1; 144 | } else if (idx == 0) { 145 | return removeFirst(); 146 | } else if (idx == this.size() - 1) { 147 | return removeLast(); 148 | } 149 | Node m1 = getNode(idx - 1); 150 | Node n = m1.next; 151 | Node p1 = n.next; 152 | 153 | m1.next = p1; 154 | this.size--; 155 | return n.data; 156 | } 157 | 158 | private Node getNode(int idx) { 159 | Node temp = head; 160 | for (int i = 0; i < idx; i++) { 161 | temp = temp.next; 162 | } 163 | return temp; 164 | } 165 | 166 | // Question 167 | 168 | public void reverseDataItertive() { 169 | Node l = head; 170 | Node r = tail; 171 | for (int i = 0; i < (this.size() / 2); i++) { 172 | int temp = r.data; 173 | r.data = l.data; 174 | l.data = temp; 175 | 176 | l = l.next; 177 | r = getNode(this.size() - i - 2); 178 | } 179 | } 180 | 181 | public void reversePointerItertive() { 182 | Node m1 = head; 183 | Node n = m1.next; 184 | Node p1 = n.next; 185 | 186 | for (int i = 0; i < this.size() - 1; i++) { 187 | n.next = m1; 188 | m1 = n; 189 | n = p1; 190 | if (p1 != null) { 191 | p1 = p1.next; 192 | } 193 | } 194 | // n.next = m1; 195 | // p1.next = n; 196 | Node temp = tail; 197 | tail = head; 198 | tail.next = null; 199 | head = temp; 200 | } 201 | 202 | public boolean IsPalindrome() { 203 | IsPalindromeLeft = head; 204 | return IsPalindrome(head); 205 | 206 | } 207 | 208 | private Node IsPalindromeLeft; 209 | 210 | private boolean IsPalindrome(Node right) { 211 | if (right == null) { 212 | return true; 213 | } 214 | boolean res = IsPalindrome(right.next); 215 | if (res == true) { 216 | if (IsPalindromeLeft.data == right.data) { 217 | IsPalindromeLeft = IsPalindromeLeft.next; 218 | return true; 219 | } else { 220 | return false; 221 | } 222 | } else { 223 | return false; 224 | } 225 | } 226 | 227 | public void fold() { 228 | foldleft = head; 229 | fold(head, 0); 230 | } 231 | 232 | private Node foldleft; 233 | 234 | private void fold(Node right, int s) { 235 | if (right == null) { 236 | return; 237 | } 238 | fold(right.next, s + 1); 239 | 240 | if (s > size / 2) { 241 | Node temp = foldleft.next; 242 | foldleft.next = right; 243 | right.next = temp; 244 | foldleft = temp; 245 | } 246 | if (s == size / 2) { 247 | tail = right; 248 | tail.next = null; 249 | } 250 | } 251 | 252 | public int mid() { 253 | Node slow = head; 254 | Node fast = head.next; 255 | 256 | while (fast.next != null) { 257 | slow = slow.next; 258 | fast = fast.next; 259 | if (fast.next != null) { 260 | fast = fast.next; 261 | } 262 | } 263 | return slow.data; 264 | } 265 | 266 | private Node midNode() { 267 | Node slow = head; 268 | Node fast = head; 269 | 270 | while (fast.next != null && fast.next.next != null) { 271 | slow = slow.next; 272 | fast = fast.next.next; 273 | } 274 | return slow; 275 | } 276 | 277 | public int kthFromLast(int k) { 278 | Node slow = head; 279 | Node fast = head; 280 | for (int i = 0; i < k; i++) { 281 | fast = fast.next; 282 | } 283 | while (fast != null) { 284 | slow = slow.next; 285 | fast = fast.next; 286 | } 287 | return slow.data; 288 | } 289 | 290 | public static LinkedListH mergeTwoSortedLists(LinkedListH l1, LinkedListH l2) { 291 | LinkedListH l3 = new LinkedListH(); 292 | 293 | Node Fll = l1.head; 294 | Node Sll = l2.head; 295 | while (Fll != null && Sll != null) { 296 | if (Fll.data < Sll.data) { 297 | l3.addLast(Fll.data); 298 | Fll = Fll.next; 299 | } else { 300 | l3.addLast(Sll.data); 301 | Sll = Sll.next; 302 | } 303 | } 304 | while (Sll != null) { 305 | l3.addLast(Sll.data); 306 | Sll = Sll.next; 307 | } 308 | while (Fll != null) { 309 | l3.addLast(Fll.data); 310 | Fll = Fll.next; 311 | } 312 | return l3; 313 | } 314 | 315 | public static LinkedListH mergSort(LinkedListH list) { 316 | if (list.head == list.tail) { 317 | return list; 318 | } 319 | 320 | Node mid = list.midNode(); 321 | Node midN = mid.next; 322 | 323 | LinkedListH list1 = new LinkedListH(); 324 | list1.head = list.head; 325 | list1.tail = mid; 326 | 327 | LinkedListH list2 = new LinkedListH(); 328 | list2.head = mid.next; 329 | list2.tail = list.tail; 330 | mid.next = null; 331 | 332 | list1 = mergSort(list1); 333 | list2 = mergSort(list2); 334 | LinkedListH ll = mergeTwoSortedLists(list1, list2); 335 | 336 | mid.next = midN; 337 | 338 | return ll; 339 | } 340 | 341 | public void addFirstNode(Node node) { 342 | if (this.size() == 0) { 343 | head = tail = node; 344 | size = 1; 345 | return; 346 | } 347 | node.next = head; 348 | head = node; 349 | this.size++; 350 | } 351 | 352 | public Node removeFirstNode() { 353 | if (this.size == 0) { 354 | return null; 355 | } else if (this.size() == 1) { 356 | Node node = head; 357 | head = tail = null; 358 | size--; 359 | return node; 360 | } 361 | 362 | Node temp = head; 363 | head = head.next; 364 | temp.next = null; // garbage will not clear 365 | this.size--; 366 | return temp; 367 | } 368 | 369 | public void kthReverse(int k) { 370 | 371 | LinkedListH p = new LinkedListH(); 372 | LinkedListH c = null; 373 | 374 | while (this.size != 0) { 375 | 376 | for (int i = 0; i < k; i++) { 377 | p.addFirstNode(this.removeFirstNode()); 378 | } 379 | 380 | if (c == null) { 381 | c = p; 382 | } else { 383 | c.tail.next = p.head; 384 | c.tail = p.tail; 385 | c.size = c.size + p.size; 386 | } 387 | 388 | p = new LinkedListH(); 389 | } 390 | this.head = c.head; 391 | this.tail = c.tail; 392 | this.size = c.size; 393 | } 394 | 395 | public void KthReverse2() { 396 | 397 | } 398 | 399 | public void removeDuplicates() { 400 | Node nodeF = head; 401 | Node nodeS = head.next; 402 | 403 | while (nodeS.next != null) { 404 | if (nodeF.data == nodeS.data) { 405 | nodeS = nodeS.next; 406 | } else { 407 | nodeF.next = nodeS; 408 | 409 | nodeF = nodeS; 410 | nodeS = nodeF.next; 411 | } 412 | 413 | if (nodeS.next == null && nodeF.data == nodeS.data) { 414 | nodeF.next = null; 415 | } 416 | } 417 | } 418 | 419 | public void addLastNode(Node node) { 420 | 421 | if (this.size == 0) { 422 | this.head = this.tail = node; 423 | this.size++; 424 | } else { 425 | tail.next = node; 426 | tail = node; 427 | this.size++; 428 | } 429 | } 430 | 431 | public void oddEven() { 432 | 433 | LinkedListH odd = new LinkedListH(); 434 | LinkedListH even = new LinkedListH(); 435 | 436 | while (this.size != 0) { 437 | Node temp = this.removeFirstNode(); 438 | 439 | if (temp.data % 2 == 0) { 440 | even.addLastNode(temp); 441 | } else { 442 | odd.addLastNode(temp); 443 | 444 | } 445 | 446 | } 447 | if (odd.size() > 0 && even.size() > 0) { 448 | odd.size = odd.size + even.size; 449 | odd.tail.next = even.head; 450 | odd.tail = even.tail; 451 | 452 | this.head = odd.head; 453 | this.tail = odd.tail; 454 | this.size = odd.size; 455 | } else if (odd.size() > 0) { 456 | this.head = odd.head; 457 | this.tail = odd.tail; 458 | this.size = odd.size; 459 | } else { 460 | this.head = even.head; 461 | this.tail = even.tail; 462 | this.size = even.size; 463 | 464 | } 465 | } 466 | 467 | public static Node commonPoint(LinkedListH l1, LinkedListH l2) { 468 | 469 | Node n1 = l1.head; 470 | Node n2 = l2.head; 471 | 472 | if (l1.size > l2.size) { 473 | int k = (l1.size - l2.size); 474 | while (k != 0) { 475 | n1 = n1.next; 476 | k--; 477 | } 478 | } else { 479 | int k = (l2.size - l1.size); 480 | while (k != 0) { 481 | n2 = n2.next; 482 | k--; 483 | } 484 | } 485 | 486 | } 487 | 488 | private Node left; 489 | 490 | public void reverseDataRecursive() { 491 | left = head; 492 | reverseDataRecursiveHelper(head, 0); 493 | } 494 | 495 | private void reverseDataRecursiveHelper(Node right, int i) { 496 | if (right == null) { 497 | return; 498 | } 499 | reverseDataRecursiveHelper(right.next , i + 1); 500 | if (i >= size / 2) { 501 | int temp = left.data; 502 | left.data = right.data; 503 | right.data = temp; 504 | } 505 | left = left.next; 506 | 507 | } 508 | 509 | } 510 | -------------------------------------------------------------------------------- /dataStructures/genericTree/GenericTreeH.java: -------------------------------------------------------------------------------- 1 | package dataStructures.genericTree; 2 | 3 | import java.util.*; 4 | 5 | public class GenericTreeH { 6 | 7 | private class Node { 8 | int data; 9 | ArrayList children = new ArrayList<>(); 10 | } 11 | 12 | private Node root; 13 | private int size; 14 | 15 | public GenericTreeH(int[] arr) { 16 | Stack stack = new Stack<>(); 17 | for (int i = 0; i < arr.length; i++) { 18 | if (arr[i] == -1) { 19 | stack.pop(); 20 | } else { 21 | Node node = new Node(); 22 | this.size++; 23 | node.data = arr[i]; 24 | if (stack.size() > 0) { 25 | stack.peek().children.add(node); 26 | stack.push(node); 27 | } else { 28 | root = node; 29 | stack.push(node); 30 | } 31 | } 32 | } 33 | } 34 | 35 | public void display() { 36 | display(root); 37 | } 38 | 39 | private void display(Node node) { 40 | String s = node.data + "-->>"; 41 | for (Node child : node.children) { 42 | s += child.data + " "; 43 | } 44 | s += "."; 45 | System.out.println(s); 46 | for (Node child : node.children) { 47 | display(child); 48 | } 49 | } 50 | 51 | public int size2() { 52 | return size2(root); 53 | } 54 | 55 | private int size2(Node node) { 56 | int t = 0; 57 | for (Node child : node.children) { 58 | t += size2(child); 59 | } 60 | return t + 1; 61 | } 62 | 63 | public int max() { 64 | return max(root); 65 | } 66 | 67 | private int max(Node node) { 68 | int max = node.data; 69 | for (Node child : node.children) { 70 | int tmax = max(child); 71 | if (tmax > max) { 72 | max = tmax; 73 | } 74 | } 75 | return max; 76 | } 77 | 78 | public int height() { 79 | return height(root); 80 | } 81 | 82 | private int height(Node node) { 83 | int ans = 0; 84 | 85 | for (Node child : node.children) { 86 | int th = height(child); 87 | if (ans < th) { 88 | ans = th; 89 | } 90 | } 91 | return ans + 1; 92 | } 93 | 94 | public boolean find(int data) { 95 | return find(root, data); 96 | } 97 | 98 | private boolean find(Node node, int data) { 99 | if (node.data == data) { 100 | return true; 101 | } 102 | for (Node child : node.children) { 103 | boolean tans = find(child, data); 104 | if (tans == true) { 105 | return tans; 106 | } 107 | } 108 | return false; 109 | } 110 | 111 | public ArrayList nodeToRootPath(int data) { 112 | return nodeToRootPath(root, data); 113 | } 114 | 115 | private ArrayList nodeToRootPath(Node node, int data) { 116 | ArrayList ans = new ArrayList<>(); 117 | if (data == node.data) { 118 | ans.add(node.data); 119 | return ans; 120 | } 121 | for (Node child : node.children) { 122 | ans = nodeToRootPath(child, data); 123 | if (ans.size() != 0) { 124 | ans.add(node.data); 125 | return ans; 126 | } 127 | } 128 | return ans; 129 | } 130 | 131 | public void removeAllLeaf() { 132 | removeAllLeaf(root); 133 | } 134 | 135 | private void removeAllLeaf(Node node) { 136 | 137 | for (int i = node.children.size() - 1; i >= 0; i--) { 138 | Node data = node.children.get(i); 139 | 140 | if (data.children.size() == 0) { 141 | node.children.remove(i); 142 | } else { 143 | removeAllLeaf(data); 144 | } 145 | } 146 | } 147 | 148 | public void mirrorImage() { 149 | mirrorImage(root); 150 | } 151 | 152 | private void mirrorImage(Node node) { 153 | 154 | for (Node child : node.children) { 155 | mirrorImage(child); 156 | } 157 | 158 | int left = 0; 159 | int right = node.children.size() - 1; 160 | while (left < right) { 161 | 162 | Node leftN = node.children.get(left); 163 | Node rightN = node.children.get(right); 164 | 165 | node.children.set(left, rightN); 166 | node.children.set(right, leftN); 167 | 168 | left++; 169 | right--; 170 | } 171 | } 172 | 173 | public void Linearize() { 174 | linearize(root); 175 | } 176 | 177 | private void linearize(Node node) { 178 | 179 | for (Node child : node.children) { 180 | linearize(child); 181 | } 182 | 183 | for (int i = node.children.size() - 1; i > 0; i--) { 184 | Node g = node.children.get(i - 1); 185 | Node r = node.children.remove(i); 186 | Node tail = getTail(g); 187 | tail.children.add(r); 188 | } 189 | } 190 | 191 | private Node getTail(Node node) { 192 | Node tail = node; 193 | while (tail.children.size() != 0) { 194 | tail = tail.children.get(0); 195 | } 196 | return tail; 197 | } 198 | 199 | public void Linearize2() { 200 | linearize2(root); 201 | } 202 | 203 | private Node linearize2(Node node) { 204 | 205 | if (node.children.size() == 0) { 206 | return node; 207 | } 208 | 209 | Node tail = node.children.get(node.children.size() - 1); 210 | Node itail = linearize2(tail); 211 | 212 | while (node.children.size() > 1) { 213 | Node r = node.children.remove(node.children.size() - 1); 214 | Node g = node.children.get(node.children.size() - 1); 215 | Node stail = linearize2(g); 216 | stail.children.add(r); 217 | } 218 | return itail; 219 | } 220 | 221 | public static boolean areSimilarShaped(GenericTreeH gt1, GenericTreeH gt2) { 222 | return areSimilarShaped(gt1.root, gt2.root); 223 | } 224 | 225 | private static boolean areSimilarShaped(Node node1, Node node2) { 226 | if (node1.children.size() != node2.children.size()) { 227 | return false; 228 | } 229 | 230 | for (int i1 = 0, i2 = 0; i1 < node1.children.size() && i2 < node2.children.size(); i1++, i2++) { 231 | boolean ans = areSimilarShaped(node1.children.get(i1), node2.children.get(i2)); 232 | if (ans == false) { 233 | return false; 234 | } 235 | } 236 | 237 | return true; 238 | } 239 | 240 | public static boolean areMirrorImage(GenericTreeH gt1, GenericTreeH gt2) { 241 | return areMirrorImage(gt1.root, gt2.root); 242 | } 243 | 244 | private static boolean areMirrorImage(Node node1, Node node2) { 245 | if (node1.children.size() != node2.children.size()) { 246 | return false; 247 | } 248 | 249 | for (int i1 = 0, i2 = node2.children.size() - 1; i1 < node1.children.size() && i2 >= 0; i1++, i2--) { 250 | boolean ans = areMirrorImage(node1.children.get(i1), node2.children.get(i2)); 251 | if (ans == false) { 252 | return false; 253 | } 254 | } 255 | 256 | return true; 257 | } 258 | 259 | public boolean isSymmetric() { 260 | return GenericTreeH.isSymmetric(this, this); 261 | } 262 | 263 | public static boolean isSymmetric(GenericTreeH gt1, GenericTreeH gt2) { 264 | return GenericTreeH.isSymmetric(gt1.root, gt2.root); 265 | } 266 | 267 | private static boolean isSymmetric(Node node1, Node node2) { 268 | if (node1.children.size() != node2.children.size()) { 269 | return false; 270 | } 271 | int left = 0, right = node2.children.size() - 1; 272 | while (left < right) { 273 | boolean ans = isSymmetric(node1.children.get(left), node2.children.get(right)); 274 | if (ans == false) { 275 | return false; 276 | } 277 | left++; 278 | right--; 279 | } 280 | return true; 281 | } 282 | 283 | private int msSize = 0; 284 | private int msMin = Integer.MAX_VALUE; 285 | private int msMax = Integer.MIN_VALUE; 286 | private int msHeight = 0; 287 | 288 | // Size , min , max , height 289 | public void multiSolver() { 290 | 291 | msSize = 0; 292 | msMin = Integer.MAX_VALUE; 293 | msMax = Integer.MIN_VALUE; 294 | msHeight = 0; 295 | 296 | multiSolver(root, 1); 297 | System.out.println(msSize); 298 | System.out.println(msMin); 299 | System.out.println(msMax); 300 | System.out.println(msHeight); 301 | } 302 | 303 | private void multiSolver(Node node, int depth) { 304 | msSize++; 305 | msMin = Math.min(msMin, node.data); 306 | msMax = Math.max(msMax, node.data); 307 | msHeight = Math.max(msHeight, depth); 308 | 309 | for (Node child : node.children) { 310 | multiSolver(child, depth + 1); 311 | } 312 | } 313 | 314 | private Integer msPred = null; 315 | private Integer msSucc = null; 316 | private Integer pre = null; 317 | private Integer cur = null; 318 | 319 | // pred , succ , 320 | public void multiSolve2(int data) { 321 | msPred = null; 322 | msSucc = null; 323 | pre = null; 324 | cur = null; 325 | multiSolver2(root, data); 326 | System.out.println(msPred); 327 | System.out.println(msSucc); 328 | } 329 | 330 | private void multiSolver2(Node node, int data) { 331 | 332 | pre = cur; 333 | cur = node.data; 334 | 335 | if (cur == data) { 336 | msPred = pre; 337 | } 338 | if (pre != null && pre == data) { 339 | msSucc = cur; 340 | } 341 | 342 | for (Node child : node.children) { 343 | multiSolver2(child, data); 344 | } 345 | 346 | } 347 | 348 | private Integer msfloor = null; 349 | private Integer msceil = null; 350 | 351 | // ceil , floor 352 | public void multiSolve3(int data) { 353 | msfloor = null; 354 | msceil = null; 355 | multiSolver3(root, data); 356 | System.out.println(msfloor); 357 | System.out.println(msceil); 358 | } 359 | 360 | private void multiSolver3(Node node, int data) { 361 | 362 | if (node.data > data) { 363 | msceil = msceil == null ? node.data : Math.min(msceil, node.data); 364 | } 365 | 366 | if (node.data < data) { 367 | msfloor = msfloor == null ? node.data : Math.max(msfloor, node.data); 368 | } 369 | 370 | for (Node child : node.children) { 371 | multiSolver3(child, data); 372 | } 373 | 374 | } 375 | 376 | // pred , succ , 377 | public void multiSolve4(int data) { 378 | msPred = null; 379 | msSucc = null; 380 | pre = null; 381 | cur = null; 382 | multiSolver4(root, data, 0); 383 | System.out.println(msPred); 384 | System.out.println(msSucc); 385 | } 386 | 387 | private void multiSolver4(Node node, int data, int flag) { 388 | for (Node child : node.children) { 389 | multiSolver4(child, data, flag); 390 | } 391 | } 392 | 393 | public void levelOrder() { 394 | LinkedList queue = new LinkedList<>(); 395 | queue.addLast(root); 396 | 397 | while (queue.size() > 0) { 398 | Node t = queue.removeFirst(); 399 | System.out.print(t.data + " "); 400 | for (Node child : t.children) { 401 | queue.addLast(child); 402 | } 403 | } 404 | System.out.println(); 405 | } 406 | 407 | public void levelOrderLineWise() { 408 | LinkedList queue = new LinkedList<>(); 409 | queue.addLast(root); 410 | 411 | Node node = new Node(); 412 | queue.addLast(node); 413 | 414 | while (queue.size() > 1) { 415 | Node t = queue.removeFirst(); 416 | if (t.data == 0) { 417 | queue.addLast(t); 418 | System.out.println(); 419 | } else { 420 | System.out.print(t.data + " "); 421 | for (Node child : t.children) { 422 | queue.addLast(child); 423 | } 424 | } 425 | 426 | } 427 | System.out.println(); 428 | 429 | } 430 | 431 | public void levelOrderLineWise2() { 432 | LinkedList CurQueue = new LinkedList<>(); 433 | LinkedList PreQueue = new LinkedList<>(); 434 | CurQueue.addLast(root); 435 | 436 | while (CurQueue.size() > 0) { 437 | Node t = CurQueue.removeFirst(); 438 | 439 | System.out.print(t.data + " "); 440 | for (Node child : t.children) { 441 | PreQueue.addLast(child); 442 | } 443 | 444 | if (CurQueue.size() == 0 && PreQueue.size() > 0) { 445 | CurQueue = PreQueue; 446 | PreQueue = new LinkedList<>(); 447 | System.out.println(); 448 | } 449 | } 450 | System.out.println(); 451 | 452 | } 453 | 454 | public void levelOrderZigZag() { 455 | LinkedList queue = new LinkedList<>(); 456 | Stack stack = new Stack<>(); 457 | queue.addLast(root); 458 | 459 | boolean flag = true; 460 | 461 | while (queue.size() > 0) { 462 | Node t = queue.removeFirst(); 463 | System.out.print(t.data + " "); 464 | 465 | if (flag == true) { 466 | for (Node child : t.children) { 467 | stack.push(child); 468 | } 469 | 470 | } else { 471 | for (int i = t.children.size() - 1; i >= 0l; i--) { 472 | stack.push(t.children.get(i)); 473 | } 474 | 475 | } 476 | 477 | if (queue.size() == 0 && stack.size() > 0) { 478 | while (stack.size() > 0) { 479 | queue.addLast(stack.pop()); 480 | } 481 | System.out.println(); 482 | flag = !flag; 483 | } 484 | } 485 | } 486 | 487 | public int diameter(Node node1, Node node2) { 488 | 489 | } 490 | 491 | } -------------------------------------------------------------------------------- /dataStructures/binaryTree/BinaryTree.java: -------------------------------------------------------------------------------- 1 | package dataStructures.binaryTree; 2 | 3 | import java.util.*; 4 | 5 | public class BinaryTree { 6 | 7 | private class Node { 8 | int data; 9 | Node left; 10 | Node right; 11 | } 12 | 13 | Node root; 14 | int size; 15 | 16 | public BinaryTree(int[] arr) { 17 | Stack stack = new Stack<>(); 18 | 19 | for (int i = 0; i < arr.length; i++) { 20 | if (arr[i] == -1) { 21 | stack.pop(); 22 | } else { 23 | Node node = new Node(); 24 | node.data = arr[i]; 25 | this.size++; 26 | 27 | if (stack.size() == 0) { 28 | root = node; 29 | } else if (stack.peek().left == null) { 30 | stack.peek().left = node; 31 | } else { 32 | stack.peek().right = node; 33 | } 34 | stack.push(node); 35 | } 36 | } 37 | } 38 | 39 | public void display() { 40 | 41 | display(root); 42 | } 43 | 44 | private void display(Node node) { 45 | if (node == null) { 46 | return; 47 | } 48 | 49 | String str = ""; 50 | str += node.left != null ? (node.left.data + " ") : (". "); 51 | str += node.data + " "; 52 | str += node.right != null ? (node.right.data + " ") : (" ."); 53 | 54 | System.out.println(str); 55 | 56 | display(node.left); 57 | display(node.right); 58 | 59 | } 60 | 61 | public int Size() { 62 | return Size(root); 63 | } 64 | 65 | private int Size(Node node) { 66 | if (node == null) { 67 | return 0; 68 | } 69 | int ans = Size(node.left) + Size(node.right); 70 | return ans + 1; 71 | } 72 | 73 | public int height() { 74 | return height(root); 75 | } 76 | 77 | private int height(Node node) { 78 | if (node == null) { 79 | return 0; 80 | } 81 | int hl = height(node.left); 82 | int hr = height(node.right); 83 | int ans = Math.max(hl, hr) + 1; 84 | return ans; 85 | 86 | } 87 | 88 | public int max() { 89 | return max(root); 90 | } 91 | 92 | private int max(Node node) { 93 | if (node == null) { 94 | return Integer.MIN_VALUE; 95 | } 96 | int ml = max(node.left); 97 | int mr = max(node.right); 98 | int ans = Math.max(node.data, Math.max(ml, mr)); 99 | return ans; 100 | 101 | } 102 | 103 | public boolean find(int data) { 104 | return find(root, data); 105 | } 106 | 107 | private boolean find(Node node, int data) { 108 | if (node == null) { 109 | return false; 110 | } 111 | 112 | // if (data == node.data) { 113 | // return true; 114 | // } 115 | // 116 | // boolean fl = find(node.left, data); 117 | // if (fl == true) { 118 | // return true; 119 | // } 120 | // 121 | // boolean fr = find(node.right, data); 122 | // if (fr == true) { 123 | // return true; 124 | // } 125 | // 126 | // return false; 127 | 128 | boolean found = false; 129 | found = found || data == node.data; 130 | found = found || find(node.left, data); 131 | found = found || find(node.right, data); 132 | return found; 133 | } 134 | 135 | public void removeLeaf() { 136 | // removeLeaf(root, root.left); 137 | // removeLeaf(root, root.right); 138 | removeLeaf(null, root); 139 | } 140 | 141 | private void removeLeaf(Node parent, Node child) { 142 | 143 | if (child == null) { 144 | return; 145 | } 146 | 147 | if (child.left == null && child.right == null) { 148 | if (parent.left != null && parent.left == child) { 149 | parent.left = null; 150 | } else { 151 | parent.right = null; 152 | } 153 | } 154 | 155 | removeLeaf(child, child.left); 156 | removeLeaf(child, child.right); 157 | 158 | } 159 | 160 | public void SingleChild() { 161 | SingleChild(root); 162 | } 163 | 164 | private void SingleChild(Node node) { 165 | if (node == null) { 166 | return; 167 | } 168 | if (node.left != null && node.right == null) { 169 | System.out.println(node.left.data); 170 | } 171 | 172 | if (node.left == null && node.right != null) { 173 | System.out.println(node.right.data); 174 | } 175 | 176 | SingleChild(node.left); 177 | SingleChild(node.right); 178 | 179 | } 180 | 181 | public void rootToNodePathWithTarget(int low, int high) { 182 | rootToNodePathWithTarget(root, 0, "", low, high); 183 | } 184 | 185 | private void rootToNodePathWithTarget(Node node, int sum, String psf, int low, int high) { 186 | if (node == null || (node.left == null && node.right == null)) { 187 | sum = sum + node.data; 188 | if (low < sum && sum < high) { 189 | System.out.println(psf + " " + node.data + " ==== " + sum); 190 | } 191 | return; 192 | } 193 | rootToNodePathWithTarget(node.left, sum + node.data, psf + node.data + " -> ", low, high); 194 | rootToNodePathWithTarget(node.right, sum + node.data, psf + node.data + " -> ", low, high); 195 | } 196 | 197 | public void NodetoKth(int data, int k) { 198 | ArrayList temp = rootToNodePath(root, data); 199 | for (int i = 0; i < temp.size(); i++) { 200 | System.out.print(temp.get(i).data + " "); 201 | } 202 | System.out.println(); 203 | 204 | kthDown(temp.get(0), k); 205 | int i = 1; 206 | k--; 207 | 208 | while (k > 0 && i < temp.size()) { 209 | 210 | if (temp.get(i).left == temp.get(i - 1)) { 211 | kthDown(temp.get(i).right, k - 1); 212 | } 213 | 214 | if (temp.get(i).right == temp.get(i - 1)) { 215 | kthDown(temp.get(i).left, k - 1); 216 | } 217 | 218 | k--; 219 | i++; 220 | } 221 | 222 | if (k == 0 && i < temp.size()) { 223 | System.out.println(temp.get(i).data); 224 | } 225 | 226 | } 227 | 228 | private ArrayList rootToNodePath(Node node, int data) { 229 | if (node == null) { 230 | ArrayList al = new ArrayList<>(); 231 | return al; 232 | } 233 | if (node.data == data) { 234 | ArrayList al = new ArrayList<>(); 235 | al.add(node); 236 | return al; 237 | } 238 | ArrayList alL = rootToNodePath(node.left, data); 239 | if (alL.size() > 0) { 240 | alL.add(node); 241 | return alL; 242 | } 243 | ArrayList alR = rootToNodePath(node.right, data); 244 | if (alR.size() > 0) { 245 | alR.add(node); 246 | return alR; 247 | } 248 | 249 | return new ArrayList<>(); 250 | } 251 | 252 | private void kthDown(Node node, int k) { 253 | if (node == null) { 254 | return; 255 | } 256 | 257 | if (k == 0) { 258 | System.out.println(node.data); 259 | return; 260 | } 261 | 262 | kthDown(node.left, k - 1); 263 | kthDown(node.right, k - 1); 264 | } 265 | 266 | public void preO() { 267 | preO(root); 268 | } 269 | 270 | private void preO(Node node) { 271 | if (node == null) { 272 | return; 273 | } 274 | System.out.print(node.data + " "); 275 | preO(node.left); 276 | preO(node.right); 277 | } 278 | 279 | public void inO() { 280 | inO(root); 281 | } 282 | 283 | private void inO(Node node) { 284 | if (node == null) { 285 | return; 286 | } 287 | inO(node.left); 288 | System.out.print(node.data + " "); 289 | inO(node.right); 290 | } 291 | 292 | public void postO() { 293 | postO(root); 294 | } 295 | 296 | private void postO(Node node) { 297 | if (node == null) { 298 | return; 299 | } 300 | postO(node.left); 301 | postO(node.right); 302 | System.out.print(node.data + " "); 303 | } 304 | 305 | public BinaryTree(int[] pre, int[] in) { 306 | // root = Contruct(pre, 0, pre.length - 1, in, 0, in.length - 1); 307 | root = Contruct1(pre, 0, pre.length - 1, in, 0, in.length - 1); 308 | } 309 | 310 | private Node Contruct(int[] pre, int pl, int ph, int[] in, int il, int ih) { 311 | 312 | if (il > ih) { 313 | return null; 314 | } 315 | 316 | Node node = new Node(); 317 | node.data = pre[pl]; 318 | 319 | int idx = -1; 320 | for (int i = il; i <= ih; i++) { 321 | if (in[i] == node.data) { 322 | idx = i; 323 | break; 324 | } 325 | } 326 | int plh = idx - il; 327 | 328 | node.left = Contruct(pre, pl + 1, pl + plh, in, il, idx - 1); 329 | node.right = Contruct(pre, pl + plh + 1, ph, in, idx + 1, ih); 330 | return node; 331 | } 332 | 333 | private Node Contruct1(int[] post, int pl, int ph, int[] in, int il, int ih) { 334 | 335 | if (il > ih) { 336 | return null; 337 | } 338 | 339 | Node node = new Node(); 340 | node.data = post[ph]; 341 | 342 | int idx = -1; 343 | for (int i = il; i <= ih; i++) { 344 | if (in[i] == node.data) { 345 | idx = i; 346 | break; 347 | } 348 | } 349 | 350 | int ilh = idx - il; 351 | 352 | node.left = Contruct1(post, pl, pl + ilh - 1, in, il, idx - 1); 353 | node.right = Contruct1(post, pl + ilh, ph - 1, in, idx + 1, ih); 354 | 355 | return node; 356 | } 357 | 358 | private class Pair { 359 | Node node; 360 | int cs; 361 | 362 | public Pair(Node node, int cs) { 363 | this.node = node; 364 | this.cs = cs; 365 | } 366 | } 367 | 368 | public void preoi() { 369 | Stack stack = new Stack<>(); 370 | Pair rP = new Pair(root, 0); 371 | stack.push(rP); 372 | 373 | while (stack.size() > 0) { 374 | Pair top = stack.peek(); 375 | 376 | // if (top.node == null) { 377 | // stack.pop(); 378 | // continue; 379 | // } 380 | 381 | if (top.cs == 0) { 382 | System.out.print(top.node.data + " "); 383 | } else if (top.cs == 1) { 384 | if (top.node.left != null) { 385 | stack.push(new Pair(top.node.left, 0)); 386 | } 387 | } else if (top.cs == 2) { 388 | if (top.node.right != null) { 389 | stack.push(new Pair(top.node.right, 0)); 390 | } 391 | } else { 392 | stack.pop(); 393 | } 394 | top.cs++; 395 | } 396 | System.out.println(); 397 | } 398 | 399 | public void inoi() { 400 | Stack stack = new Stack<>(); 401 | Pair rP = new Pair(root, 0); 402 | stack.push(rP); 403 | 404 | while (stack.size() > 0) { 405 | Pair top = stack.peek(); 406 | 407 | if (top.cs == 0) { 408 | if (top.node.left != null) { 409 | stack.push(new Pair(top.node.left, 0)); 410 | } 411 | } else if (top.cs == 1) { 412 | System.out.print(top.node.data + " "); 413 | } else if (top.cs == 2) { 414 | if (top.node.right != null) { 415 | stack.push(new Pair(top.node.right, 0)); 416 | } 417 | } else { 418 | stack.pop(); 419 | } 420 | top.cs++; 421 | } 422 | System.out.println(); 423 | } 424 | 425 | public void postoi() { 426 | Stack stack = new Stack<>(); 427 | Pair rP = new Pair(root, 0); 428 | stack.push(rP); 429 | 430 | while (stack.size() > 0) { 431 | Pair top = stack.peek(); 432 | 433 | if (top.cs == 0) { 434 | if (top.node.left != null) { 435 | stack.push(new Pair(top.node.left, 0)); 436 | } 437 | } else if (top.cs == 1) { 438 | if (top.node.right != null) { 439 | stack.push(new Pair(top.node.right, 0)); 440 | } 441 | } else if (top.cs == 2) { 442 | System.out.print(top.node.data + " "); 443 | } else { 444 | stack.pop(); 445 | } 446 | top.cs++; 447 | } 448 | System.out.println(); 449 | } 450 | 451 | public boolean baleneceHeight() { 452 | return baleneceHeight(root); 453 | } 454 | 455 | private boolean baleneceHeight(Node node) { 456 | if (node == null) { 457 | return true; 458 | } 459 | boolean lans = baleneceHeight(node.left); 460 | if (lans == false) { 461 | return lans; 462 | } 463 | boolean rans = baleneceHeight(node.right); 464 | if (rans == false) { 465 | return rans; 466 | } 467 | int lh = height(node.left); 468 | int rh = height(node.right); 469 | int diff = Math.abs(rh - lh); 470 | if (diff >= 2) { 471 | return false; 472 | } else { 473 | return true; 474 | } 475 | } 476 | 477 | private class BalPair { 478 | int height; 479 | boolean isBal; 480 | 481 | public BalPair(int height, boolean isBal) { 482 | this.height = height; 483 | this.isBal = isBal; 484 | } 485 | } 486 | 487 | public boolean baleneceHeightON() { 488 | return baleneceHeightON(root).isBal; 489 | } 490 | 491 | private BalPair baleneceHeightON(Node node) { 492 | if (node == null) { 493 | BalPair bs = new BalPair(0, true); 494 | return bs; 495 | } 496 | 497 | BalPair ls = baleneceHeightON(node.left); 498 | if (ls.isBal == false) { 499 | return ls; 500 | } 501 | 502 | BalPair rs = baleneceHeightON(node.right); 503 | if (rs.isBal == false) { 504 | return rs; 505 | } 506 | 507 | int diff = Math.abs(ls.height - rs.height); 508 | Boolean tempB = diff <= 1 ? true : false; 509 | int tempH = Math.max(ls.height, rs.height) + 1; 510 | BalPair bp = new BalPair(tempH, tempB); 511 | return bp; 512 | } 513 | 514 | } 515 | --------------------------------------------------------------------------------