From 2d2b34002f4145c8ece714f4cc1991f57dd4d90e Mon Sep 17 00:00:00 2001 From: MrAkashKumar Date: Sun, 22 Nov 2020 15:56:03 +0530 Subject: [PATCH] add algorithm program --- Alogoritham/.classpath | 6 ++ Alogoritham/.project | 17 ++++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++ .../src/com/akash/circular/CircularOrNot.java | 45 +++++++++++ .../com/akash/hashTable/HashTableDemo.java | 44 ++++++++++ .../largestPrime/LargestPrimeNumber.java | 58 +++++++++++++ .../FindLargestGivenSum.java | 41 ++++++++++ .../LargestAreaTriangleAlgo.java | 28 +++++++ .../akash/matrixsprial/SprialMatrixDemo.java | 72 +++++++++++++++++ .../akash/matrixsprial/SprialOrderValue.java | 65 +++++++++++++++ .../MaxMinElementFromBinary.java | 81 +++++++++++++++++++ .../com/akash/mergesort/MergeSortExample.java | 57 +++++++++++++ .../com/akash/numOfIslands/NumOfIsLand.java | 59 ++++++++++++++ .../numOfIslands/NumOfIslandsProgram.java | 46 +++++++++++ .../akash/permutations/PermutationArray.java | 40 +++++++++ .../SearchNodeFromBinaryTree.java | 67 +++++++++++++++ .../com/akash/sequence/SequenceThread.java | 50 ++++++++++++ .../sortAnArrayofNth/SortArrayProgram.java | 45 +++++++++++ .../trappingRainWater/FindWaterFromArray.java | 41 ++++++++++ Alogoritham/src/com/akash/triNode/Trie.java | 65 +++++++++++++++ .../src/com/akash/triNode/TrieNode.java | 44 ++++++++++ 21 files changed, 982 insertions(+) create mode 100644 Alogoritham/.classpath create mode 100644 Alogoritham/.project create mode 100644 Alogoritham/.settings/org.eclipse.jdt.core.prefs create mode 100644 Alogoritham/src/com/akash/circular/CircularOrNot.java create mode 100644 Alogoritham/src/com/akash/hashTable/HashTableDemo.java create mode 100644 Alogoritham/src/com/akash/largestPrime/LargestPrimeNumber.java create mode 100644 Alogoritham/src/com/akash/largestSumGivenDigit/FindLargestGivenSum.java create mode 100644 Alogoritham/src/com/akash/largesttriangle/LargestAreaTriangleAlgo.java create mode 100644 Alogoritham/src/com/akash/matrixsprial/SprialMatrixDemo.java create mode 100644 Alogoritham/src/com/akash/matrixsprial/SprialOrderValue.java create mode 100644 Alogoritham/src/com/akash/max_min_binary/MaxMinElementFromBinary.java create mode 100644 Alogoritham/src/com/akash/mergesort/MergeSortExample.java create mode 100644 Alogoritham/src/com/akash/numOfIslands/NumOfIsLand.java create mode 100644 Alogoritham/src/com/akash/numOfIslands/NumOfIslandsProgram.java create mode 100644 Alogoritham/src/com/akash/permutations/PermutationArray.java create mode 100644 Alogoritham/src/com/akash/searchNodeInBinary/SearchNodeFromBinaryTree.java create mode 100644 Alogoritham/src/com/akash/sequence/SequenceThread.java create mode 100644 Alogoritham/src/com/akash/sortAnArrayofNth/SortArrayProgram.java create mode 100644 Alogoritham/src/com/akash/trappingRainWater/FindWaterFromArray.java create mode 100644 Alogoritham/src/com/akash/triNode/Trie.java create mode 100644 Alogoritham/src/com/akash/triNode/TrieNode.java diff --git a/Alogoritham/.classpath b/Alogoritham/.classpath new file mode 100644 index 0000000..e461bea --- /dev/null +++ b/Alogoritham/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Alogoritham/.project b/Alogoritham/.project new file mode 100644 index 0000000..3dbf1a2 --- /dev/null +++ b/Alogoritham/.project @@ -0,0 +1,17 @@ + + + Alogoritham_DataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Alogoritham/.settings/org.eclipse.jdt.core.prefs b/Alogoritham/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/Alogoritham/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/Alogoritham/src/com/akash/circular/CircularOrNot.java b/Alogoritham/src/com/akash/circular/CircularOrNot.java new file mode 100644 index 0000000..2f8483b --- /dev/null +++ b/Alogoritham/src/com/akash/circular/CircularOrNot.java @@ -0,0 +1,45 @@ +package com.akash.circular; + +public class CircularOrNot { + + public static void main(String[] args) { + // TODO + + String path = "GLGLGLG"; + + char chPath[] = path.toCharArray(); + + if(isCircularPath(chPath)) + System.out.println("Given Sequence "+ " of moves is circular"); + else + System.out.println("Given Sequence "+ " of moves is not circular"); + } + + private static boolean isCircularPath(char[] chPath) { + // TODO + + int x=0; int y=0; + int dir = 0; + + for(int i = 0; i(); + } + } + + /*public void insert(String value) { + int index = Math.abs(hashFunction(value)) % data.length; + this.data[index].insert(value); + } + + public boolean delete(String value) { + int index = Math.abs(hashFunction(value)) % data.length; + return data[index].delete(value); + } + + public boolean search(String value) { + int index = Math.abs(hashFunction(value)) % data.length; + return data[index].search(value); + } +*/ + private int hashFunction(String value) { + // TODO + int hash = 7; + for(char c : value.toCharArray()) { + hash = hash*31+c; + } + return hash; + } + + + + +} diff --git a/Alogoritham/src/com/akash/largestPrime/LargestPrimeNumber.java b/Alogoritham/src/com/akash/largestPrime/LargestPrimeNumber.java new file mode 100644 index 0000000..7bee46f --- /dev/null +++ b/Alogoritham/src/com/akash/largestPrime/LargestPrimeNumber.java @@ -0,0 +1,58 @@ +package com.akash.largestPrime; + +public class LargestPrimeNumber { + + public static void main(String[] args) { + // TODO + String str = "1000"; + StringBuilder sb = new StringBuilder(str); + System.out.println(largestPrimeNumber(sb)); + + } + + private static String largestPrimeNumber(StringBuilder sb) { + // TODO + for(int i = 0; i=0 && sb.charAt(i) <= '2') + i--; + if(i<0) { + i=0; + decreaseNum(sb, i); + } + else + decreaseNum(sb, i); + + for(int j = i+1; j 9*n) { + System.out.println("Not Possible"); + return; + } + + int res[] = new int[n]; + + for(int i =0; i=9) { + res[i] = 9; + num = num-9; + }else { + res[i] = num; + num = 0; + } + } + + System.out.print("largest Number : "); + for(int i = 0; iright) { + break; + } + + for(int i = left; i<=right; i++) { + mat[top][i]=arr[index++]; + } + top++; + + if(top>bottom) { + break; + } + + for(int i = top; i<=bottom; i++) { + mat[i][right] = arr[index++]; + } + right--; + + + if(left>right) { + break; + } + for(int i= right; i>=left; i--) { + mat[bottom][i] = arr[index++]; + } + bottom--; + + if(top>bottom) { + break; + } + + for(int i= bottom; i>=top; i--) { + mat[i][left] = arr[index++]; + } + left++; + + } + } +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/matrixsprial/SprialOrderValue.java b/Alogoritham/src/com/akash/matrixsprial/SprialOrderValue.java new file mode 100644 index 0000000..593b674 --- /dev/null +++ b/Alogoritham/src/com/akash/matrixsprial/SprialOrderValue.java @@ -0,0 +1,65 @@ +package com.akash.matrixsprial; + +public class SprialOrderValue { + + public static void main(String[] args) { + // TODO + int [][] mat = + { + {1,3,5,7,9}, + {4,6,1,8,0}, + {5,7,2,1,3}, + {1,3,1,7,4}, + }; + + sprialOrder(mat); + } + + private static void sprialOrder(int[][] mat) { + // TODO + + int top = 0, bottom = mat.length-1; + int left=0, right = mat[0].length-1; + + while(true) { + + if(left>right) { + break; + } + + for(int i = left; i<=right; i++) { + System.out.print(mat[top][i]+" "); + } + top++; + + if(top>bottom) { + break; + } + + for(int i= top; i<=bottom; i++) { + System.out.print(mat[i][right]+" "); + } + right--; + + if(left>right) { + break; + } + + for(int i = right; i>=left; i--) { + System.out.print(mat[bottom][i]+" "); + } + bottom--; + + + if(top>bottom) { + break; + } + + for(int i = bottom; i>=top; i--) { + System.out.print(mat[i][left]+ " "); + } + left++; + + } + } +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/max_min_binary/MaxMinElementFromBinary.java b/Alogoritham/src/com/akash/max_min_binary/MaxMinElementFromBinary.java new file mode 100644 index 0000000..d1c34fd --- /dev/null +++ b/Alogoritham/src/com/akash/max_min_binary/MaxMinElementFromBinary.java @@ -0,0 +1,81 @@ +package com.akash.max_min_binary; + +public class MaxMinElementFromBinary { + + static class Node{ + int key; + Node left, right; + }; + + static Node newNode(int key) { + Node temp = new Node(); + temp.key = key; + temp.left = temp.right = null; + return temp; + } + + static void printMinMax(Node root) { + if(root == null) { + System.out.println("Tree is Empty"); + return; + } + + Node current = root; + + Node pre; + + int max_value = Integer.MIN_VALUE; + int min_value = Integer.MAX_VALUE; + + while(current != null) { + + if(current.left == null) { + max_value = Math.max(max_value, current.key); + min_value = Math.min(min_value, current.key); + + current = current.right; + }else { + pre = current.left; + while(pre.right != null && pre.right != current) + + pre = pre.right; + + if(pre.right == null) { + pre.right = current; + current = current.left; + }else { + pre.right = null; + max_value = Math.max(max_value, current.key); + min_value = Math.min(min_value, current.key); + + current = current.right; + } + } + } + + System.out.println("Max Value is : "+max_value); + System.out.println("Min Value is : "+min_value); + + } + + public static void main(String[] args) { + // TODO + Node root = newNode(15); + root.left = newNode(19); + root.right = newNode(11); + + root.right.left = newNode(17); + root.right.right = newNode(29); + + root.right.left.left = newNode(10); + root.right.left.right = newNode(15); + root.right.right.left = newNode(11); + root.right.right.right = newNode(14); + + printMinMax(root); + + root.left = newNode(19); + + } + +} diff --git a/Alogoritham/src/com/akash/mergesort/MergeSortExample.java b/Alogoritham/src/com/akash/mergesort/MergeSortExample.java new file mode 100644 index 0000000..cc98217 --- /dev/null +++ b/Alogoritham/src/com/akash/mergesort/MergeSortExample.java @@ -0,0 +1,57 @@ +package com.akash.mergesort; + +public class MergeSortExample { + + public static void main(String[] args) { + // TODO + int arr[]= {4,6,8,16,2,7,21,63,41}; + int first = 0; + int last = arr.length-1; + mergeSort(arr, first, last); + } + + private static void mergeSort(int[] arr, int first, int last) { + // TODO Auto-generated method stub + + if(first right[rightPointer]) { + arr[i] = right[rightPointer]; + rightPointer++; + }else { + arr[i] = left[leftPointer]; + leftPointer++; + } + } + } +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/numOfIslands/NumOfIsLand.java b/Alogoritham/src/com/akash/numOfIslands/NumOfIsLand.java new file mode 100644 index 0000000..6acc546 --- /dev/null +++ b/Alogoritham/src/com/akash/numOfIslands/NumOfIsLand.java @@ -0,0 +1,59 @@ +package com.akash.numOfIslands; + +public class NumOfIsLand { + + /*for implement Java Code*/ + public static void main(String[] args) { + /*int[][] grid= {{1,1,1,1,0}, + {1,1,0,1,0}, + {1,1,0,0,0}, + {0,0,0,0,0}};*/ + + int[][] grid= { + {1,1,0,0,0}, + {1,1,0,0,0}, + {0,0,1,0,0}, + {0,0,0,1,1} + }; + + int num = numOfLand(grid); + System.out.println(num); + } + + private static int numOfLand(int[][] grid) { + // TODO + if(grid == null || grid.length==0) { + return 0; + } + int numIsLand = 0; + + for(int i = 0; i < grid.length; i++) { + + for(int j = 0; j < grid[i].length; j++) { + + if(grid[i][j] == 1) { + numIsLand += dfs(grid, i, j); + } + + } + + } + return numIsLand; + } + + private static int dfs(int[][] grid, int i, int j) { + // TODO + if(i<0 || i>= grid.length || j<0 || j>= grid[i].length || grid[i][j]==0) { + return 0; + } + + grid[i][j] = 0; + dfs(grid, i+1, j); + dfs(grid, i-1, j); + dfs(grid, i, j+1); + dfs(grid, i, j-1); + + return 1; + } + +} diff --git a/Alogoritham/src/com/akash/numOfIslands/NumOfIslandsProgram.java b/Alogoritham/src/com/akash/numOfIslands/NumOfIslandsProgram.java new file mode 100644 index 0000000..5ecfb37 --- /dev/null +++ b/Alogoritham/src/com/akash/numOfIslands/NumOfIslandsProgram.java @@ -0,0 +1,46 @@ +package com.akash.numOfIslands; + +public class NumOfIslandsProgram { + + /*for leet Code implement*/ + public static void main(String[] args) { + // TODO + char[][] grid= { + {1,1,1,1,0}, + {1,1,0,1,0}, + {1,1,0,0,0}, + {0,0,0,0,0} + }; + + int count = numIsland(grid); + + System.out.println(count); + } + + private static int numIsland(char[][] grid) { + // TODO + int count = 0; + for(int i =0; i=grid.length || j<0 || j>=grid[i].length || grid[i][j] == '0') + return; + + grid[i][j] = '0'; + callBFS(grid, i+1, j); + callBFS(grid, i-1, j); + callBFS(grid, i, j-1); + callBFS(grid, i, j+1); + } +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/permutations/PermutationArray.java b/Alogoritham/src/com/akash/permutations/PermutationArray.java new file mode 100644 index 0000000..a178136 --- /dev/null +++ b/Alogoritham/src/com/akash/permutations/PermutationArray.java @@ -0,0 +1,40 @@ +package com.akash.permutations; + +import java.util.HashSet; +import java.util.Set; + +public class PermutationArray { + + public static void main(String[] args) { + // TODO + + String str = "ABC"; + + System.out.println(getPermutation(str)); + } + + private static Set getPermutation(String str) { + // TODO + Set permutations = new HashSet<>(); + + if(str == null) { + return null; + }else if(str.length()==0) { + permutations.add(""); + return permutations; + } + + char ch = str.charAt(0); + + String sub = str.substring(1); + + Set words = getPermutation(sub); + + for(String word : words) { + for(int i=0; i<=word.length(); i++) { + permutations.add(word.substring(0,i)+ch+word.substring(i)); + } + } + return permutations; + } +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/searchNodeInBinary/SearchNodeFromBinaryTree.java b/Alogoritham/src/com/akash/searchNodeInBinary/SearchNodeFromBinaryTree.java new file mode 100644 index 0000000..44ccbcf --- /dev/null +++ b/Alogoritham/src/com/akash/searchNodeInBinary/SearchNodeFromBinaryTree.java @@ -0,0 +1,67 @@ +package com.akash.searchNodeInBinary; + +public class SearchNodeFromBinaryTree { + + public static class Node{ + int data; + Node left; + Node right; + + + public Node(int data) { + this.data = data; + this.left = null; + this.right = null; + + } + + } + + public Node root; + public static boolean flag = false; + + public SearchNodeFromBinaryTree() { + // TODO + root = null; + } + + public void searchNode(Node temp, int value) { + if(root == null) { + System.out.println("Tree is Empty"); + }else { + if(temp.data == value) { + flag = true; + return; + } + + if(flag == false && temp.left != null) { + searchNode(temp.left, value); + } + + if(flag == false && temp.right != null) { + searchNode(temp.right, value); + } + } + } + + + public static void main(String[] args) { + + SearchNodeFromBinaryTree search = new SearchNodeFromBinaryTree(); + + search.root = new Node(1); + search.root.left = new Node(2); + search.root.right = new Node(3); + search.root.left.left = new Node(4); + search.root.right.left = new Node(5); + search.root.right.right = new Node(6); + + search.searchNode(search.root, 10); + + if(flag) + System.out.println("Element is parent in the binary tree"); + else + System.out.println("Element is not present in the binary tree"); + } + +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/sequence/SequenceThread.java b/Alogoritham/src/com/akash/sequence/SequenceThread.java new file mode 100644 index 0000000..256e261 --- /dev/null +++ b/Alogoritham/src/com/akash/sequence/SequenceThread.java @@ -0,0 +1,50 @@ +package com.akash.sequence; + +public class SequenceThread implements Runnable{ + + public int Print_Number= 10; + static int number = 1; + int remainder; + + static Object lock = new Object(); + + public SequenceThread(int remainder) { + // TODO + this.remainder = remainder; + } + public static void main(String[] args) { + // TODO + + SequenceThread sequenceThread0 = new SequenceThread(0); + SequenceThread sequenceThread1 = new SequenceThread(1); + SequenceThread sequenceThread2 = new SequenceThread(2); + + Thread t1 = new Thread(sequenceThread0, "T1"); + Thread t2 = new Thread(sequenceThread1, "T2"); + Thread t3 = new Thread(sequenceThread2, "T3"); + + t1.start(); + t2.start(); + t3.start(); + } + + @Override + public void run() { + // TODO + while (number < Print_Number-1) { + synchronized (lock) { + while(number%3 != remainder) { + try { + lock.wait(); + + }catch(InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println(Thread.currentThread().getName()+ " "+ number); + number++; + lock.notifyAll(); + } + } + } +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/sortAnArrayofNth/SortArrayProgram.java b/Alogoritham/src/com/akash/sortAnArrayofNth/SortArrayProgram.java new file mode 100644 index 0000000..dda2def --- /dev/null +++ b/Alogoritham/src/com/akash/sortAnArrayofNth/SortArrayProgram.java @@ -0,0 +1,45 @@ +package com.akash.sortAnArrayofNth; + +public class SortArrayProgram { + + public static void main(String[] args) { + // TODO + + int arr[] = {0,1,2,0,1,2}; + + sortedByArray(arr, arr.length); + + for(int i=0; ileft) { + left = arr[low]; + }else { + result += left -arr[low]; + low++; + } + }else { + if(arr[high]> right) { + right = arr[high]; + }else { + result += right-arr[high]; + high--; + } + } + } + return result; + } +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/triNode/Trie.java b/Alogoritham/src/com/akash/triNode/Trie.java new file mode 100644 index 0000000..3906adb --- /dev/null +++ b/Alogoritham/src/com/akash/triNode/Trie.java @@ -0,0 +1,65 @@ +package com.akash.triNode; + +public class Trie { + + TrieNode root; + + Trie(){ + this.root = new TrieNode(); + } + + public void insert(String value) { + char[] input = value.toCharArray(); + TrieNode trieNode = root; + + for(char c : input) { + if(!trieNode.haveRecord(c)) { + trieNode.addRecord(c, new TrieNode()); + } + trieNode = trieNode.followChar(c); + } + trieNode.setIsWord(true); + } + + public boolean search(String value) { + char[] input = value.toCharArray(); + TrieNode n = root; + + for(int i = 0; i=0; i++) { + if(n.haveNoRecord() && (!n.isWord())) { + n = path[i]; + n.deleteRecord(ch[inputIndex]); + inputIndex--; + }else { + break; + } + } + return true; + } +} \ No newline at end of file diff --git a/Alogoritham/src/com/akash/triNode/TrieNode.java b/Alogoritham/src/com/akash/triNode/TrieNode.java new file mode 100644 index 0000000..693e376 --- /dev/null +++ b/Alogoritham/src/com/akash/triNode/TrieNode.java @@ -0,0 +1,44 @@ +package com.akash.triNode; + +import java.util.HashMap; +import java.util.Map; + +public class TrieNode { + + private Map table; + private boolean isWord; + + public TrieNode() { + this.table= new HashMap(); + isWord = false; + } + + public boolean haveRecord(char ch) { + return this.table.containsKey(ch); + } + + public boolean haveNoRecord() { + return this.table.isEmpty(); + } + + public TrieNode followChar(char ch) { + return this.table.get(ch); + } + + public void addRecord(char ch, TrieNode t) { + this.table.put(ch, t); + } + + public void deleteRecord(char ch) { + this.table.remove(ch); + } + + public boolean isWord() { + return isWord; + } + + public void setIsWord(boolean isWord) { + this.isWord = isWord; + } + +} \ No newline at end of file