├── Search.java ├── recusrion.md ├── Recursion Problems ├── StairCase.java ├── BST ├── SearchInBST.java └── checkBST.java ├── StackAndQueue ├── theory ├── QueueUsingLL.java ├── stackLL.java └── BalancedParanthesis.java ├── README.md ├── LinkedList ├── Linkedlist basic ├── Insert.java ├── Node.java ├── Programs │ ├── LengthLL.java │ ├── findNode.java │ ├── PrintReverseLL.java │ ├── removeduplicates.java │ ├── Palindrome.java │ ├── MiddleLinkedList.java │ ├── Append │ │ └── Append.java │ ├── PrintNode.java │ ├── indexOfNRec.java │ ├── delete.java │ ├── mergeTwoList.java │ ├── DeleteNodesTricky.java │ └── EvenafterOdd.java └── Insertion_Print.java ├── Time Complexity.java ├── Print Numbers ├── Multiplication Recursively ├── SumOfDigits ├── Geometric Sum ├── Fibonacci sum ├── Count Zeros ├── power_of_a_no.java ├── decimal_binary ├── GenericsEx. ├── GenericSimple.java ├── GenericMethod.java └── Concatadd.java ├── even_odd ├── Calculating power ├── Time Complexity ├── ArrayRotation.java ├── Rotate.java ├── UniqueElement.java ├── PairSum.java └── DuplicateElement.java ├── HashMaps ├── UniqueCharacter.java ├── Basics.java ├── IntersectionArray.java ├── maximumFrequencyNumber.java ├── removeDuplicates.java └── PairsWithDifferenceK.java ├── BinaryTree ├── sumOfAllNodes.java ├── PostOrder.java ├── PreOrder.java ├── NodesWithoutSibling.java ├── mirrorView.java ├── Height.java ├── LevelOrder.java ├── findNode.java ├── RemoveLeaf.java └── BalancedTree.java ├── TowerOfHanoi.java ├── CheckSorted.java ├── ConvertStringToInt.java ├── sum of natural no's ├── ReplacePi.java ├── Subsequence.java ├── StairCase1.java ├── RemoveDuplicates.java ├── StringPermutation.java ├── firstIndex.java ├── ReplaceChar.java ├── PairStar.java ├── subsetArray.java ├── RemoveX.java ├── ArrayListToArray.java ├── #doubt Iterators.java ├── SumofArray ├── QuickSort implementation.java ├── BinarySearch.java ├── Searching in Array ├── lastIndex.java ├── allIndex.java ├── MergeSort.java └── Keypad.java /Search.java: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /recusrion.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Recursion Problems: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /StairCase.java: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /BST/SearchInBST.java: -------------------------------------------------------------------------------- 1 | #pending 2 | -------------------------------------------------------------------------------- /BST/checkBST.java: -------------------------------------------------------------------------------- 1 | #pending 2 | -------------------------------------------------------------------------------- /StackAndQueue/theory: -------------------------------------------------------------------------------- 1 | 2 | //Stack revision 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Coding-Ninjas 2 | Problems at Coding Ninjas 3 | -------------------------------------------------------------------------------- /LinkedList/Linkedlist basic: -------------------------------------------------------------------------------- 1 | /* testing for creating folder*/ 2 | -------------------------------------------------------------------------------- /LinkedList/Insert.java: -------------------------------------------------------------------------------- 1 | package com; 2 | 3 | public class Insert { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /LinkedList/Node.java: -------------------------------------------------------------------------------- 1 | package com; 2 | 3 | public class Node { 4 | 5 | T data; 6 | Node next; 7 | Node(T data){ 8 | this.data=data; 9 | next=null; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Time Complexity.java: -------------------------------------------------------------------------------- 1 | 2 | Read theory but, 3 | 14/2/18->recurrence relation 4 | 15/2/18-> Time complexity finding 5 | 6 | #pending 7 | Calculation of time complexity of 8 | bubble sort 9 | selection sort 10 | insertion sort 11 | -------------------------------------------------------------------------------- /Print Numbers: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static void print(int n){ 4 | 5 | if(n == 1){ 6 | System.out.print(n + " "); 7 | return; 8 | } 9 | print(n - 1); 10 | System.out.print(n+" "); 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Multiplication Recursively: -------------------------------------------------------------------------------- 1 | 2 | public class solution { 3 | 4 | public static int multiplyTwoIntegers(int m, int n){ 5 | 6 | if(n==1){ 7 | return m; 8 | } 9 | int total=m+multiplyTwoIntegers(m,--n); 10 | return total; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /SumOfDigits: -------------------------------------------------------------------------------- 1 | 2 | public class solution { 3 | 4 | public static int sumOfDigits(int input){ 5 | 6 | if(input==0){ 7 | return 0; 8 | } 9 | int rem=input%10; 10 | int total=rem+sumOfDigits(input/10); 11 | return total; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Geometric Sum: -------------------------------------------------------------------------------- 1 | 2 | public class solution { 3 | 4 | public static double findGeometricSum(int k){ 5 | 6 | if(k==0){ 7 | return 1; 8 | } 9 | double total=1/Math.pow(2,k); 10 | total+=findGeometricSum(--k); 11 | return total; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Fibonacci sum: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.lang.*; 3 | 4 | class GFG { 5 | static int fibo(int n){ 6 | if(n==0||n==1){ 7 | return n; 8 | } 9 | int sum=fibo(n-1)+fibo(n-2); 10 | return sum+1; 11 | } 12 | public static void main (String[] args) { 13 | System.out.println(fibo((4))); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Count Zeros: -------------------------------------------------------------------------------- 1 | 2 | public class solution { 3 | 4 | public static int countZerosRec(int input){ 5 | 6 | if(input==0){ 7 | return 0; 8 | } 9 | int rem=input%10; 10 | int total=0; 11 | if(rem==0){ 12 | total++; 13 | } 14 | total+=countZerosRec(input/10); 15 | return total; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /power_of_a_no.java: -------------------------------------------------------------------------------- 1 | /*package whatever //do not write package name here */ 2 | 3 | import java.io.*; 4 | 5 | class GFG { 6 | 7 | public static int to_power(int number,int power){ 8 | if(power==1){ 9 | return number; 10 | } 11 | int total=number*to_power(number,power-1); 12 | return total; 13 | } 14 | 15 | public static void main (String[] args) { 16 | System.out.println(to_power(3,5)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /decimal_binary: -------------------------------------------------------------------------------- 1 | /*package whatever //do not write package name here */ 2 | 3 | import java.io.*; 4 | 5 | class GFG { 6 | 7 | public static void decimal_binary(int n){ 8 | if(n==0){ 9 | return; 10 | } 11 | int rem=n%2; 12 | decimal_binary(n/2); 13 | System.out.print(rem); 14 | } 15 | 16 | public static void main (String[] args) { 17 | System.out.println(); 18 | decimal_binary(8); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /GenericsEx./GenericSimple.java: -------------------------------------------------------------------------------- 1 | /*package whatever //do not write package name here */ 2 | 3 | import java.util.*; 4 | 5 | class Pair{ 6 | 7 | T first; 8 | V second; 9 | Pair(T first, V second){ 10 | this.first=first; 11 | this.second=second; 12 | } 13 | } 14 | 15 | class GFG { 16 | public static void main (String[] args) { 17 | Pair p=new Pair<>(1,"abc"); 18 | System.out.println(p.first+" "+p.second); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /even_odd: -------------------------------------------------------------------------------- 1 | /*package whatever //do not write package name here */ 2 | 3 | import java.io.*; 4 | 5 | class GFG { 6 | 7 | public static void even_odd(int m,int n){ 8 | if(m<=n){ 9 | even_odd(m,n-1); 10 | if(n%2==0){ 11 | System.out.println("even "+n); 12 | }else{ 13 | System.out.println("odd "+n); 14 | } 15 | } 16 | } 17 | public static void main (String[] args) { 18 | even_odd(10,15); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /GenericsEx./GenericMethod.java: -------------------------------------------------------------------------------- 1 | 2 | /*package whatever //do not write package name here */ 3 | 4 | import java.io.*; 5 | 6 | 7 | class PrintArray{ 8 | 9 | //the method which can return any data type 10 | //whatever we pass 11 | public void print(T value){ 12 | 13 | System.out.println(value); 14 | } 15 | } 16 | 17 | class GFG { 18 | public static void main (String[] args) { 19 | PrintArray p=new PrintArray(); 20 | p.print(2); 21 | p.print("raj"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Calculating power: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int power(int x, int n) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | if(n==0){ 12 | return 1; 13 | } 14 | if(n==1){ 15 | return x; 16 | } 17 | int mult=power(x,n-1); 18 | return x*mult; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Time Complexity/ArrayRotation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CheckRotation { 4 | 5 | public static int arrayRotateCheck(int[] arr){ 6 | 7 | //for clockwise roatated array only 8 | //the no of times array has been rotated is the array index 9 | //of the minimumm element 10 | 11 | int min=arr[0],index=0; 12 | for(int i=1;iarr[i]){ 14 | min=arr[i]; 15 | index=i; 16 | } 17 | } 18 | return index; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HashMaps/UniqueCharacter.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class solution { 4 | 5 | public static String uniqueChar(String str){ 6 | 7 | 8 | Map mp=new HashMap<>(); 9 | String uniqueString=""; 10 | for(int i=0;i { 7 | T data; 8 | BinaryTreeNode left; 9 | BinaryTreeNode right; 10 | 11 | public BinaryTreeNode(T data) { 12 | this.data = data; 13 | } 14 | } 15 | */ 16 | 17 | public static int sum(BinaryTreeNode root){ 18 | 19 | // Write your code here 20 | if(root==null){ 21 | return 0; 22 | } 23 | int sum=root.data+sum(root.left)+sum(root.right); 24 | return sum; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /LinkedList/Programs/LengthLL.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int length(LinkedListNode head){ 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | int length=0; 12 | LinkedListNode temp=head; 13 | while(temp!=null){ 14 | length++; 15 | temp=temp.next; 16 | } 17 | return length; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /TowerOfHanoi.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class runner { 4 | public static void main(String[] args) { 5 | Scanner s = new Scanner(System.in); 6 | int n = s.nextInt(); 7 | solution.towerOfHanoi(n, 'a', 'b', 'c'); 8 | } 9 | } 10 | 11 | 12 | 13 | public class solution { 14 | 15 | public static void towerOfHanoi(int n, char source, char intermediate, char destination){ 16 | 17 | if(n>=1){ 18 | towerOfHanoi(n-1,source,destination,intermediate); 19 | System.out.println(source+" "+destination); 20 | towerOfHanoi(n-1,intermediate,source,destination); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LinkedList/Programs/findNode.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | public static int indexOfNIter(LinkedListNode head, int n) { 4 | /* Your class should be named Solution 5 | * Don't write main(). 6 | * Don't read input, it is passed as function argument. 7 | * Return output and don't print it. 8 | * Taking input and printing output is handled automatically. 9 | */ 10 | LinkedListNode temp=head; 11 | int len=0; 12 | while(temp!=null){ 13 | if(temp.data==n){ 14 | return len; 15 | } 16 | len++; 17 | temp=temp.next; 18 | } 19 | return -1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Time Complexity/Rotate.java: -------------------------------------------------------------------------------- 1 | public class ArrayRotate { 2 | 3 | public static void rotate(int[] arr, int d) { 4 | /* Your class should be named ArrayRotate 5 | * Don't write main(). 6 | * Don't read input, it is passed as function argument. 7 | * No need to print or return the output. 8 | * Taking input and printing the output is handled automatically. 9 | */ 10 | 11 | int temp=0; 12 | int j=0; 13 | for(int i=0;i { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | //caution- only printing :) 23 | public class Solution { 24 | public static void printReverseRecursive(LinkedListNode root) { 25 | 26 | if(root==null){ 27 | return; 28 | } 29 | printReverseRecursive(root.next); 30 | System.out.print(root.data+" "); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ConvertStringToInt.java: -------------------------------------------------------------------------------- 1 | 2 | import java.lang.Math.*; 3 | 4 | public class solution { 5 | 6 | public static int convertStringToInt(String input){ 7 | // Write your code here 8 | if(input.length()==1){ 9 | return Integer.parseInt(input); 10 | } 11 | char []arr=input.toCharArray(); 12 | char arr1[]=new char[arr.length-1]; 13 | for(int i=0;i mp=new HashMap<>(); 9 | mp.put("abc",1); 10 | mp.put("def",2); 11 | mp.put("ghi",3); 12 | mp.put("jkl",4); 13 | 14 | System.out.println(mp.get("jkl"));//to get the value 15 | System.out.println(mp.isEmpty());//checking the hashmap is empty or TypeNotPresentException 16 | System.out.println("The actual hashmap size "+ mp.size());//return the size 17 | System.out.println(mp.remove("jkl"));//remove returns the deleted key 18 | System.out.println("The actual hashmap size after deleting one key "+ mp.size());//return the size 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Time Complexity/UniqueElement.java: -------------------------------------------------------------------------------- 1 | 2 | /*package whatever //do not write package name here */ 3 | 4 | import java.util.*; 5 | import java.io.*; 6 | 7 | class GFG { 8 | public static void main (String[] args) { 9 | int arr[]={2,3,1,6,3,6,2}; 10 | System.out.println(naive(arr)); 11 | } 12 | 13 | //arr={2,2,3,3,6,6,7}; 14 | //arr={1,2,2,3,3,6,6}; 15 | public static int naive(int arr[]){ 16 | Arrays.sort(arr); 17 | int i=0; 18 | System.out.println("the length of array is "+arr.length); 19 | while(i for 1-5--> 1 2 3 4 5 13 | System.out.println(n);*/ 14 | 15 | if(m<=n){ 16 | printNatural(m,n-1); 17 | System.out.println(n); 18 | } 19 | } 20 | public static void main (String[] args) { 21 | printNatural(1,5); 22 | System.out.println("GfG!"); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ReplacePi.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class runner { 5 | public static void main(String[] args) { 6 | Scanner s = new Scanner(System.in); 7 | String input = s.next(); 8 | System.out.println(solution.replace(input)); 9 | } 10 | } 11 | class solution { 12 | 13 | public static String replace(String input){ 14 | 15 | if(input.length()==1){ 16 | //System.out.println(input); 17 | return input; 18 | } 19 | String s1=replace(input.substring(1)); 20 | String actual=""; 21 | if(input.charAt(0)=='p' && s1.charAt(0)=='i'){ 22 | actual="3.14"+s1.substring(1); 23 | } 24 | else{ 25 | actual=input.charAt(0)+s1; 26 | } 27 | return actual; 28 | } 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /BinaryTree/PostOrder.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* Binary Tree Node class 4 | * 5 | * class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | } 13 | } 14 | */ 15 | public static void preOrder(BinaryTreeNode root) { 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Print output and don't return it. 20 | * Taking input is handled automatically. 21 | */ 22 | 23 | if(root==null){ 24 | return; 25 | } 26 | System.out.print(root.data+" "); 27 | preOrder(root.left); 28 | preOrder(root.right); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /BinaryTree/PreOrder.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* Binary Tree Node class 4 | * 5 | * class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | } 13 | } 14 | */ 15 | public static void preOrder(BinaryTreeNode root) { 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Print output and don't return it. 20 | * Taking input is handled automatically. 21 | */ 22 | 23 | if(root==null){ 24 | return; 25 | } 26 | System.out.print(root.data+" "); 27 | preOrder(root.left); 28 | preOrder(root.right); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Subsequence.java: -------------------------------------------------------------------------------- 1 | 2 | #pending 3 | import java.io.*; 4 | import java.lang.*; 5 | 6 | class GFG { 7 | 8 | public static String[] subSequence(String str){ 9 | if(str.length()==0){ 10 | String ans[]={""}; 11 | return ans; 12 | } 13 | String s1[]=subSequence(str.substring(1)); 14 | String s[]=new String[2*s1.length]; 15 | for(int i=0;i { 5 | public T data; 6 | public LinkedListNode next; 7 | 8 | public LinkedListNode(T data) { 9 | this.setData(data); 10 | this.next = null; 11 | } 12 | 13 | public T getData() { 14 | return data; 15 | } 16 | 17 | public void setData(T data) { 18 | this.data = data; 19 | } 20 | 21 | } 22 | * */ 23 | 24 | public class Solution { 25 | public static LinkedListNode removeDuplicates(LinkedListNode head) { 26 | 27 | LinkedListNode temp=head; 28 | while(temp.next!=null){ 29 | if(temp.data.equals((temp.next).data)){ // 30 | //temp.next 31 | temp.next=temp.next.next; 32 | }else{ 33 | temp=temp.next; 34 | } 35 | } 36 | return head; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /HashMaps/IntersectionArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Intersection{ 4 | 5 | public static void intersection(int[] arr1, int[] arr2){ 6 | /* Your class should be named Intersection 7 | * Don't write main(). 8 | * Don't read input, it is passed as function argument. 9 | * Print output and don't return it. 10 | * Taking input is handled automatically. 11 | */ 12 | if(arr1.length==0 || arr2.length==0){ 13 | return 0; 14 | } 15 | Map mp=new HashMap<>(); 16 | for(int i=0;i { 9 | T data; 10 | BinaryTreeNode left; 11 | BinaryTreeNode right; 12 | 13 | public BinaryTreeNode(T data) { 14 | this.data = data; 15 | } 16 | } 17 | */ 18 | 19 | public static void printNodesWithoutSibling(BinaryTreeNode root) { 20 | 21 | // Write your code here 22 | if(root==null){ 23 | return; 24 | } 25 | if(root.left!=null && root.right==null){ 26 | System.out.println(root.left.data); 27 | } 28 | if(root.left==null && root.right!=null){ 29 | System.out.println(root.right.data); 30 | } 31 | printNodesWithoutSibling(root.left); 32 | printNodesWithoutSibling(root.right); 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /ReplaceChar.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Runner { 5 | 6 | public static void main(String[] args) { 7 | Scanner s = new Scanner(System.in); 8 | String input = s.next(); 9 | char c1 = s.next().charAt(0); 10 | char c2 = s.next().charAt(0); 11 | System.out.println(Solution.replaceCharacter(input, c1, c2)); 12 | } 13 | } 14 | public class Solution { 15 | 16 | public static String removeConsecutiveDuplicates(String s) { 17 | // Write your code here 18 | 19 | String str=""; 20 | if(s.length()==1){ 21 | str=s; 22 | return str; 23 | } 24 | String actual=removeConsecutiveDuplicates(s.substring(0,s.length()-1)); 25 | if(s.charAt(s.length()-1)==actual.charAt(actual.length()-1)){ 26 | return actual; 27 | } 28 | actual+=Character.toString(s.charAt(s.length()-1)); 29 | return actual; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /HashMaps/maximumFrequencyNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Solution { 4 | 5 | public static int maxFrequencyNumber(int[] arr){ 6 | 7 | if(arr.length==1){ 8 | return arr[0]; 9 | } 10 | 11 | Map mp=new HashMap<>(); 12 | //ArrayList list=new ArrayList<>(); 13 | int max=-1; 14 | for(int i=0;i entry : mp.entrySet()){ 24 | if(entry.getValue()>max){ 25 | max=entry.getValue(); 26 | key=entry.getKey(); 27 | } 28 | } 29 | return key; 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PairStar.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import java.util.Scanner; 4 | 5 | public class runner { 6 | public static void main(String[] args) { 7 | Scanner s = new Scanner(System.in); 8 | String input = s.nextLine(); 9 | System.out.println(solution.addStars(input)); 10 | } 11 | } 12 | 13 | public class solution { 14 | 15 | // Return the updated string 16 | public static String addStars(String s) { 17 | 18 | if(s.length()==1){ 19 | return s; 20 | } 21 | char arr1[]=s.toCharArray(); 22 | char arr2[]=new char[arr1.length-1]; 23 | for(int index=0;index { 10 | T data; 11 | BinaryTreeNode left; 12 | BinaryTreeNode right; 13 | 14 | public BinaryTreeNode(T data) { 15 | this.data = data; 16 | } 17 | } 18 | */ 19 | 20 | public static void mirror(BinaryTreeNode root){ 21 | /* Your class should be named Solution 22 | * Don't write main(). 23 | * Don't read input, it is passed as function argument. 24 | * No need to print or return the output. 25 | * Taking input and printing output is handled automatically. 26 | */ 27 | 28 | BinaryTreeNode temp=null; 29 | if(root==null){ 30 | return ; 31 | } 32 | mirror(root.left); 33 | mirror(root.right); 34 | temp=root.left; 35 | root.left=root.right; 36 | root.right=temp; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /GenericsEx./Concatadd.java: -------------------------------------------------------------------------------- 1 | /*package whatever //do not write package name here */ 2 | 3 | //program which adds an integer and concatenate strings using same methods 4 | //code doesn't work as we cannot apply + operator directly on generic values 5 | 6 | 7 | import java.util.*; 8 | 9 | class Pair{ 10 | 11 | T first; 12 | T second; 13 | Pair(T first, T second){ 14 | this.first=first; 15 | this.second=second; 16 | System.out.println(this.first); 17 | System.out.println(this.second); 18 | } 19 | public void addWithConcat(){ 20 | System.out.println(first+second); 21 | } 22 | } 23 | 24 | class GFG { 25 | public static void main (String[] args) { 26 | Pair p=new Pair(1,3); 27 | // Pair p1=new Pair("abc","def"); 28 | // System.out.println(); 29 | p.addWithConcat(); 30 | // p1.addWithConcat(); 31 | // System.out.println(p1.addWithConcat()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LinkedList/Programs/Palindrome.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | 24 | static LinkedListNode left=null; 25 | public static boolean isPalindrome_2(LinkedListNode head) { 26 | 27 | left=head; 28 | return find(head); 29 | 30 | } 31 | public static boolean find(LinkedListNode right){ 32 | if(right==null){ 33 | return true; 34 | } 35 | boolean val=find(right.next); 36 | if(val==true){ 37 | if(left.data==right.data){ 38 | left=left.next; 39 | return true; 40 | } 41 | } 42 | return false; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /subsetArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class solution { 4 | public static void printSubsets(int input[]) { 5 | List> subsets = subsets(input); 6 | Object[] objArray = subsets.toArray(); 7 | for(int index=0;index> subsets(int[] nums) { 12 | List> list = new ArrayList<>(); 13 | subsetsHelper(list, new ArrayList<>(), nums, 0); 14 | return list; 15 | } 16 | 17 | public static void subsetsHelper(List> list , List resultList, int [] nums, int start){ 18 | list.add(new ArrayList<>(resultList)); 19 | for(int i = start; i < nums.length; i++){ 20 | // add element 21 | resultList.add(nums[i]); 22 | // Explore 23 | subsetsHelper(list, resultList, nums, i + 1); 24 | // remove 25 | resultList.remove(resultList.size() - 1); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /RemoveX.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class runner { 4 | public static void main(String[] args) { 5 | Scanner s = new Scanner(System.in); 6 | String input = s.nextLine(); 7 | System.out.println(solution.removeX(input)); 8 | } 9 | } 10 | 11 | public class solution { 12 | 13 | // Return the changed string 14 | public static String removeX(String input){ 15 | 16 | if(input.length()==1){ 17 | if(input.equals("x")){ 18 | return ""; 19 | }else{ 20 | return input; 21 | } 22 | } 23 | char arr[]=input.toCharArray(); 24 | char arr1[]=new char[arr.length-1]; 25 | for(int index=0;index al = new ArrayList(); 11 | al.add(10); 12 | al.add(20); 13 | al.add(30); 14 | al.add(40); 15 | 16 | //toArray() is used but it returns the object type array which is not an Integer here 17 | Object obj[]=al.toArray(); 18 | for(Object o:obj){ 19 | System.out.println(o); 20 | } 21 | //using get() also we can convert an arraylist values to an array 22 | 23 | 24 | // Integer[] arr = new Integer[al.size()+2]; 25 | // arr = al.toArray(arr); //the use of passing the array is to tell 26 | // //about the type of the array //it is going to return and accepts the array 27 | // for (Integer x : arr) 28 | // System.out.print(x + " "); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /BinaryTree/Height.java: -------------------------------------------------------------------------------- 1 | #find the height of the tree 2 | /* 3 | no of nodes from leaf to root(longest route) 4 | */ 5 | public class Solution { 6 | 7 | /* Binary Tree Node class 8 | * 9 | * class BinaryTreeNode { 10 | T data; 11 | BinaryTreeNode left; 12 | BinaryTreeNode right; 13 | 14 | public BinaryTreeNode(T data) { 15 | this.data = data; 16 | } 17 | } 18 | */ 19 | public static int height(BinaryTreeNode root) { 20 | /* Your class should be named Solution 21 | * Don't write main(). 22 | * Don't read input, it is passed as function argument. 23 | * Return output and don't print it. 24 | * Taking input and printing output is handled automatically. 25 | */ 26 | 27 | if(root==null){ 28 | return 0; 29 | } 30 | int ldepth=height(root.left); 31 | int rdepth=height(root.right); 32 | if(ldepth>rdepth){ 33 | return ldepth+1; 34 | }else{ 35 | return rdepth+1; 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /BinaryTree/LevelOrder.java: -------------------------------------------------------------------------------- 1 | #pending 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class Solution { 7 | 8 | /* Binary Tree Node class 9 | * 10 | * class BinaryTreeNode { 11 | T data; 12 | BinaryTreeNode left; 13 | BinaryTreeNode right; 14 | 15 | public BinaryTreeNode(T data) { 16 | this.data = data; 17 | } 18 | } 19 | */ 20 | 21 | public static void printLevelWise(BinaryTreeNode root){ 22 | 23 | // Write your code here 24 | 25 | QueueUsingLL> queue = new QueueUsingLL>(); 26 | queue.enqueue(root); 27 | while(!queue.isEmpty()){ 28 | BinaryTreeNode temp=queue.dequeue(); 29 | System.out.println(temp.data); 30 | if(temp.left!=null){ 31 | queue.add(temp.left); 32 | } 33 | if(temp.right!=null){ 34 | queue.add(queue.right); 35 | } 36 | } 37 | } 38 | 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /HashMaps/removeDuplicates.java: -------------------------------------------------------------------------------- 1 | package com; 2 | import java.util.*; 3 | import java.util.Map.Entry; 4 | 5 | public class removeDupli { 6 | 7 | public static void main(String[] args) { 8 | 9 | Map mp=new HashMap<>(); 10 | /*mp.put("1", true); 11 | mp.put("2", false); 12 | //iterating using keyset 13 | for(String keys:mp.keySet()){ 14 | System.out.println(keys); 15 | } 16 | for(Boolean values:mp.values()){ 17 | System.out.println(values); 18 | } 19 | //iterating using entry set 20 | 21 | 22 | for (Entry entry : mp.entrySet()) 23 | System.out.println("Key = " + entry.getKey() + 24 | ", Value = " + entry.getValue()); 25 | } 26 | */ 27 | 28 | int a[]={1,8,9,8,5,5}; 29 | ArrayList arr=new ArrayList<>(); 30 | for(int i=0;i al=new ArrayList(); 13 | al.add(s1); 14 | al.add(s2); 15 | al.add(s3); 16 | 17 | Iterator itr=al.iterator(); 18 | 19 | //traverse elements of ArrayList object 20 | while(itr.hasNext()){ 21 | Student st=itr.next(); //typecasting required 22 | System.out.println(st.rollno+" "+st.name+" "+st.age); 23 | } 24 | } 25 | } 26 | 27 | class Student{ 28 | int rollno; 29 | String name; 30 | int age; 31 | Student(int rollno,String name,int age){ 32 | this.rollno=rollno; 33 | this.name=name; 34 | this.age=age; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LinkedList/Programs/MiddleLinkedList.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | public static int printMiddel(LinkedListNode head) { 24 | 25 | LinkedListNode temp=head; 26 | int len=1; 27 | while(temp!=null){ 28 | len++; 29 | temp=temp.next; 30 | } 31 | //if it is even then fine 32 | //if odd then .5 will lead to ceil value which is actually mid value 33 | int mid=(int)Math.ceil(len/2); 34 | int start=1; 35 | temp=head; 36 | //System.out.println(temp.data); 37 | while(start Hashmap=new HashMap<>(); 8 | /*for(int x:input){ 9 | if(mp.containsKey(x+k)){ 10 | System.out.println(x+" "+(x+k)); 11 | } 12 | mp.put(x,true); 13 | }*/ 14 | int i=0,j=0; 15 | for(i=0;i { 5 | public T data; 6 | public LinkedListNode next; 7 | 8 | public LinkedListNode(T data) { 9 | this.setData(data); 10 | this.next = null; 11 | } 12 | 13 | public T getData() { 14 | return data; 15 | } 16 | 17 | public void setData(T data) { 18 | this.data = data; 19 | } 20 | 21 | } 22 | * */ 23 | 24 | 25 | public class Solution { 26 | public static LinkedListNode append(LinkedListNode root, int n) { 27 | LinkedListNode temp=null; 28 | LinkedListNode end=null; 29 | int length=1; 30 | end=root; 31 | //pointing this end node to last node 32 | while(end.next!=null){ 33 | length++; 34 | end=end.next; 35 | } 36 | int i=1; 37 | temp=root; 38 | //pointing this temp node to the (length-2)node i.e the node after this we have to append 39 | //if n==2 then we will point to 8th node 40 | while(ielement){ 40 | return indexFind(input,element,start,mid-1); 41 | } 42 | } 43 | return -1; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /BinaryTree/findNode.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* Binary Tree Node class 4 | * 5 | * class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | } 13 | } 14 | */ 15 | 16 | public static boolean isNodePresent(BinaryTreeNode root,int x){ 17 | /* Your class should be named Solution 18 | * Don't write main(). 19 | * Don't read input, it is passed as function argument. 20 | * Return output and don't print it. 21 | * Taking input and printing output is handled automatically. 22 | */ 23 | 24 | boolean val=false; 25 | 26 | if(root.data==x){ 27 | return true; 28 | } 29 | //checking left side of tree is null or not 30 | if(root.left!=null){ 31 | val=isNodePresent(root.left,x); 32 | } 33 | //we need to only check if and only if we didn't found 34 | //in left side of tree otherwise value will be overridden 35 | if(val==false){ 36 | if(root.right!=null){ 37 | val=isNodePresent(root.right,x); 38 | } 39 | } 40 | return val; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /LinkedList/Programs/PrintNode.java: -------------------------------------------------------------------------------- 1 | 2 | /*************** 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static void printIth(LinkedListNode head, int i){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Print output and don't return it. 20 | * Taking input is handled automatically. 21 | */ 22 | if(head==null){ 23 | return; 24 | } 25 | int actualLength=0; 26 | LinkedListNode temp=head; 27 | //finding the actual length 28 | while(temp!=null){ 29 | actualLength++; 30 | temp=temp.next; 31 | } 32 | //checking if ith index is greater than length of LL or not 33 | if(i>actualLength){ 34 | System.out.println(); 35 | return; 36 | } 37 | //no so putting head to temp again to ith index 38 | temp=head; 39 | int len=1;//as the first node is the head node itself 40 | while(len<=i ){ 41 | temp=temp.next; 42 | len++; 43 | } 44 | System.out.println(temp.data); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /BinaryTree/RemoveLeaf.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | 4 | public class Solution { 5 | 6 | /* Binary Tree Node class 7 | * 8 | * class BinaryTreeNode { 9 | T data; 10 | BinaryTreeNode left; 11 | BinaryTreeNode right; 12 | 13 | public BinaryTreeNode(T data) { 14 | this.data = data; 15 | } 16 | } 17 | */ 18 | 19 | public static BinaryTreeNode removeAllLeaves(BinaryTreeNode root){ 20 | 21 | if(root==null){ 22 | //if its null then we need to return some new node but not null 23 | //to identify the actual nodes where both left and right pointers are null 24 | return new BinaryTreeNode(-1); 25 | }else if(root.left==null&&root.right==null){//checking both pointers 26 | return null; 27 | } 28 | BinaryTreeNode value1; 29 | BinaryTreeNode value2; 30 | 31 | value1=removeAllLeaves(root.left); 32 | value2=removeAllLeaves(root.right); 33 | if(value1==null){//if value1 null then it means both its pointers are null but not a new node(not null values) 34 | root.left=null; 35 | } 36 | if(value2==null){//same as above 37 | root.right=null; 38 | } 39 | return root;//updated root 40 | } 41 | 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /LinkedList/Insertion_Print.java: -------------------------------------------------------------------------------- 1 | package com; 2 | 3 | import java.util.*; 4 | 5 | 6 | public class Insertion_Print { 7 | 8 | static Scanner sc=new Scanner(System.in); 9 | static Node head=null; 10 | public static void main(String[] args) { 11 | 12 | while(true){ 13 | System.out.println("Enter 1 to insert a new node \n2 to print\nAny other number to exit"); 14 | int x=sc.nextInt(); 15 | if(x==1){ 16 | create(); 17 | } 18 | else if(x==2){ 19 | print(); 20 | } 21 | else{ 22 | break; 23 | } 24 | } 25 | } 26 | public static void create(){ 27 | 28 | Integer data=sc.nextInt(); 29 | if(head==null){ 30 | Node node1=new Node(data); 31 | head=node1; 32 | // System.out.println(head); 33 | // System.out.println(node1); 34 | // System.out.println(node1.data); 35 | // System.out.println(node1.next); 36 | return; 37 | } 38 | //creating new node to insert 39 | Node newNode=new Node(data); 40 | Node temp=head; 41 | while(temp.next!=null){ 42 | temp=temp.next; 43 | } 44 | temp.next=newNode; 45 | newNode.next=null; 46 | } 47 | public static void print(){ 48 | Node temp=head; 49 | while(temp!=null){ 50 | System.out.println(temp.data); 51 | temp=temp.next; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Searching in Array: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Runner { 5 | 6 | static Scanner s = new Scanner(System.in); 7 | 8 | public static void main(String[] args) { 9 | int n = s.nextInt(); 10 | int input[] = new int[n]; 11 | for(int i = 0; i < n; i++) { 12 | input[i] = s.nextInt(); 13 | } 14 | int x = s.nextInt(); 15 | System.out.println(Solution.checkNumber(input, x)); 16 | } 17 | } 18 | 19 | public class Solution { 20 | 21 | public static boolean checkNumber(int input[], int x) { 22 | /* Your class should be named Solution 23 | * Don't write main(). 24 | * Don't read input, it is passed as function argument. 25 | * Return output and don't print it. 26 | * Taking input and printing output is handled automatically. 27 | */ 28 | if(input.length==1){ 29 | if(input[0]==x){ 30 | return true; 31 | } 32 | else{ 33 | return false; 34 | } 35 | } 36 | int smallerArray[]=new int[input.length-1]; 37 | for(int i=1;i { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | public static int indexOfNRec(LinkedListNode head, int n) { 15 | /* Your class should be named Solution 16 | * Don't write main(). 17 | * Don't read input, it is passed as function argument. 18 | * Return output and don't print it. 19 | * Taking input and printing output is handled automatically. 20 | */ 21 | //below works for distinct elements 22 | /* 23 | 24 | if(head==null){ 25 | return -1; 26 | } 27 | int index=indexOfNRec(head.next,n); 28 | if(index==-1){ 29 | if(head.data==n){ 30 | return 0; 31 | } 32 | }else if(index>=0){ 33 | return ++index; 34 | } 35 | return -1; 36 | */ 37 | //we need to return the first matched element index 38 | //i.e the first occurence 39 | if(head==null){ 40 | return -1; 41 | } 42 | if(head.data==n){ 43 | return 0; 44 | } 45 | int index=indexOfNRec(head.next,n); 46 | if(index>=0){ 47 | return ++index; 48 | } 49 | return -1; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /lastIndex.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Runner { 4 | 5 | static Scanner s = new Scanner(System.in); 6 | 7 | public static int[] takeInput(){ 8 | int size = s.nextInt(); 9 | int[] input = new int[size]; 10 | for(int i = 0; i < size; i++){ 11 | input[i] = s.nextInt(); 12 | } 13 | return input; 14 | } 15 | 16 | public static void main(String[] args) { 17 | int[] input = takeInput(); 18 | int x = s.nextInt(); 19 | System.out.println(Solution.lastIndex(input, x)); 20 | } 21 | } 22 | 23 | 24 | 25 | public class Solution { 26 | 27 | public static int lastIndex(int input[], int x) { 28 | /* Your class should be named Solution 29 | * Don't write main(). 30 | * Don't read input, it is passed as function argument. 31 | * Return output and don't print it. 32 | * Taking input and printing output is handled automatically. 33 | */ 34 | if(input.length<=1){ 35 | if(input[0]==x){ 36 | return 0; 37 | }else{ 38 | return -1; 39 | } 40 | } 41 | int []smallArray=new int[input.length-1]; 42 | for(int index=1;index=0){ 52 | return ++val; 53 | } 54 | return -1; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /BinaryTree/BalancedTree.java: -------------------------------------------------------------------------------- 1 | #pending problem 2 | 3 | /* 4 | Given a binary tree, check if its balanced i.e. depth of left and right subtrees of every node differ by at max 1. 5 | Return true if given binary tree is balanced, false otherwise. 6 | */ 7 | 8 | public class Solution { 9 | 10 | /* Binary Tree Node class 11 | * 12 | * class BinaryTreeNode { 13 | T data; 14 | BinaryTreeNode left; 15 | BinaryTreeNode right; 16 | 17 | public BinaryTreeNode(T data) { 18 | this.data = data; 19 | } 20 | } 21 | */ 22 | 23 | public static boolean checkBalanced(BinaryTreeNode root){ 24 | 25 | // Write your code here 26 | if(root==null){ 27 | return true; 28 | } 29 | int ldepth=height(root.left); 30 | int rdepth=height(root.right); 31 | if(checkBalanced(root.left)){ 32 | if(checkBalanced(root.right)){ 33 | if(Math.abs(ldepth-rdepth)<=1){ 34 | return true; 35 | } 36 | } 37 | 38 | 39 | } 40 | 41 | 42 | return false; 43 | } 44 | public static int height(BinaryTreeNode root){ 45 | if(root==null){ 46 | return 0; 47 | } 48 | int ldepth=height(root.left); 49 | int rdepth=height(root.right); 50 | if(ldepth>rdepth){ 51 | return ldepth+1; 52 | } 53 | else{ 54 | return rdepth+1; 55 | } 56 | } 57 | 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /LinkedList/Programs/delete.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static LinkedListNode deleteIthNode(LinkedListNode head, int i){ 16 | 17 | if(head==null){ 18 | return null; 19 | } 20 | //FINDING THE LENGTH OF THE LINKEDLIST 21 | LinkedListNode temp=head; 22 | int actual_length=0; 23 | while(temp!=null){ 24 | temp=temp.next; 25 | actual_length++; 26 | } 27 | //we got the actual length of LL 28 | //now the indexes we have assumed starts from 0 29 | // for input pattern 1 4 5 -1 30 | //3 31 | //so 3 is actually out of bound as we have assumed starting index :( as 0 32 | //but actually acc to length wise calcultion it exists so we have to give condition 33 | //so that i(to be found) should be in the boundary 34 | if(i>actual_length-1){ 35 | return head; 36 | } 37 | temp=head; 38 | if(i==0){ 39 | head=temp.next; 40 | return head; 41 | } 42 | LinkedListNode prev=null; 43 | int len=0; 44 | 45 | while(len { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | //only changing of pointers 13 | public class Solution { 14 | 15 | public static LinkedListNode mergeTwoList(LinkedListNode head1, LinkedListNode head2) { 16 | 17 | 18 | LinkedListNode temp,header1,header2,head3; 19 | header1=head1; 20 | header2=head2; 21 | if(header1.data<=header2.data){ 22 | head3=header1; 23 | temp=head1; 24 | head1=head1.next; 25 | temp.next=null; 26 | }else{ 27 | head3=header2; 28 | temp=head2; 29 | head2=head2.next; 30 | temp.next=null; 31 | } 32 | while(head1!=null && head2!=null){ 33 | if(head1.data<=head2.data){ 34 | temp.next=head1; 35 | temp=head1; 36 | head1=head1.next; 37 | temp.next=null; 38 | }else{ 39 | temp.next=head2; 40 | temp=head2; 41 | head2=head2.next; 42 | temp.next=null; 43 | } 44 | } 45 | if(head1!=null){ 46 | temp.next=head1; 47 | } 48 | if(head2!=null){ 49 | temp.next=head2; 50 | } 51 | return head3; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /allIndex.java: -------------------------------------------------------------------------------- 1 | //run on https://ide.geeksforgeeks.org/ 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static int[] allIndexes(int input[], int x) { 7 | /* Your class should be named Solution 8 | * Don't write main(). 9 | * Don't read input, it is passed as function argument. 10 | * Return output and don't print it. 11 | * Taking input and printing output is handled automatically. 12 | */ 13 | ArrayList arr= findAll(input,x,0,input.length-1); 14 | int actual[]=new int[arr.size()]; 15 | for(int i=0;i findAll(int input[],int x,int p,int r){ 22 | 23 | if(p==r){ 24 | ArrayList arr=new ArrayList(); 25 | if(input[0]==x){ 26 | arr.add(0); 27 | } 28 | return arr; 29 | } 30 | ArrayList arr1=findAll(input,x,p,r-1); 31 | if(input[r]==x){ 32 | arr1.add(r); 33 | } 34 | return arr1; 35 | } 36 | } 37 | 38 | class Runner { 39 | 40 | static Scanner s = new Scanner(System.in); 41 | 42 | public static int[] takeInput(){ 43 | int size = s.nextInt(); 44 | int[] input = new int[size]; 45 | for(int i = 0; i < size; i++){ 46 | input[i] = s.nextInt(); 47 | } 48 | return input; 49 | } 50 | 51 | public static void main(String[] args) { 52 | int[] input = takeInput(); 53 | int x = s.nextInt(); 54 | int output[] = Solution.allIndexes(input, x); 55 | for(int i = 0; i < output.length; i++) { 56 | System.out.print(output[i] + " "); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /LinkedList/Programs/DeleteNodesTricky.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class solution { 23 | public static LinkedListNode skipMdeleteN(LinkedListNode head, int M, int N) { 24 | 25 | LinkedListNode temp=head; 26 | LinkedListNode curr=null; 27 | while(temp!=null){ 28 | int l=M;//for traversing till m nodes 29 | int i=1; 30 | while(i { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | 13 | 14 | } 15 | * */ 16 | public class Solution { 17 | public static LinkedListNode sortEvenOdd(LinkedListNode head) { 18 | 19 | if(head==null){ 20 | return null; 21 | } 22 | int flageven=0,flagodd=0; 23 | LinkedListNode oddstart=null; 24 | LinkedListNode oddend=null; 25 | LinkedListNode evenstart=null; 26 | LinkedListNode evenend=null; 27 | LinkedListNode temp=head; 28 | while(temp!=null){ 29 | if(temp.data%2!=0){ 30 | if(oddstart==null){ 31 | oddstart=temp; 32 | oddend=oddstart; 33 | }else{ 34 | oddend.next=temp; 35 | oddend=temp; 36 | } 37 | flagodd++; 38 | }else{ 39 | if(evenstart==null){ 40 | evenstart=temp; 41 | evenend=evenstart; 42 | }else{ 43 | evenend.next=temp; 44 | evenend=temp; 45 | } 46 | flageven++; 47 | } 48 | temp=temp.next; 49 | } 50 | if(flageven!=0 && flagodd!=0){ 51 | 52 | oddend.next=evenstart; 53 | evenend.next=null; 54 | head=oddstart; 55 | 56 | }else{//the problem only is when we have only even no or odd no. in the lists 57 | //so we put flags count if there is a combination of odd and even both flags will increment from 0 to some values 58 | //if not then either of them will be zero which shows only unique type lists.(0,2,4,6),(1,3,5,7) 59 | return head;//so we return head as it is no need to change anything 60 | } 61 | return head; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /StackAndQueue/QueueUsingLL.java: -------------------------------------------------------------------------------- 1 | 2 | /*************** 3 | public static void main(String[] args) { 4 | Scanner s = new Scanner(System.in); 5 | 6 | Queue st = new Queue(); 7 | 8 | int choice = s.nextInt(); 9 | int input; 10 | 11 | while (choice !=-1) { 12 | if(choice == 1) { 13 | input = s.nextInt(); 14 | st.enqueue(input); 15 | } 16 | else if(choice == 2) { 17 | try { 18 | System.out.println(st.dequeue()); 19 | } catch (QueueEmptyException e) { 20 | System.out.println(-1); 21 | } 22 | } 23 | else if(choice == 3) { 24 | try { 25 | System.out.println(st.front()); 26 | } catch (QueueEmptyException e) { 27 | System.out.println(-1); 28 | } 29 | } 30 | else if(choice == 4) { 31 | System.out.println(st.size()); 32 | } 33 | else if(choice == 5) { 34 | System.out.println(st.isEmpty()); 35 | } 36 | choice = s.nextInt(); 37 | } 38 | } 39 | } 40 | 41 | class Node { 42 | T data; 43 | Node next; 44 | 45 | public Node(T data) { 46 | this.data = data; 47 | } 48 | } 49 | 50 | 51 | ***********/ 52 | 53 | class QueueEmptyException extends Exception{ 54 | QueueEmptyException(){ 55 | super(); 56 | } 57 | } 58 | 59 | public class Queue { 60 | 61 | private Node front; 62 | private Node rear; 63 | private int size; 64 | 65 | public Queue() { 66 | front=null; 67 | rear=null; 68 | size=0; 69 | } 70 | 71 | public void enqueue(T data) { 72 | Node newNode=new Node<>(data); 73 | newNode.next=null; 74 | if(front==null){ 75 | front=newNode; 76 | rear=newNode; 77 | size++; 78 | return; 79 | } 80 | rear.next=newNode; 81 | rear=newNode; 82 | size++; 83 | } 84 | 85 | public int size() { 86 | return size; 87 | } 88 | 89 | public boolean isEmpty() { 90 | if(size<=0){ 91 | return true; 92 | } 93 | return false; 94 | } 95 | 96 | public T dequeue() throws QueueEmptyException { 97 | 98 | 99 | T x=front.data; 100 | front=front.next; 101 | size--; 102 | return x; 103 | } 104 | public T front() throws QueueEmptyException { 105 | 106 | //front=front.next; 107 | return front.data; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /StackAndQueue/stackLL.java: -------------------------------------------------------------------------------- 1 | /*********** 2 | 3 | public static void main(String[] args) throws StackEmptyException { 4 | Scanner s = new Scanner(System.in); 5 | 6 | Stack st = new Stack(); 7 | 8 | int choice = s.nextInt(); 9 | int input; 10 | 11 | while (choice !=-1) { 12 | if(choice == 1) { 13 | input = s.nextInt(); 14 | st.push(input); 15 | } 16 | else if(choice == 2) { 17 | try { 18 | System.out.println(st.pop()); 19 | } catch (StackEmptyException e) { 20 | System.out.println(-1); 21 | } 22 | } 23 | else if(choice == 3) { 24 | try { 25 | System.out.println(st.top()); 26 | } catch (StackEmptyException e) { 27 | System.out.println(-1); 28 | } 29 | } 30 | else if(choice == 4) { 31 | System.out.println(st.size()); 32 | } 33 | else if(choice == 5) { 34 | System.out.println(st.isEmpty()); 35 | } 36 | choice = s.nextInt(); 37 | } 38 | } 39 | 40 | 41 | class Node { 42 | T data; 43 | Node next; 44 | 45 | public Node(T data) { 46 | this.data = data; 47 | } 48 | } 49 | 50 | ************/ 51 | 52 | class StackEmptyException extends Exception{ 53 | StackEmptyException(){ 54 | super(); 55 | } 56 | } 57 | 58 | public class Stack { 59 | 60 | private Node top; 61 | private int size; 62 | public Stack() { 63 | top=null; 64 | size=0; 65 | } 66 | 67 | public int size() { 68 | return size; 69 | } 70 | 71 | public void push(T data) { 72 | Node newNode=new Node<>(data); 73 | if(top==null){ 74 | top=newNode; 75 | newNode.next=null; 76 | size++;//this has to be present man:) 77 | return; 78 | } 79 | newNode.next=top; 80 | top=newNode; 81 | size++; 82 | } 83 | 84 | public boolean isEmpty() { 85 | 86 | if(size<=0){ 87 | return true; 88 | } 89 | return false; 90 | } 91 | 92 | public T pop() throws StackEmptyException{ 93 | 94 | if(top==null){ 95 | throw new StackEmptyException(); 96 | } 97 | T data=top.data; 98 | top=top.next; 99 | size--; 100 | return data; 101 | } 102 | 103 | public T top() throws StackEmptyException{ 104 | 105 | if(top==null){ 106 | throw new StackEmptyException(); 107 | } 108 | return top.data; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /MergeSort.java: -------------------------------------------------------------------------------- 1 | 2 | //run on https://ide.geeksforgeeks.org/ 3 | 4 | import java.util.Scanner; 5 | 6 | public class runner { 7 | public static int[] takeInput() { 8 | Scanner s = new Scanner(System.in); 9 | int size = s.nextInt(); 10 | int arr[] = new int[size]; 11 | for (int i = 0; i < size; i++) { 12 | arr[i] = s.nextInt(); 13 | } 14 | return arr; 15 | } 16 | 17 | public static void printArray(int input[]) { 18 | for(int i = 0; i < input.length; i++) { 19 | System.out.print(input[i] + " "); 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | int[] input = takeInput(); 25 | solution.mergeSort(input); 26 | printArray(input); 27 | } 28 | } 29 | 30 | public class solution { 31 | 32 | public static void mergeSort(int[] input){ 33 | merging(input,0,input.length-1); 34 | } 35 | public static void merging(int []input,int start,int end){ 36 | if(starthere 15 will be copied as the rest element in the array 80 | [1,8,22] [9,15] --> here 22 will be copied as it is the only element 81 | */ 82 | while (i < n1) 83 | { 84 | arr[k] = L[i]; 85 | i++; 86 | k++; 87 | } 88 | while (j < n2) 89 | { 90 | arr[k] = R[j]; 91 | j++; 92 | k++; 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /Keypad.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import java.lang.Math; 4 | 5 | 6 | import java.util.Scanner; 7 | 8 | public class runner { 9 | 10 | public static void main(String[] args) { 11 | Scanner s = new Scanner(System.in); 12 | int input = s.nextInt(); 13 | String output[] = solution.keypad(input); 14 | for(int i = 0; i < output.length; i++) { 15 | System.out.println(output[i]); 16 | } 17 | } 18 | } 19 | import java.lang.Math; 20 | 21 | class solution { 22 | 23 | // Return a string array that contains all the possible strings 24 | public static String[] keypad(int n){ 25 | // Write your code here 26 | 27 | if(n<=1){ 28 | String s[]={""}; 29 | return s; 30 | } 31 | String prev[]=keypad(n/10); 32 | String actual[]=new String[(int)Math.pow(4.00,count(n))]; 33 | //System.out.println(actual.length); 34 | String helper=setOfCharacter(n%10); 35 | int k=0; 36 | 37 | /*for(int i=0;i0){ 70 | count++; 71 | n=n/10; 72 | } 73 | return count; 74 | } 75 | /*returns the string corresponding to an integer*/ 76 | public static String setOfCharacter(int n){ 77 | switch(n){ 78 | case 2: return "abc"; 79 | 80 | case 3: return "def"; 81 | 82 | case 4: return "ghi"; 83 | 84 | case 5: return "jkl"; 85 | 86 | case 6: return "mno"; 87 | 88 | case 7: return "pqrs"; 89 | 90 | case 8: return "tuv"; 91 | 92 | case 9: return "wxyz"; 93 | 94 | default:break; 95 | } 96 | return ""; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /StackAndQueue/BalancedParanthesis.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static boolean checkBalanced(String exp) { 4 | 5 | Stack st=new Stack(); 6 | boolean val=false; 7 | 8 | for(int i=0;i