Skip to content

Commit cb09bb0

Browse files
Merge pull request #52 from 4N1Z/main
Added leetcode questions
2 parents 93fac0b + dcf0167 commit cb09bb0

6 files changed

+236
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
LEETCODE QUESTION : 2169. Count Operations to Obtain Zero
3+
*/
4+
5+
public class Count_Operations_to_Obtain_Zero {
6+
7+
public int countOperations(int num1, int num2) {
8+
return helper(num1, num2, 0);
9+
10+
}
11+
12+
private int helper(int num1, int num2, int count) {
13+
14+
if (num1 == 0 || num2 == 0) {
15+
return count;
16+
}
17+
if (num1 > num2)
18+
return helper(num1 - num2, num2, count + 1);
19+
else
20+
return helper(num1, num2 - num1, count + 1);
21+
22+
}
23+
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
LEETCODE QUESTION : 1812. Determine Color of a Chessboard Square
3+
4+
*/
5+
6+
public class Determine_Color_of_a_Chessboard_Square {
7+
8+
public boolean squareIsWhite(String coordinates) {
9+
10+
int a = coordinates.charAt(0);
11+
int b = coordinates.charAt(1);
12+
if ((a + b) % 2 == 0) {
13+
return false;
14+
} else
15+
return true;
16+
17+
}
18+
19+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
3+
LEETCODE QUESTION : 1979. Find Greatest Common Divisor of Array
4+
5+
=> Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums.
6+
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
7+
*/
8+
9+
//The code
10+
class Find_Greatest_Common_Divisor_of_Array {
11+
public int findGCD(int[] nums) {
12+
insertion(nums);
13+
14+
int large = nums[nums.length - 1];
15+
int small = nums[0];
16+
17+
int rem = large % small;
18+
19+
if (rem == 0) {
20+
return small;
21+
} else
22+
return gcd(small, large);
23+
24+
}
25+
26+
static int gcd(int a, int b) {
27+
int result = Math.min(a, b); // Find Minimum of a and b
28+
while (result > 0) {
29+
if (a % result == 0 && b % result == 0) {
30+
break;
31+
}
32+
result--;
33+
}
34+
return result; // return gcd of a and b
35+
}
36+
37+
private void insertion(int arr[]) {
38+
39+
for (int i = 0; i < arr.length - 1; i++) {
40+
41+
for (int j = i + 1; j > 0; j--) {
42+
43+
if (arr[j] < arr[j - 1]) {
44+
// swap
45+
swaparray(arr, j, j - 1);
46+
} else {
47+
break;
48+
}
49+
}
50+
}
51+
}
52+
53+
static void swaparray(int[] arr, int first, int second) {
54+
55+
int temp = arr[first];
56+
arr[first] = arr[second];
57+
arr[second] = temp;
58+
59+
}
60+
61+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
LEETCODE QUESTION : 141. Linked List Cycle
3+
4+
=> 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,
5+
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.
6+
Return true if there is a cycle in the linked list. Otherwise, return false.
7+
8+
*/
9+
10+
// * Definition for singly-linked list.
11+
public class ListNode {
12+
int val;
13+
ListNode next;
14+
15+
ListNode() {
16+
}
17+
18+
ListNode(int val) {
19+
this.val = val;
20+
}
21+
22+
public class Linked_List_Cycle {
23+
public boolean hasCycle(ListNode head) {
24+
ListNode fast = head;
25+
ListNode slow = head;
26+
27+
//for iteration
28+
while(fast!=null && fast.next!=null){
29+
fast=fast.next.next; //iterate 2 times
30+
slow = slow.next; //iterate 1 time
31+
32+
if(slow== fast)
33+
return true;
34+
35+
}
36+
return false;
37+
38+
39+
}
40+
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
LEETCODE QUESTION : 876. Middle of the Linked List
3+
=> Given the head of a singly linked list, return the middle node of the linked list.
4+
If there are two middle nodes, return the second middle node.
5+
6+
7+
*/
8+
9+
// * Definition for singly-linked list.
10+
public class ListNode {
11+
int val;
12+
ListNode next;
13+
14+
ListNode() {
15+
}
16+
17+
ListNode(int val) {
18+
this.val = val;
19+
}
20+
21+
22+
public class Middle_of_the_Linked_List {
23+
24+
//Add code from here in Leetcode.
25+
public ListNode middleNode(ListNode head) {
26+
27+
ListNode fast = head;
28+
ListNode slow = head;
29+
30+
while(fast!= null && fast.next != null){
31+
32+
slow=slow.next;
33+
fast= fast.next.next;
34+
//length=lenght+1;
35+
36+
}
37+
return slow;
38+
39+
40+
41+
}
42+
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
LEETCODE QUESTION : 206. Reverse Linked List
3+
=> Given the head of a singly linked list, reverse the list, and return the reversed list.
4+
*/
5+
6+
7+
// * Definition for singly-linked list.
8+
public class ListNode {
9+
int val;
10+
ListNode next;
11+
12+
ListNode() {
13+
}
14+
15+
ListNode(int val) {
16+
this.val = val;
17+
}
18+
19+
ListNode(int val, ListNode next) {
20+
this.val = val;
21+
this.next = next;
22+
}
23+
}
24+
25+
public class Reverse_Linked_List {
26+
27+
//Submit in leetcode from here :-
28+
public ListNode reverseList(ListNode head) {
29+
if (head == null) {
30+
return head;
31+
}
32+
ListNode prev = null;
33+
ListNode present = head;
34+
ListNode nextnode = present.next;
35+
36+
while (present != null) {
37+
present.next = prev;
38+
prev = present;
39+
present = nextnode;
40+
if (nextnode != null) {
41+
nextnode = nextnode.next;
42+
}
43+
}
44+
return prev;
45+
46+
}
47+
48+
}

0 commit comments

Comments
 (0)