diff --git a/C++_DSA/BinaryTreeInorderTraversal.cpp b/C++_DSA/BinaryTreeInorderTraversal.cpp new file mode 100644 index 0000000..64d638d --- /dev/null +++ b/C++_DSA/BinaryTreeInorderTraversal.cpp @@ -0,0 +1,68 @@ +/* + +Binary Tree Inorder Traversal + +Given the root of a binary tree, return the inorder traversal of its nodes' values. + + + +Input: root = [1,null,2,3] +Output: [1,3,2] +Example 2: + +Input: root = [] +Output: [] +Example 3: + +Input: root = [1] +Output: [1] + + +Constraints: + +The number of nodes in the tree is in the range [0, 100]. +-100 <= Node.val <= 100 + + +*/ + + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + + + + vector inorderTraversal(TreeNode* root) { + vector v; + + stack s; + + + while(root || !s.empty()){ + + while(root){ + s.push(root); + root=root->left; + } + root=s.top(); + s.pop(); + v.push_back(root->val); + root=root->right; + } + + + return v; + } +}; \ No newline at end of file diff --git a/C++_DSA/BinaryTreeMinimumDepth.cpp b/C++_DSA/BinaryTreeMinimumDepth.cpp new file mode 100644 index 0000000..b833d22 --- /dev/null +++ b/C++_DSA/BinaryTreeMinimumDepth.cpp @@ -0,0 +1,69 @@ +/* + +Minimum Depth of Binary Tree + +Given a binary tree, find its minimum depth. + +The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. + +Note: A leaf is a node with no children. + + +Input: root = [3,9,20,null,null,15,7] +Output: 2 +Example 2: + +Input: root = [2,null,3,null,4,null,5,null,6] +Output: 5 + + +Constraints: + +The number of nodes in the tree is in the range [0, 105]. +-1000 <= Node.val <= 1000 + +*/ + + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int minDepth(TreeNode* root) { + int c=0; + if(!root) return c; + + queue q; + + q.push(root); + + while(!q.empty()){ + + int x=q.size(); + c++; + while(x){ + TreeNode *t=q.front(); + q.pop(); + if(t->left ==nullptr && t->right ==nullptr){ + return c; + } + if(t->left) q.push(t->left); + if(t->right) q.push(t->right); + x--; + } + + + } + + return c; + } +}; \ No newline at end of file diff --git a/FibonacciNumberFunction.java b/FibonacciNumberFunction.java new file mode 100644 index 0000000..063052c --- /dev/null +++ b/FibonacciNumberFunction.java @@ -0,0 +1,16 @@ + //Using Dynamic Programming + static int fib(int n) + { + int f[] = new int[n+2]; // 1 extra to handle case, n = 0 + int i; + + f[0] = 0; + f[1] = 1; + + for (i = 2; i <= n; i++) + { + f[i] = f[i-1] + f[i-2]; + } + + return f[n]; + } diff --git a/Java_DSA/DSA-Recursion.java b/Java_DSA/DSA-Recursion.java new file mode 100644 index 0000000..5f49b1e --- /dev/null +++ b/Java_DSA/DSA-Recursion.java @@ -0,0 +1,33 @@ +public class RecursionDemo { + public static void main(String[] args){ + RecursionDemo recursionDemo = new RecursionDemo(); + int n = 5; + System.out.println(Factorial of + n + + + recursionDemo.factorial(n)); + System.out.print(Fibbonacci of + n + ); + for(int i=0;in;i++){ + System.out.print(recursionDemo.fibbonacci(i) + ); + } + } + + private int factorial(int n){ + base case + if(n == 0){ + return 1; + }else{ + return n factorial(n-1); + } + } + + private int fibbonacci(int n){ + if(n ==0){ + return 0; + } + else if(n==1){ + return 1; + } + else { + return (fibbonacci(n-1) + fibbonacci(n-2)); + } + } +} \ No newline at end of file diff --git a/Java_DSA/FractionalKnapsack.java b/Java_DSA/FractionalKnapsack.java deleted file mode 100644 index 806c7c7..0000000 --- a/Java_DSA/FractionalKnapsack.java +++ /dev/null @@ -1,88 +0,0 @@ -// Java program to solve fractional Knapsack Problem -import java.util.Comparator; -import java.util.Arrays; -// the greedy approach -public class FractionalKnapSack -{ -// method to get maximum value -private static double getMaxValue(int[] w, int[] v, int c) -{ -// length of the array -int size = w.length; -// array of storing all the items -ItemValues[] iValue = new ItemValues[size]; -for (int j = 0; j < size; j++) -{ -// storing the items -iValue[j] = new ItemValues(w[j], v[j], j); -} -// sorting the items on the basis of cost -Arrays.sort(iValue, new Comparator() -{ -@Override -public int compare(ItemValues i1, ItemValues i2) -{ -return i2.cost.compareTo(i1.cost); -} -}); -// contains the maximum value that is possible -// for the given capacity of the knapsack -double totalVal = 0d; -for (int j = 0; j < size; j++) -{ -// obtaining the current weight -// and value of the ith item -int currWt = (int)iValue[j].w; -int currVal = (int)iValue[j].value; -if (c - currWt >= 0) -{ -// the current capacity of the knapsack allows the -// item to be taken as a whole -cc = c - currWt; -totalValtotalVal = totalVal + currVal; -} -else -{ -// when an item can not be picked as a whole -// we break the item and take a portion of it -// such that we get the maximum value in the knapsack -double fraction = ((double)c / (double)currWt); -totalValtotalVal = totalVal + (currVal * fraction); -c = (int)(c - (currWt * fraction)); -// the knapsack is full -// no need to proceed further -break; -} -} -return totalVal; -} -// Itemvalues class -static class ItemValues -{ -Double cost; -double w, value, index; -// item value function -public ItemValues(int w, int value, int index) -{ -this.w = w; -this.value = value; -this.index = index; -// on the basis of cost -// we sort the item -cost = new Double((double)value / (double)w); -} -} -// main method -public static void main(String argvs[]) -{ -// input arrays -int[] weight = { 40, 10, 20, 30 }; -int[] values = { 40, 60, 100, 120 }; -// capacity of the knapsack -int C = 50; -// invoking the getMaxValue() method -double maxVal = getMaxValue(weight, values, C); -// printing the result -System.out.println("Maximum value that can be obtained is = " + maxVal); -} -} diff --git a/Java_DSA/HouseRobber.java b/Java_DSA/HouseRobber.java new file mode 100644 index 0000000..91de408 --- /dev/null +++ b/Java_DSA/HouseRobber.java @@ -0,0 +1,30 @@ +/* You are a professional robber planning to rob houses along a street. + Each house has acertain amount of monet stashed. + The onlt constraint stopping you for robbing each of them is that each house have security systems connected and it will automatically contact the police + if two ajacent houses were broken into on the same night. + Considering the constarints determine the maximum amount of money you can rob tonight without alerting the police. +*/ + +public class HouseRobber{ + public static void main(String[] args) { + int[] arr = {2,45,8,7,98,24}; + int maxAmount = robHouse(arr); + System.out.println("Maximum amount that can be robbed : "+ maxAmount); + } + + static int robHouse(int[] arr){ + int prev = arr[0], curr = Math.max(arr[0], arr[1]); + + for(int i =2; i< arr.length; i++){ + int temp = curr; + curr = Math.max(prev + arr[i], curr); + prev = temp; + } + return curr; + } +} + + + + +//prints:- Maximum amount that can be robbed : 143 diff --git a/Java_DSA/LeetCode_Same_Tree.java b/Java_DSA/LeetCode_Same_Tree.java deleted file mode 100644 index f1bcc3f..0000000 --- a/Java_DSA/LeetCode_Same_Tree.java +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Definition for a binary tree node. - * public class TreeNode { - * int val; - * TreeNode left; - * TreeNode right; - * TreeNode() {} - * TreeNode(int val) { this.val = val; } - * TreeNode(int val, TreeNode left, TreeNode right) { - * this.val = val; - * this.left = left; - * this.right = right; - * } - * } - */ -class Solution { - public boolean isSameTree(TreeNode p, TreeNode q) { - if(p == null || q == null) { - return p==q; - } - return p.val == q.val && this.isSameTree(p.left, q.left) && this.isSameTree(p.right, q.right); - } -} diff --git a/Java_DSA/ShortestPathInAMaze.java b/Java_DSA/ShortestPathInAMaze.java new file mode 100644 index 0000000..2363587 --- /dev/null +++ b/Java_DSA/ShortestPathInAMaze.java @@ -0,0 +1,64 @@ +package backtracking; + +public class ShortestPathInAMaze { + + public static void main(String[] args) { + int a[][] = + { + { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 }, + { 0, 1, 1, 1, 1, 1, 0, 1, 0, 1 }, + { 0, 0, 1, 0, 1, 1, 1, 0, 0, 1 }, + { 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 }, + { 0, 0, 0, 1, 0, 0, 0, 1, 0, 1 }, + { 1, 0, 1, 1, 1, 0, 0, 1, 1, 0 }, + { 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 }, + { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 }, + { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 }, + { 0, 0, 1, 0, 0, 1, 1, 0, 0, 1 }, + }; + + int result = shortestPath(a, 0, 0, 0, 9); + if(result >= 1000000) { + System.out.println("No path possible"); + } else { + System.out.println(result); + } + + } + + static int shortestPath(int a[][], int i, int j, int x, int y) { + + int rows = a.length; + int cols = a[0].length; + + boolean vis[][] = new boolean[rows][cols]; + + return shortestPath(a, i, j, x, y, vis); + + } + + static boolean isValid(int a[][], int i, int j, boolean vis[][]) { + int rows = a.length; + int cols = a[0].length; + + return i >= 0 && j >= 0 && i < rows && j < cols && a[i][j] == 1 && + !vis[i][j]; + } + + static int shortestPath(int a[][], int i, int j, int x, int y, boolean vis[][]) { + + if(!isValid(a, i, j, vis)) return 1000000; + if(i == x && j == y) return 0; + + vis[i][j] = true; + int left = shortestPath(a, i, j-1, x, y, vis) + 1; + int bottom = shortestPath(a, i+1, j, x, y, vis)+1; + int right = shortestPath(a, i, j+1, x, y, vis)+1; + int top = shortestPath(a, i-1, j, x, y, vis)+1; + + // This line makes backtracking work + vis[i][j] = false; + return Math.min(Math.min(left, bottom), Math.min(right, top)); + + } +} diff --git a/Java_DSA/linkedLists/MainLinkedList.java b/Java_DSA/linkedLists/MainLinkedList.java new file mode 100644 index 0000000..9a2929b --- /dev/null +++ b/Java_DSA/linkedLists/MainLinkedList.java @@ -0,0 +1,22 @@ +package linkedLists; + +import java.util.*; + +public class MainLinkedList { + + public static void main(String[] args) { + + MyLinkedList myLL = new MyLinkedList(); + + + for (int i = 0; i < 10; i++) { + // add method use to add a element in the last node of the linked list + myLL.add(i + "added"); + + } +// print method use to print the linked list + myLL.print(); + } + +} + diff --git a/Java_DSA/linkedLists/MyLinkedList.java b/Java_DSA/linkedLists/MyLinkedList.java new file mode 100644 index 0000000..cf8c7fc --- /dev/null +++ b/Java_DSA/linkedLists/MyLinkedList.java @@ -0,0 +1,84 @@ +package linkedLists; + +public class MyLinkedList { + + +//E is the Class defining the type of the inputs accepted + Node head; + + public void add(E data) { + Node toAdd = new Node(data); + + if (isEmpty()) { + head = toAdd; + return; + } + +//initialising temp as head to traverse the Linked list without breaking the chain + Node temp = head; + // control from loop exits as soon as next element becomes null + while (temp.next != null) { + + temp = temp.next; + } + // adding the new node after reaching to the end of linked list + temp.next = toAdd; + } + + void print() { + Node temp = head; + while (temp != null) { + System.out.print(temp.data + " "); + temp = temp.next; + } + } + + public boolean isEmpty() { + return head == null; + } + + public E removeLast() throws Exception { + Node temp = head; + + if (temp == null) { + throw new Exception("Cannot remove last element from empty linked list"); + } + + if (temp.next == null) { + Node toRemove = head; + head = null; + return toRemove.data; + } + + while (temp.next.next != null) { + temp = temp.next; + } + Node toRemove = temp.next; + temp.next = null; // changing the pointer of temp.next from toRemove to null,and garbage collection is done automatically + return toRemove.data; + } + + public E getLast() throws Exception { + Node temp = head; + + if (temp == null) { + throw new Exception("Cannot peek last element from empty linked list"); + } + while (temp.next != null) { + temp = temp.next; + } + return temp.data; + } + + public static class Node { + public E data; + public Node next; + +//constructor + public Node(E data) { + + this.data = data; + next = null; + } + } +} diff --git a/LongestSubstringWithoutRepeatingCharacters.java b/LongestSubstringWithoutRepeatingCharacters.java new file mode 100644 index 0000000..a9b81cb --- /dev/null +++ b/LongestSubstringWithoutRepeatingCharacters.java @@ -0,0 +1,19 @@ +class Solution { + public int lengthOfLongestSubstring(String s) { + int left = 0, right = 0, maxWindowSize = -1; + if(s.length() == 0 || s.length() == 1){ return s.length(); } + HashMap map = new HashMap(); + while(left < s.length() && right < s.length()){ + if(!map.containsKey(s.charAt(right))){ + map.put(s.charAt(right), 1); + right++; + maxWindowSize = Math.max(maxWindowSize, right - left); + } + else{ + map.remove(s.charAt(left)); + left++; + } + } + return maxWindowSize; + } +} \ No newline at end of file diff --git a/Program15.cpp b/Program15.cpp new file mode 100644 index 0000000..b5100e0 --- /dev/null +++ b/Program15.cpp @@ -0,0 +1,69 @@ +#include +#include +void sum(int A[10][10] ,int N ) +{ +int i,j,sum=0; +for(i=0;ij||j>i) + sum+=A[i][j]; + } + cout<<"sum="<>N; +for(i=0;i>A[i][j]; + } +cout<<"Menu \n1 Sum of upper and lower diagonal elements \n2 Product of diagonal elements\n3 transpose and display "; + cin>>ch; +if(ch==1) + sum(A,N); +else if(ch==2) + prod(A,N); +else if(ch==3) + trans(A,N); +else + cout<<"invalid choice"; +cout<<"\nDo you want to continue?"; +cin>>y; +} +getch(); +} \ No newline at end of file diff --git a/Program17.cpp b/Program17.cpp new file mode 100644 index 0000000..6610754 --- /dev/null +++ b/Program17.cpp @@ -0,0 +1,55 @@ +#include +#include +void arr(int a[10],int n) +{ + int b[10][10],i,j; + for(i=0;i>s>>r; + for(i=n;i>r;i--) + a[i]=a[i-1]; + a[r]=s; + for(i=0;i<=n;i++) + cout<>n; + cout<<"enter array"; + for(i=0;i>a[i]; + cout<<"menu \n1 Assign to 2D array \n2 Insert an element"; + cin>>ch; + if(ch==1) + arr(a,n); + else if(ch==2) + ins(a,n); + else + cout<<"invalid choice"; + cout<<"\nDo you want to continue?"; + cin>>x; + } + getch(); +} \ No newline at end of file diff --git a/README.md b/README.md index 42fa625..a7e6856 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ I sorry for that repository excluded even I don't know how that was happening. Keep supporting me -Add contribution to this repository if got it like excluded +Add contribution to this repository if got it like excludedadd new repository +last four days hactoberfest https://github.com/prashantkalokhe/Hactoberfest-2022-New diff --git a/README.me b/README.me index 23530fc..d574e36 100644 --- a/README.me +++ b/README.me @@ -11,3 +11,5 @@ ___ - for VSCODE editor --------> at first copy the code and save in vs code with .ipynb format. Then if you download the extension of jupyter then the code surely work. --- +

