AAPS Lab File Code (1)
AAPS Lab File Code (1)
ADM NO.-22SCSE1010944
SECTION-BTECH CSE CORE -19
SUBMITTED TO: ANAND SIR
DATE:24.04.2025
2. Equilibrium Index
public class EquilibriumIndex {
public static int findEquilibriumIndex(int[] arr) {
int total = 0, leftSum = 0;
for (int num : arr) total += num;
int prefix = 0;
for (int i = 0; i < arr.length - 1; i++) {
prefix += arr[i];
if (prefix == total - prefix)
return true;
}
return false;
}
}
10. Generate all permutations of a given string. Write its algorithm, program. Find its time and
space complexities.\
11.Find two numbers in a sorted array that add up to a target. Write its algorithm, program. Find
its time and space complexities. Explain with suitable example.
int x = nums1.length;
int y = nums2.length;
int low = 0, high = x;
if (count < k) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
count = 0;
for (int num : nums) {
if (num == candidate) {
count++;
}
}
17. Calculate how much water can be trapped between the bars of a histogram. Write its
algorithm, program. Find its time and space complexities. Explain with suitable example
public class TrappingRainWater {
public static int trap(int[] height) {
int left = 0, right = height.length - 1;
int leftMax = 0, rightMax = 0;
int water = 0;
Algorithm:
1. Use a Trie (binary tree) to insert all numbers in binary.
2. For each number, try to find a number in Trie that gives the maximum XOR with it.
Time Complexity:
Java Code:
java
CopyEdit
class TrieNode {
TrieNode[] children = new TrieNode[2];
}
Example:
Algorithm:
● Time: O(n)
● Space: O(1)
Java Code:
public class MaxProductSubarray {
public static int maxProduct(int[] nums) {
int max = nums[0], min = nums[0], result = nums[0];
return result;
}
Example:
● Output: 6 (2 * 3)
Algorithm:
● Time: O(n)
● Space: O(1)
Java Code:
public class UniqueDigits {
public static int countNumbersWithUniqueDigits(int n) {
if (n == 0) return 1;
int result = 10, uniqueDigits = 9, available = 9;
return result;
}
Q21. How to count the number of 1s in the binary representation of numbers from 0 to n.
Write its algorithm, program. Find its time and space complexities. Explain with suitable
example.
import java.util.Arrays;
}
return countBits;
}
int n = 5;
}
Q22. How to check if a number is a power of two using bit manipulation. Write its algorithm,
program. Find its time and space complexities. Explain with suitable example.
}
}
}
Q23. How to find the maximum XOR of two numbers in an array. Write its algorithm,
program. Find its time and space complexities. Explain with suitable example.
import java.util.HashSet;
mask |= (1 << i); // Update the mask to include the i-th bit
}
if (prefixes.contains(prefix ^ candidate)) {
maxXOR = candidate;
break;
}
}
}
return maxXOR;
}
public static void main(String[] args) {
}
Q24. Explain the concept of bit manipulation and its advantages in algorithm design.
Bit manipulation involves performing operations directly on the binary representation of numbers
using bitwise operators. These operators include:
OR ` `
Example :
1. Efficiency:
○ Bitwise operations are faster than arithmetic or logical operations (single CPU
instruction).
○ Useful in time-critical applications like cryptography, networking, or embedded
systems.
2. Memory Optimization:
○ Compactly store and manipulate data (e.g., flags, masks, sets) using single
integers.
○ Ideal for fixed-width data like 32-bit or 64-bit numbers.
3. Simpler Logic for Certain Problems:
○ Toggle a bit: x ^ (1 << i)
○ Check if power of two: x & (x - 1) == 0
○ Count set bits: Brian Kernighan’s Algorithm
4. Real-World Applications:
○ Finding subsets using bitmasking.
○ Swapping values without temporary variable.
○ Optimization in DSA problems like max XOR, set operations, etc.
Example :
boolean isPowerOfTwo(int n) {
Why it works:
Q25. Solve the problem of finding the next greater element for each element in an array.
Write its algorithm, program. Find its time and space complexities. Explain with suitable
example.
import java.util.Stack;
import java.util.Arrays;
int n = nums.length;
stack.pop();
}
stack.push(nums[i]);
}
return res;
}
}
Q26. Remove the n-th node from the end of a singly linked list. Write its algorithm,
program. Find its time and space complexities. Explain with suitable example.
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
dummy.next = head;
fast = fast.next;
}
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
head = head.next;
}
System.out.println();
}
int n = 2;
printList(head);
}
}
// 27. Intersection of two linked lists
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
class LinkedListIntersection {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA, b = headB;
while (a != b) {
a = (a == null) ? headB : a.next;
b = (b == null) ? headA : b.next;
}
return a;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; next = null; }
}