Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.class
32 changes: 31 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
// Initially had an issue with the mid calculation and misplaced braces,
// but after fixing those, the logic worked correctly.



// I used an iterative Binary Search approach. In each iteration, I compute
// the mid index and compare the middle element with the target.
// - If arr[mid] equals the target, I return mid.
// - If the target is smaller, I move the right pointer to mid - 1.
// - If the target is larger, I move the left pointer to mid + 1.
// I continue this until left > right. If no match is found, I return -1.
class BinarySearch {
// Returns index of x if it is present in arr[l.. r], else return -1
int binarySearch(int arr[], int l, int r, int x)
{
//Write your code here
while(l <= r){
int mid = l +(r-1)/2;

if (arr[mid]==x){
return mid;
//Write your code here
}

if (arr[mid] > x){
r = mid -1;
}
else{
l=mid + 1;
}
}
return -1;
}

// Driver method to test above
public static void main(String args[])
Expand Down
35 changes: 32 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Time Complexity : O(log n)
// Space Complexity : O(1) for iterative version
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
// Initially had issues with mid calculation and extra braces,
// but corrected them to ensure clean and accurate execution.


// I used the standard iterative Binary Search algorithm. In each step,
// I calculate the mid index and compare it with the target value.
// If the target is smaller, I move the right boundary to mid-1.
// If the target is larger, I move the left boundary to mid+1.
// If arr[mid] equals the target, I return the mid index.
// If the loop ends with no match, I return -1.
class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -7,20 +21,35 @@ class QuickSort
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
//Your code here
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;//Your code here
}

int partition(int arr[], int low, int high)
{
//Write code here for Partition and Swap
int pivot = arr[high];
int i = (low-1);
for (int j = low; j < high; j++){
if(arr[j] <= pivot){
i++;
swap(arr,i,j);
}//Write code here for Partition and Swap
}
swap(arr, i+1, high);
return i + 1;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void sort(int arr[], int low, int high)
{
// Recursively sort elements before
if(low < high){
int pi = partition(arr,low, high);
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}// Recursively sort elements before
// partition and after partition
}

Expand Down
31 changes: 27 additions & 4 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
// No major issues. The main challenge was remembering that the fast pointer
// moves two steps at a time while the slow pointer moves one step. This ensures
// the slow pointer reaches the middle when fast reaches the end.


// I used the two-pointer (slow and fast) technique. The fast pointer moves
// two nodes at a time, while the slow pointer moves one node at a time.
// When the fast pointer reaches the end of the list (or becomes null),
// the slow pointer will be pointing to the middle element of the list.
// This approach allows us to find the middle in a single traversal.
class LinkedList
{
Node head; // head of linked list
Expand All @@ -18,13 +32,22 @@ class Node
//Complete this function
void printMiddle()
{
//Write your code here
if (head == null){
System.out.println("The last is empty");
return;}
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;//Write your code here
//Implement using Fast and slow pointers
}

System.out.println("Middle element is: " + slow.data);
}
public void push(int new_data)
{
Node new_node = new Node(new_data);

Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
Expand All @@ -50,4 +73,4 @@ public static void main(String [] args)
llist.printMiddle();
}
}
}
}
57 changes: 54 additions & 3 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,69 @@
// Time Complexity : O(n log n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
// No major issues. The tricky part was correctly dividing the array,
// creating temporary subarrays, and ensuring all remaining elements
// from each subarray were copied back into the main array.



// I used the classic Merge Sort algorithm, which divides the array
// into two halves recursively until each subarray has one element.
// Then the merge() function is used to combine the two sorted halves
// by comparing elements and placing them in the correct order.
// The merge step uses temporary arrays to store left and right halves.
// Finally, the sorted values are copied back into the original array.
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
//Your code here
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] =arr[l + i];

for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 +j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2){
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}else{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1){
arr[k] = L[i];
i++;
k++;
}while(j < n2){
arr[k] = R[j];
j++;
k++;
}//Your code here
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
//Write your code here
if(l < r){
int m =(l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}//Write your code here
//Call mergeSort from here
}

Expand All @@ -39,4 +90,4 @@ public static void main(String args[])
System.out.println("\nSorted array");
printArray(arr);
}
}
}
51 changes: 48 additions & 3 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,65 @@
// Time Complexity : O(n log n) average case, O(n^2) worst case
// Space Complexity : O(log n) because of the manual stack
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
// Understanding the stack-based iterative approach was the main challenge.
// Instead of recursive calls, I used an integer stack to store subarray
// boundaries and process them iteratively.



// I implemented Iterative QuickSort using a stack to simulate the recursion.
// The algorithm pushes left and right subarray boundaries onto the stack.
// For each popping, it calls partition() to place the pivot in the correct spot.
// Then it pushes the boundaries of the left and right subarrays back onto the stack.
// The process continues until the stack is empty.
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
if (i == j) return;
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
//Try swapping without extra variable
}

/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
{
//Compare elements and swap.
int pivot = arr[h];
int i = (l - 1);
for (int j = l; j <= h - 1; j++){
if(arr[j] <= pivot){
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, h);
return (i + 1);
//Compare elements and swap.
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
{
//Try using Stack Data Structure to remove recursion.
int stack[] = new int[h -l + 1];
int top = -1;
stack[++top] = l;
stack[++top] = h;
while (top >= 0){
h = stack[top--];
l =stack[top--];
int p = partition(arr, l , h);
if (p - 1 > l){
stack[++top] = l;
stack[++top] = p-1;
}
if(p + 1 < h){
stack[++top] = p + 1;
stack[++top] = h;
}
}//Try using Stack Data Structure to remove recursion.
}

// A utility function to print contents of arr
Expand All @@ -33,4 +78,4 @@ public static void main(String args[])
ob.QuickSort(arr, 0, arr.length - 1);
ob.printArr(arr, arr.length);
}
}
}