[ADA] lab file
[ADA] lab file
22SCSE1011854
GALGOTIAS UVIVERSITY
LAB FILE
Linked List
//Shiv Sundar Singh 22SCSE1011854
class Node {
this.data = data;
this.next = null;
class LinkedList {
public LinkedList() {
this.head = null;
if (head == null) {
head = newNode;
} else {
current = current.next;
current.next = newNode;
if (head == null) {
System.out.println("List is empty");
return;
if (head.data == data) {
head = head.next;
return;
current = current.next;
if (current.next == null) {
return;
current.next = current.next.next;
}
//Shiv Sundar Singh
22SCSE1011854
if (head == null) {
System.out.println("List is empty");
return;
current = current.next;
System.out.println("null");
if (current.data == data) {
return true;
}
current = current.next;
return false;
// Inserting elements
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);
System.out.println("Linked List:");
list.display();
// Deleting an element
list.delete(20);
list.display();
}
//Shiv Sundar Singh
22SCSE1011854
Stack
//Shiv Sundar Singh 22SCSE1011854
// Node class
private T data;
this.data = data;
newNode.next = top; // Link the new node to the previous top node
public T pop() {
if (top == null) {
T data = top.data;
return data;
// Peek method to view the top element of the stack without removing it
public T peek() {
if (top == null) {
return top.data;
stack.push(10);
stack.push(20);
stack.push(30);
stack.pop();
//Shiv Sundar Singh
22SCSE1011854
stack.pop();
System.out.println("Is stack empty after popping all elements? " + stack.isEmpty()); // Should
print true
}
//Shiv Sundar Singh
22SCSE1011854
DivisibilityBy2 {
if (isDivisibleBy2(number)) {
} else {
}
//Shiv Sundar Singh
22SCSE1011854
KthBitFinder {
System.out.println("The " + k + "-th bit of " + number + " is: " + bit);
// Shift the number right by k positions and perform a bitwise AND with 1
}
//Shiv Sundar Singh
22SCSE1011854
if (hasConsecutiveOnes(number)) {
} else {
// Perform a bitwise AND between the number and the number shifted right by one
}
//Shiv Sundar Singh
22SCSE1011854
if (isPowerOfTwo(number)) {
} else {
// Check if the number is greater than 0 and only one bit is set in its binary representation
}
//Shiv Sundar Singh
22SCSE1011854
22SCSE1011854 import
java.util.Scanner;
int a = scanner.nextInt();
int b = scanner.nextInt();
swapUsingTemp(a, b);
swapUsingAdditionSubtraction(a, b);
swapUsingMultiplicationDivision(a, b);
swapUsingXOR(a, b);
scanner.close();
}
//Shiv Sundar Singh
22SCSE1011854
int temp = a;
a = b;
b = temp;
} else {
}
//Shiv Sundar Singh
22SCSE1011854
}
//Shiv Sundar Singh
22SCSE1011854
Find the kth bit by 3 different ways and toggle the bit.
import java.util.Scanner;
KthBitToggler {
int k = scanner.nextInt();
toggleKthBitMethod1(number, k);
toggleKthBitMethod2(number, k);
toggleKthBitMethod3(number, k);
scanner.close();
int mask = 1 << k; // Create a mask with the k-th bit set
//Shiv Sundar Singh
22SCSE1011854
int result = number ^ mask; // Toggle the k-th bit using XOR
int mask = 1 << k; // Create a mask with the k-th bit set
int result = number ^ mask; // Toggle the k-th bit using XOR
int result = number ^ (1 << k); // Toggle the k-th bit using XOR
}
//Shiv Sundar Singh
22SCSE1011854
Find out the student with 3 activities. All students assigned 2 activities
except one
public class StudentActivities { //Shiv Sundar Singh
int n = activities.length;
if (studentWithThreeActivities != -1) {
} else {
int[] activityCounts = new int[n + 1]; // Array to store activity counts for each student
activityCounts[activity]++;
if (activityCounts[i] == 3) {
studentWithThreeActivities = i;
//Shiv Sundar Singh
22SCSE1011854
break;
return studentWithThreeActivities;
}
//Shiv Sundar Singh
22SCSE1011854
MatrixAddition {
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix2 = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
// Perform addition
displayMatrix(result);
System.out.println();
}
//Shiv Sundar Singh
22SCSE1011854
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix2 = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
displayMatrix(result);
if (columns1 != rows2) {
return result;
System.out.println();
}
//Shiv Sundar Singh
22SCSE1011854
int[][] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
};
displayMatrixInSnakeOrder(matrix);
if (i % 2 == 1) {
} else {
}
//Shiv Sundar Singh
22SCSE1011854
displayBoundary(matrix);
}
// Display left column (excluding top and bottom elements, in reverse order)
for (int i = rows - 2; i > 0; i--) {
System.out.print(matrix[i][0] + " ");
}
}
}
//Shiv Sundar Singh
22SCSE1011854
Given two numbers l and r, find the maximum XOR value of the numbers
in between l and r.
public class MaximumXORInRange //Shiv Sundar Singh 22SCSE1011854
System.out.println("Maximum XOR value between " + l + " and " + r + " is: " + maxXOR);
int max = 0;
return max;
}
//Shiv Sundar Singh
22SCSE1011854
Marks Moderator: if you are getting marks less then upto 2 of next
multiple of 5, then your marks will be moderated to next multiple of 5. If
marks is less then 48, then you fail, if you got more then 95 will be given
100.
//Shiv Sundar Singh
MarksModerator {
// Moderation rules
return 0; // Fail
} else {
} else {
}
//Shiv Sundar Singh
22SCSE1011854
if (meetingPoint[0] != -1) {
} else {
public static int[] findMeetingPoint(int x1, int j1, int x2, int j2) {
x1 += j1;
x2 += j2;
break;
return meetingPoint;
}
//Shiv Sundar Singh
22SCSE1011854
Write a program to sort the array using Bubble sort and find out the best case
and worst-case scenario.
//Shiv Sundar Singh
BubbleSort {
System.out.println("Original Array:");
printArray(arr);
bubbleSort(arr);
System.out.println("\nSorted Array:");
printArray(arr);
int[] bestCaseArr = {11, 12, 22, 25, 34, 64, 90}; // Best-case scenario
int[] worstCaseArr = {90, 64, 34, 25, 22, 12, 11}; // Worst-case scenario
printArray(bestCaseArr);
printArray(worstCaseArr);
int n = arr.length;
arr[j + 1] = temp;
int n = arr.length;
int comparisons = 0;
comparisons++;
arr[j + 1] = temp;
return comparisons;
System.out.println();
}
//Shiv Sundar Singh
22SCSE1011854
Write a program to sort the array using Insertion sort and find out the best
case and worst-case scenario.
InsertionSort {
System.out.println("Original Array:");
printArray(arr);
insertionSort(arr);
System.out.println("\nSorted Array:");
printArray(arr);
int[] bestCaseArr = {11, 12, 22, 25, 34, 64, 90}; // Best-case scenario
int[] worstCaseArr = {90, 64, 34, 25, 22, 12, 11}; // Worst-case scenario
printArray(bestCaseArr);
printArray(worstCaseArr);
int n = arr.length;
//Shiv Sundar Singh
22SCSE1011854
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int n = arr.length;
int comparisons = 0;
int j = i - 1;
comparisons++;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
return comparisons;
//Shiv Sundar Singh
22SCSE1011854
System.out.println();
}
//Shiv Sundar Singh
22SCSE1011854
Write a program to sort the array using Selection sort and find out the
best case and worst-case scenario.
//Shiv Sundar Singh
SelectionSort {
System.out.println("Original Array:");
printArray(arr);
selectionSort(arr);
System.out.println("\nSorted Array:");
printArray(arr);
int[] bestCaseArr = {11, 12, 22, 25, 34, 64, 90}; // Best-case scenario
int[] worstCaseArr = {90, 64, 34, 25, 22, 12, 11}; // Worst-case scenario
printArray(bestCaseArr);
printArray(worstCaseArr);
int n = arr.length;
int minIndex = i;
//Shiv Sundar Singh
22SCSE1011854
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
int n = arr.length;
int comparisons = 0;
int minIndex = i;
comparisons++;
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
return comparisons;
}
//Shiv Sundar Singh
22SCSE1011854
System.out.println();
}
//Shiv Sundar Singh
22SCSE1011854
Write a program to sort the array using Merge sort and find out the best
case and worst-case scenario.
//Shiv Sundar Singh
MergeSort {
System.out.println("Original Array:");
printArray(arr);
System.out.println("\nSorted Array:");
printArray(arr);
int[] bestCaseArr = {11, 12, 22, 25, 34, 64, 90}; // Best-case scenario
int[] worstCaseArr = {90, 64, 34, 25, 22, 12, 11}; // Worst-case scenario
printArray(bestCaseArr);
printArray(worstCaseArr);
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int n1 = m - l + 1;
int n2 = r - m;
int i = 0, j = 0;
int k = l;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
int comparisons = 0;
if (l < r) {
int m = l + (r - l) / 2;
return comparisons;
int comparisons = 0;
int n1 = m - l + 1;
int n2 = r - m;
int i = 0, j = 0;
//Shiv Sundar Singh
22SCSE1011854
int k = l;
comparisons++;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
return comparisons;
System.out.println();
}
//Shiv Sundar Singh
22SCSE1011854
Write a program to sort the array using Quick sort and find out the best
case and worst-case scenario.
//Shiv Sundar Singh
QuickSort {
System.out.println("Original Array:");
printArray(arr);
System.out.println("\nSorted Array:");
printArray(arr);
int[] bestCaseArr = {11, 12, 22, 25, 34, 64, 90}; // Best-case scenario
int[] worstCaseArr = {90, 64, 34, 25, 22, 12, 11}; // Worst-case scenario
printArray(bestCaseArr);
printArray(worstCaseArr);
quickSort(arr, pi + 1, high);
int i = low - 1;
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
int comparisons = 0;
return comparisons;
int i = low - 1;
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
System.out.println(); }}
//Shiv Sundar Singh
22SCSE1011854
Write a program for Fibonacci series using recursive function to find the
n terms.
//Shiv Sundar Singh
Fibonacci {
if (n <= 1) {
return n;
} else {
}
//Shiv Sundar Singh
22SCSE1011854
MinMaxArray {
min = arr[i];
max = arr[i];
}
//Shiv Sundar Singh
22SCSE1011854
TowerOfHanoi {
public static void towerOfHanoi(int n, char source, char auxiliary, char destination) {
if (n == 1) {
return;
System.out.println("Move disk " + n + " from " + source + " to " + destination);
}
//Shiv Sundar Singh
22SCSE1011854
KthMinimum {
if (kthMin != Integer.MAX_VALUE) {
} else {
if (arr.length < k) {
}
//Shiv Sundar Singh
22SCSE1011854
Find all the occurrences of a pattern in a given text. Analyse the best- and
worst-case time complexity in successful and unsuccessful search
//Shiv Sundar Singh
PatternMatching {
findAllOccurrences(text, pattern);
int n = text.length();
int m = pattern.length();
while (i < n) {
if (pattern.charAt(j) == text.charAt(i)) {
i++;
j++;
if (j == m) {
j = lps[j - 1];
if (j != 0) {
j = lps[j - 1];
} else {
i++;
int m = pattern.length();
int len = 0;
int i = 1;
lps[0] = 0;
while (i < m) {
if (pattern.charAt(i) == pattern.charAt(len)) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
} else {
lps[i] = len;
i++;
return lps; }}
//Shiv Sundar Singh
22SCSE1011854
PatternReplacement {
int n = text.length();
int m = patternP.length();
while (i < n) {
if (patternP.charAt(j) == text.charAt(i)) {
i++;
j++;
}
//Shiv Sundar Singh
22SCSE1011854
if (j == m) {
result.append(patternQ);
j = lps[j - 1];
if (j != 0) {
j = lps[j - 1];
} else {
result.append(text.charAt(i));
i++;
while (i < n) {
result.append(text.charAt(i));
i++;
return result.toString();
int m = patternP.length();
int len = 0;
int i = 1;
lps[0] = 0;
while (i < m) {
//Shiv Sundar Singh
22SCSE1011854
if (patternP.charAt(i) == patternP.charAt(len)) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
} else {
lps[i] = len;
i++;
return lps;
}
//Shiv Sundar Singh
22SCSE1011854
int[] dimensions = {10, 30, 5, 60}; // Dimensions of matrices A1, A2, ..., An
dp[i][j] = -1;
if (i == j) {
//Shiv Sundar Singh
22SCSE1011854
return 0;
if (dp[i][j] != -1) {
return dp[i][j];
dp[i][j] = Integer.MAX_VALUE;
matrixChainOrder(dimensions, k + 1, j, dp) +
return dp[i][j];
}
//Shiv Sundar Singh
22SCSE1011854
Write a program to multiply two decimal integer numbers using divide and
conquer method.
if (n == 1) {
int m = n / 2;
// Divide
// Conquer
// Combine
}
//Shiv Sundar Singh
22SCSE1011854
Write a program to find the minimum sum of lengths between the nodes
of the undirected connected graph using two different methods.
//Shiv Sundar Singh 22SCSE1011854
// Function to find the minimum sum of lengths between the nodes of the undirected connected
graph
int n = graph.length;
// Initialize distances
// Floyd-Warshall Algorithm
}
//Shiv Sundar Singh
22SCSE1011854
if (dist[i][j] != Integer.MAX_VALUE) {
return minSum;
}
//Shiv Sundar Singh
22SCSE1011854
Problem: You are a delivery driver with a limited capacity truck. Your goal
is to maximize your earnings by delivering packages to various locations.
Each package has a weight and a profit associated with it. However, you
can only deliver packages whose total weight does not exceed your truck's
capacity. How can you choose which packages to deliver to maximize your
earnings?
Your truck can carry a maximum weight of 50 units. You have the
following packages available for delivery:
KnapsackProblem {
int n = weights.length;
if (weights[i - 1] <= j) {
} else {
return dp[n][capacity];
}
//Shiv Sundar Singh
22SCSE1011854
Example:
- Input:
Sequence 1: "ACGTAC"
Sequence 2: "TACGAC"
- Output: "ACAC"
import java.util.*;
String sequence1=sc.next();
String sequence2=sc.next();
int m = s1.length();
int t = s2.length();
} else {
int i = m, j = t;
lcs.append(s1.charAt(i - 1));
i--;
j--;
i--;
} else {
j--;
return lcs.reverse().toString();
}
//Shiv Sundar Singh
22SCSE1011854