diff --git a/Recursion/BinarySum.java b/Recursion/BinarySum.java new file mode 100644 index 0000000..e899501 --- /dev/null +++ b/Recursion/BinarySum.java @@ -0,0 +1,32 @@ +import java.math.BigInteger; + +public class BinarySum { + + /* + * Given two binary strings a and b, return their sum as a binary string. + + + +Example 1: + +Input: a = "11", b = "1" +Output: "100" +Example 2: + +Input: a = "1010", b = "1011" +Output: "10101" + * + */ + public static void main(String[] args) { + var classname = new BinarySum(); + System.out.println(classname.addBinary("1010", "1011")); + } + + public String addBinary(String a, String b) { + BigInteger x = new BigInteger(a, 2); + BigInteger y = new BigInteger(b, 2); + BigInteger sum = x.add(y); + return sum.toString(2); + } + +} diff --git a/Recursion/FindSqrt.java b/Recursion/FindSqrt.java new file mode 100644 index 0000000..6ff2484 --- /dev/null +++ b/Recursion/FindSqrt.java @@ -0,0 +1,35 @@ +import java.math.BigInteger; + +public class FindSqrt { + + + public static void main(String[] args) { + + + } + + public int mySqrt(int x) { + BigInteger c = BigInteger.valueOf(x); + return c.sqrt().intValue(); + } + + public int mySqrt2(int x) { + if(x == 0){ + return 0; + } + int start = 1; + int end = x; + int ans = 0; + while(start <= end){ + int mid = start + (end-start)/2; + //instead of mid*mid we are giving x/mid to tackle overflow of integer range when multiplying with bigger numbers + if(mid <= x/mid){ + ans = mid; + start = mid + 1; + }else{ + end = mid -1; + } + } + return ans; + } +} diff --git a/SinglyLinkedList/SinglyLinkedList.java b/SinglyLinkedList/SinglyLinkedList.java index 23cb6ca..6461068 100644 --- a/SinglyLinkedList/SinglyLinkedList.java +++ b/SinglyLinkedList/SinglyLinkedList.java @@ -53,7 +53,7 @@ else if(location>=size){ public void traverseLinkedList(Node head){ if(head==null){ - System.out.println("The hea is null and there is no Linked List linked to it"); + System.out.println("The head is null and there is no Linked List linked to it"); return; } @@ -139,4 +139,13 @@ else if(location==size){ } } + + public void deleteSingleLinkedList(){ + if(head==null) System.out.println("the single linked list is already empty"); + else { + head=null;tail=null; + System.out.println("Single linked list is deleted"); + } + return; + } } \ No newline at end of file diff --git a/SinglyLinkedList/testsinglelinkedlist.java b/SinglyLinkedList/testsinglelinkedlist.java index 40a4983..dcb0291 100644 --- a/SinglyLinkedList/testsinglelinkedlist.java +++ b/SinglyLinkedList/testsinglelinkedlist.java @@ -17,5 +17,7 @@ public static void main(String[] args) { s.searchValueInLinkedList(10); s.deleteNode(4); s.traverseLinkedList(s.head); + s.deleteSingleLinkedList(); + s.traverseLinkedList(s.head); } }