AAPS_LAB FileFormat
AAPS_LAB FileFormat
Lab File
For
BACHELOR OF
UTTAR PRADESH
Semester : VI
1|P a g e
PROBLEM NO. : 1
PROGRAM :
DESCRIPTION:
Find the sum of the numbers in the range [1, N] using the formula N * (N+1)/2. Now find
the sum of all the elements in the array and subtract it from the sum of the first N natural
numbers. This will give the value of the missing element.
SOURCE CODE:
import java.util.Arrays;
class prb_solv {
OUTPUT:
2|P a g e
PROBLEM NO. : 2
PROGRAM :
DESCRIPTION:
A simple method for this is to simply take the log of the number on base 2 and if you get
an integer then the number is the power of 2
SOURCE CODE:
class prb_solv {
// Driver code
public static void main(String[] args) {
int num = 19;
int nextPower = nextPowerOf2(num);
System.out.println("Next power of 2 greater than or equal to " +
num + " is: " + nextPower);
int a = 5;
int b = 14;
int diff = bitDifference(a, b);
System.out.println("Number of differing bits between " + a + "
and " + b + " is: " + diff);
3|P a g e
}
}
OUTPUT:
4|P a g e
PROBLEM NO. : 3
PROGRAM :
DESCRIPTION:
SOURCE CODE:
import java.util.HashSet;
import java.util.Set;
class prb_solv {
public static boolean hasDuplicates(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int num : nums) {
if (!seen.add(num)) {
return true;
}
}
return false;
}
while (n > 0) {
if ((n & 1) == 1) {
currentCount++;
maxCount = Math.max(maxCount, currentCount);
} else {
currentCount = 0;
}
n >>= 1;
}
return maxCount;
}
5|P a g e
System.out.println("Array has duplicates: " +
hasDuplicates(arr));
int num = 29;
System.out.println("Longest consecutive 1's in " + num + ": " +
longestConsecutiveOnes(num));
}
}
OUTPUT:
6|P a g e
PROBLEM NO. : 4
PROGRAM :
SOURCE CODE:
import java.util.HashSet;
import java.util.Set;
class prb_solv {
prefixSet.add(0);
return maxXOR;
}
public static void main(String[] args) {
int[] uniqueArray = {2, 3, 5, 4, 5, 3, 4};
System.out.println("Unique element in the array: " +
findUnique(uniqueArray));
7|P a g e
}
}
OUTPUT:
8|P a g e
PROBLEM NO. : 5
AIM : Find the Kth largest elements in an array, Rearrange an array in maximum
minimum form using Two Pointer Technique
PROGRAM :
SOURCE CODE:
import java.util.PriorityQueue;
import java.util.Arrays;
class prb_solv {
public static int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int num : nums) {
minHeap.add(num);
if (minHeap.size() > k) {
minHeap.poll();
}
}
return minHeap.peek();
}
public static void rearrangeMaxMin(int[] nums) {
int n = nums.length;
int[] result = new int[n];
int left = 0;
int right = n - 1;
boolean placeMax = true;
9|P a g e
System.out.println("Original array: " + Arrays.toString(arr2));
rearrangeMaxMin(arr2);
System.out.println("Rearranged array: " + Arrays.toString(arr2));
}
}
OUTPUT:
10 | P a g e
PROBLEM NO. : 6
AIM : Move all zeroes to end of array, Rearrange array such that even positioned are
greater than odd
PROGRAM :
SOURCE CODE:
import java.util.Arrays;
class prb_solv {
public static void moveZeroesToEnd(int[] nums) {
int n = nums.length;
int index = 0;
int left = 0;
int right = (n % 2 == 0) ? n / 2 : n / 2 + 1;
11 | P a g e
System.out.println("Array after moving zeroes to end: " +
Arrays.toString(arr1));
OUTPUT:
12 | P a g e
PROBLEM NO. : 7
AIM : Find sub-array with given sum, Find the smallest missing number
PROGRAM :
SOURCE CODE:
import java.util.HashSet;
class ArrayManipulation {
public static int[] findSubArrayWithGivenSum(int[] nums, int
targetSum) {
int left = 0;
int currentSum = 0;
if (currentSum == targetSum) {
int[] subArray = new int[right - left + 1];
System.arraycopy(nums, left, subArray, 0, right - left +
1);
return subArray;
}
}
return new int[0];
}
public static int findSmallestMissingNumber(int[] nums) {
HashSet<Integer> set = new HashSet<>();
int smallestMissing = 1;
while (set.contains(smallestMissing)) {
smallestMissing++;
}
13 | P a g e
return smallestMissing;
}
public static void main(String[] args) {
int[] arr1 = {1, 4, 20, 3, 10, 5};
int sum = 33;
int[] subArray = findSubArrayWithGivenSum(arr1, sum);
if (subArray.length > 0) {
System.out.println("Sub-array with sum " + sum + ": " +
java.util.Arrays.toString(subArray));
} else {
System.out.println("No sub-array found with sum " + sum);
}
int[] arr2 = {1, 3, 6, 4, 1, 2};
int missingNumber = findSmallestMissingNumber(arr2);
System.out.println("Smallest missing positive number: " +
missingNumber);
}
}
OUTPUT:
14 | P a g e
PROBLEM NO. : 8
AIM : Merge two sorted arrays with O(1) extra space, Search an element in a sorted and
rotated array
PROGRAM :
SOURCE CODE:
class ArrayManipulation {
if (nums[mid] == target) {
return mid;
}
if (nums[left] <= nums[mid]) {
if (target >= nums[left] && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
15 | P a g e
} else {
right = mid - 1;
}
}
}
return -1;
}
public static void main(String[] args) {
int[] nums1 = {1, 3, 5, 0, 0, 0};
int m = 3;
int[] nums2 = {2, 4, 6};
int n = 3;
mergeSortedArrays(nums1, m, nums2, n);
System.out.println("Merged sorted array: " +
java.util.Arrays.toString(nums1));
int[] rotatedArray = {4, 5, 6, 7, 0, 1, 2};
int target = 0;
int index = searchRotatedSortedArray(rotatedArray, target);
System.out.println("Index of target " + target + " in rotated
array: " + index);
}
}
OUTPUT:
16 | P a g e
PROBLEM NO. : 9
PROGRAM :
Sorting an Array of Dates in C/C++
To sort an array of dates in C/C++, you can define a custom comparison function for the
date data type and use it with a sorting algorithm like std::sort
SOURCE CODE:
#include <iostream>
#include <vector>
#include <algorithm>
struct Date {
int day, month, year;
bool operator<(const Date& other) const {
if (year != other.year) return year < other.year;
if (month != other.month) return month < other.month;
return day < other.day;
}
};
int main() {
vector<Date> dates = {{1, 12, 2021}, {31, 10, 2020}, {15, 7, 2022},
{1, 1, 2021}};
sort(dates.begin(), dates.end());
for (const auto& date : dates) {
cout << date.day << "/" << date.month << "/" << date.year <<
endl;
}
return 0;
}
17 | P a g e
OUTPUT:
18 | P a g e
PROBLEM NO. : 10
AIM : Bucket Sort To Sort an Array with Negative Numbers, K-Way Merge Sort
PROGRAM :
SOURCE CODE:
import java.util.*;
int index = 0;
for (List<Integer> bucket : buckets) {
Collections.sort(bucket);
for (int num : bucket) {
nums[index++] = num;
}
}
}
19 | P a g e
PriorityQueue<Element> minHeap = new
PriorityQueue<>(Comparator.comparingInt(a -> a.value));
if (arrays[minElement.arrayIndex].length > 0) {
minHeap.offer(new Element(arrays[minElement.arrayIndex]
[0], minElement.arrayIndex));
arrays[minElement.arrayIndex] =
Arrays.copyOfRange(arrays[minElement.arrayIndex], 1,
arrays[minElement.arrayIndex].length);
}
}
return
sortedArray.stream().mapToInt(Integer::intValue).toArray();
}
OUTPUT:
20 | P a g e
PROBLEM NO. : 11
AIM : Sum of natural numbers using recursion, Decimal to binary number using
recursion
PROGRAM :
SOURCE CODE:
public class prob_solv {
public static int sumOfNaturalNumbers(int n) {
if (n == 0) {
return 0;
} else {
return n + sumOfNaturalNumbers(n - 1);
}
}
OUTPUT:
21 | P a g e
PROBLEM NO. : 12
AIM : Print reverse of a string using recursion, Program for length of a string using
recursion
PROGRAM :
SOURCE CODE:
public class prob_solv {
public static String reverseString(String str) {
if (str.isEmpty()) {
return str;
} else {
return reverseString(str.substring(1)) + str.charAt(0);
}
}
OUTPUT:
22 | P a g e
PROBLEM NO. : 13
AIM : Sort the Queue using Recursion, Reversing a queue using recursion
PROGRAM :
SOURCE CODE:
Sorting a Queue using Recursion
import java.util.LinkedList;
import java.util.Queue;
23 | P a g e
queue.add(1);
queue.add(5);
queue.add(9);
OUTPUT:
24 | P a g e
queue.add(5);
OUTPUT:
25 | P a g e
PROBLEM NO. : 14
PROGRAM :
SOURCE CODE:
Length of Longest Palindromic Substring using Recursion
public class PalindromeAndStringToInt {
26 | P a g e
OUTPUT:
27 | P a g e
Convert a String to an Integer using Recursion
CODE:
public class PalindromeAndStringToInt {
28 | P a g e
int length = longestPalindromicSubstring(palindromeStr);
System.out.println("Length of longest palindromic substring: " +
length);
OUTPUT:
29 | P a g e
PROBLEM NO. : 15
PROGRAM :
SOURCE CODE:
DFS Traversal of a Tree
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
left = null;
right = null;
}
}
30 | P a g e
OUTPUT:
31 | P a g e
}
}
OUTPUT:
32 | P a g e