From dcf0167126a1112f440bdce8dd01c2f837a45ac1 Mon Sep 17 00:00:00 2001 From: Aniz <20am014@gmail.com> Date: Sat, 1 Oct 2022 21:57:58 +0530 Subject: [PATCH] Added leetcode questions --- Java/Count_Operations_to_Obtain_Zero.java | 24 ++++++++ ...etermine_Color_of_a_Chessboard_Square.java | 19 ++++++ ...Find_Greatest_Common_Divisor_of_Array.java | 61 +++++++++++++++++++ Java/Linked List/Linked_List_Cycle.java | 41 +++++++++++++ .../Middle_of_the_Linked_List.java | 43 +++++++++++++ Java/Linked List/Reverse_Linked_List.java | 48 +++++++++++++++ 6 files changed, 236 insertions(+) create mode 100644 Java/Count_Operations_to_Obtain_Zero.java create mode 100644 Java/Determine_Color_of_a_Chessboard_Square.java create mode 100644 Java/Find_Greatest_Common_Divisor_of_Array.java create mode 100644 Java/Linked List/Linked_List_Cycle.java create mode 100644 Java/Linked List/Middle_of_the_Linked_List.java create mode 100644 Java/Linked List/Reverse_Linked_List.java diff --git a/Java/Count_Operations_to_Obtain_Zero.java b/Java/Count_Operations_to_Obtain_Zero.java new file mode 100644 index 00000000..5ff2efce --- /dev/null +++ b/Java/Count_Operations_to_Obtain_Zero.java @@ -0,0 +1,24 @@ +/* + LEETCODE QUESTION : 2169. Count Operations to Obtain Zero + */ + +public class Count_Operations_to_Obtain_Zero { + + public int countOperations(int num1, int num2) { + return helper(num1, num2, 0); + + } + + private int helper(int num1, int num2, int count) { + + if (num1 == 0 || num2 == 0) { + return count; + } + if (num1 > num2) + return helper(num1 - num2, num2, count + 1); + else + return helper(num1, num2 - num1, count + 1); + + } + +} diff --git a/Java/Determine_Color_of_a_Chessboard_Square.java b/Java/Determine_Color_of_a_Chessboard_Square.java new file mode 100644 index 00000000..69bafd43 --- /dev/null +++ b/Java/Determine_Color_of_a_Chessboard_Square.java @@ -0,0 +1,19 @@ +/* + LEETCODE QUESTION : 1812. Determine Color of a Chessboard Square + + */ + +public class Determine_Color_of_a_Chessboard_Square { + + public boolean squareIsWhite(String coordinates) { + + int a = coordinates.charAt(0); + int b = coordinates.charAt(1); + if ((a + b) % 2 == 0) { + return false; + } else + return true; + + } + +} diff --git a/Java/Find_Greatest_Common_Divisor_of_Array.java b/Java/Find_Greatest_Common_Divisor_of_Array.java new file mode 100644 index 00000000..906d6eb4 --- /dev/null +++ b/Java/Find_Greatest_Common_Divisor_of_Array.java @@ -0,0 +1,61 @@ +/* + +LEETCODE QUESTION : 1979. Find Greatest Common Divisor of Array + +=> Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums. + The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers. + */ + +//The code +class Find_Greatest_Common_Divisor_of_Array { + public int findGCD(int[] nums) { + insertion(nums); + + int large = nums[nums.length - 1]; + int small = nums[0]; + + int rem = large % small; + + if (rem == 0) { + return small; + } else + return gcd(small, large); + + } + + static int gcd(int a, int b) { + int result = Math.min(a, b); // Find Minimum of a and b + while (result > 0) { + if (a % result == 0 && b % result == 0) { + break; + } + result--; + } + return result; // return gcd of a and b + } + + private void insertion(int arr[]) { + + for (int i = 0; i < arr.length - 1; i++) { + + for (int j = i + 1; j > 0; j--) { + + if (arr[j] < arr[j - 1]) { + // swap + swaparray(arr, j, j - 1); + } else { + break; + } + } + } + } + + static void swaparray(int[] arr, int first, int second) { + + int temp = arr[first]; + arr[first] = arr[second]; + arr[second] = temp; + + } + +} \ No newline at end of file diff --git a/Java/Linked List/Linked_List_Cycle.java b/Java/Linked List/Linked_List_Cycle.java new file mode 100644 index 00000000..8c657da0 --- /dev/null +++ b/Java/Linked List/Linked_List_Cycle.java @@ -0,0 +1,41 @@ +/* + LEETCODE QUESTION : 141. Linked List Cycle + + => There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, + pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. + Return true if there is a cycle in the linked list. Otherwise, return false. + + */ + +// * Definition for singly-linked list. +public class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + +public class Linked_List_Cycle { + public boolean hasCycle(ListNode head) { + ListNode fast = head; + ListNode slow = head; + + //for iteration + while(fast!=null && fast.next!=null){ + fast=fast.next.next; //iterate 2 times + slow = slow.next; //iterate 1 time + + if(slow== fast) + return true; + + } + return false; + + + } + +} diff --git a/Java/Linked List/Middle_of_the_Linked_List.java b/Java/Linked List/Middle_of_the_Linked_List.java new file mode 100644 index 00000000..530be29e --- /dev/null +++ b/Java/Linked List/Middle_of_the_Linked_List.java @@ -0,0 +1,43 @@ +/* + LEETCODE QUESTION : 876. Middle of the Linked List + => Given the head of a singly linked list, return the middle node of the linked list. + If there are two middle nodes, return the second middle node. + + + */ + +// * Definition for singly-linked list. +public class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + +public class Middle_of_the_Linked_List { + + //Add code from here in Leetcode. + public ListNode middleNode(ListNode head) { + + ListNode fast = head; + ListNode slow = head; + + while(fast!= null && fast.next != null){ + + slow=slow.next; + fast= fast.next.next; + //length=lenght+1; + + } + return slow; + + + + } + +} diff --git a/Java/Linked List/Reverse_Linked_List.java b/Java/Linked List/Reverse_Linked_List.java new file mode 100644 index 00000000..94f11d29 --- /dev/null +++ b/Java/Linked List/Reverse_Linked_List.java @@ -0,0 +1,48 @@ +/* + LEETCODE QUESTION : 206. Reverse Linked List + => Given the head of a singly linked list, reverse the list, and return the reversed list. + */ + + + // * Definition for singly-linked list. +public class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} + +public class Reverse_Linked_List { + + //Submit in leetcode from here :- + public ListNode reverseList(ListNode head) { + if (head == null) { + return head; + } + ListNode prev = null; + ListNode present = head; + ListNode nextnode = present.next; + + while (present != null) { + present.next = prev; + prev = present; + present = nextnode; + if (nextnode != null) { + nextnode = nextnode.next; + } + } + return prev; + + } + +}