prashantkalokhe

+ diff --git a/SimpleCalculator/CSS/style.css b/SimpleCalculator/CSS/style.css deleted file mode 100644 index 0a7a51b..0000000 --- a/SimpleCalculator/CSS/style.css +++ /dev/null @@ -1,79 +0,0 @@ -*{ - margin: 0%; - padding: 0%; - box-sizing: border-box; - background-color: rgb(212, 194, 255); - font-family: 'Nunito Sans', sans-serif; - font-family: 'Ubuntu', sans-serif; - font-size: 15px; - font-weight: 600; - outline: none; -} - -.container{ - height: 100vh; - display: flex; - justify-content: center; - align-items: center; - text-align: center; -} - -.calculator{ - background-color: rgb(254, 254, 255); - padding: 15px; - border-radius: 35px; - box-shadow: inset 5px 5px 12px white, - 5px 5px 12px rgb(165, 165, 165); - display: grid; - grid-template-columns: 70px 70px 70px 70px; -} - -a,label{ - margin-left: 15px; - background-color: rgb(241, 240, 237); - border-radius: 25px; - width: max-content; -} - -input{ - grid-column: span 4; - height: 70px; - width: 100%; - background-color: rgb(230, 230, 230); - box-shadow: inset -5px -5px 15px rgb(170, 170, 170), - 5px 5px 12px rgb(246, 246, 246); - border: none; - border-radius: 27px; - color: rgb(63, 63, 63); - font-size: 35px; - text-align: right; - margin: 35px 0px 25px 0px; - padding: 20px; -} - -button{ - height: 50px; - width: 50px; - padding: 20px; - border:none; - background-color: rgb(253, 253, 253); - box-shadow: inset -5px -5px 12px rgb(226, 226, 226), - 5px 5px 12px rgb(136, 136, 136); - border-radius: 50%; - margin:10px; - text-align: center; - cursor: pointer; -} - -button.equal{ - border-radius: 20px; - width: 110px; -} - -button:hover{ - background-color: rgb(255, 255, 255); - font-weight: 500; -} - - - diff --git a/SimpleCalculator/HTML/index.html b/SimpleCalculator/HTML/index.html deleted file mode 100644 index f9e5865..0000000 --- a/SimpleCalculator/HTML/index.html +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - Calculator - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - -
-
- - - \ No newline at end of file diff --git a/SimpleCalculator/IMG/calculatorTitle.png b/SimpleCalculator/IMG/calculatorTitle.png deleted file mode 100644 index 08deaf5..0000000 Binary files a/SimpleCalculator/IMG/calculatorTitle.png and /dev/null differ diff --git a/SimpleCalculator/JS/script.js b/SimpleCalculator/JS/script.js deleted file mode 100644 index 9daba4a..0000000 --- a/SimpleCalculator/JS/script.js +++ /dev/null @@ -1,22 +0,0 @@ -let userInput = document.getElementById("user-Input"); - - -function display(num){ - userInput.value += num; -} - -function Calculate(){ - try{ - userInput.value = eval(userInput.value); - } catch(err){ - alert("Invalid Input!"); - } -} - -function Clear(){ - userInput.value=""; -} - -function Delete(){ - userInput.value = userInput.value.slice(0,-1); -} \ No newline at end of file diff --git a/SimpleCalculator/README.md b/SimpleCalculator/README.md deleted file mode 100644 index ee16254..0000000 --- a/SimpleCalculator/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# SimpleCalculator - -Check out live here: https://jainshubham766.github.io/SimpleCalculator/HTML/index.html \ No newline at end of file diff --git a/kroneckerproduct.java b/kroneckerproduct.java new file mode 100644 index 0000000..0aecd1f --- /dev/null +++ b/kroneckerproduct.java @@ -0,0 +1,63 @@ +// Java code to find the Kronecker Product of +// two matrices and stores it as matrix C +import java.io.*; +import java.util.*; + +class GFG { + + // rowa and cola are no of rows and columns + // of matrix A + // rowb and colb are no of rows and columns + // of matrix B + static int cola = 2, rowa = 3, colb = 3, rowb = 2; + + // Function to computes the Kronecker Product + // of two matrices + static void Kroneckerproduct(int A[][], int B[][]) + { + + int[][] C= new int[rowa * rowb][cola * colb]; + + // i loops till rowa + for (int i = 0; i < rowa; i++) + { + + // k loops till rowb + for (int k = 0; k < rowb; k++) + { + + // j loops till cola + for (int j = 0; j < cola; j++) + { + + // l loops till colb + for (int l = 0; l < colb; l++) + { + + // Each element of matrix A is + // multiplied by whole Matrix B + // resp and stored as Matrix C + C[i + l + 1][j + k + 1] = A[i][j] * B[k][l]; + System.out.print( C[i + l + 1][j + k + 1]+" "); + } + } + System.out.println(); + } + } + } + + // Driver program + public static void main (String[] args) + { + int A[][] = { { 1, 2 }, + { 3, 4 }, + { 1, 0 } }; + + int B[][] = { { 0, 5, 2 }, + { 6, 7, 3 } }; + + Kroneckerproduct(A, B); + } +} + +// This code is contributed by Gitanjali. diff --git a/reverse_integer.cpp b/reverse_integer.cpp new file mode 100644 index 0000000..1e9dfa1 --- /dev/null +++ b/reverse_integer.cpp @@ -0,0 +1,27 @@ +/* + Reverse Integer + +Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. +Assume the environment does not allow you to store 64-bit integers (signed or unsigned). + +*/ + +class Solution { +public: + int reverse(int x) { + + long long ans=0; + while(x!=0) + { + + int rem=x%10; + ans=ans*10+rem; + x/=10; + } + + if(ans>INT_MAX || ans