Skip to content

Added leetcode questions #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Java/Count_Operations_to_Obtain_Zero.java
Original file line number Diff line number Diff line change
@@ -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);

}

}
19 changes: 19 additions & 0 deletions Java/Determine_Color_of_a_Chessboard_Square.java
Original file line number Diff line number Diff line change
@@ -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;

}

}
61 changes: 61 additions & 0 deletions Java/Find_Greatest_Common_Divisor_of_Array.java
Original file line number Diff line number Diff line change
@@ -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;

}

}
41 changes: 41 additions & 0 deletions Java/Linked List/Linked_List_Cycle.java
Original file line number Diff line number Diff line change
@@ -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;


}

}
43 changes: 43 additions & 0 deletions Java/Linked List/Middle_of_the_Linked_List.java
Original file line number Diff line number Diff line change
@@ -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;



}

}
48 changes: 48 additions & 0 deletions Java/Linked List/Reverse_Linked_List.java
Original file line number Diff line number Diff line change
@@ -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;

}

}