0% found this document useful (0 votes)
4 views

Lab 04

Uploaded by

abhi786ggjj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 04

Uploaded by

abhi786ggjj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB_04

BY--> 221B310 (B9)

1.) PROBLEM NO. 01-->


import java.util.Arrays;

public class MinAbsoluteDifference {

public static void findMinAbsoluteDifferencePair(int[] arr) {

if (arr.length < 2) {

System.out.println("Array should have at least two elements.");

return;

// Sort the array

Arrays.sort(arr);

// Initialize minimum difference and corresponding pair

int minDiff = Integer.MAX_VALUE;

int x = -1, y = -1;

// Traverse the sorted array and find the minimum absolute difference

for (int i = 0; i < arr.length - 1; i++) {

int currentDiff = arr[i + 1] - arr[i];

if (currentDiff < minDiff) {

minDiff = currentDiff;
x = arr[i];

y = arr[i + 1];

System.out.println("Pair with minimum absolute difference: " + x + " and " + y);

public static void main(String[] args) {

int[] S = {4, 15, 8, 1, 19, 0, 12};

findMinAbsoluteDifferencePair(S);

OUTPUT-->

2.) PROBLEM NO. 02-->


import java.util.HashMap;

public class FindPairWithSum {

// Function to find a pair with the given sum and return the indices

public static String findPairWithSum(int[] A1, int[] A2, int x) {


HashMap<Integer, Integer> complementIndices = new HashMap<>();

// Traverse A1 and store complement indices in the hash set

for (int i = 0; i < A1.length; i++) {

int complement = x - A1[i];

complementIndices.put(complement, i);

// Traverse A2 and check if the complement exists in the hash set

for (int i = 0; i < A2.length; i++) {

if (complementIndices.containsKey(A2[i])) {

int indexA1 = complementIndices.get(A2[i]);

int indexA2 = i;

return "Yes with i1=" + indexA1 + " and i2=" + indexA2;

return "No";

public static void main(String[] args) {

int[] A1 = {4, 5, 8, 1, 3, 9, 0, 2};

int[] A2 = {2, 3, 35, 32, 12, 9, 2};

int x1 = 41;

int x2 = 25;

System.out.println(findPairWithSum(A1, A2, x1)); // Output: Yes with i1=5 and i2=3

System.out.println(findPairWithSum(A1, A2, x2)); // Output: No

}
OUTPUT-->

3.) PROBLEM NO. 03


public class MaxSegmentSum {

// Function to find the maximum segment sum and corresponding indices

public static void findMaxSegmentSum(int[] A) {

int n = A.length;

if (n == 0) {

System.out.println("Empty array");

return;

int maxSum = A[0];

int currentSum = A[0];

int start = 0;

int end = 0;

int tempStart = 0;

for (int i = 1; i < n; i++) {

if (currentSum < 0) {

currentSum = A[i];
tempStart = i;

} else {

currentSum += A[i];

if (currentSum > maxSum) {

maxSum = currentSum;

start = tempStart;

end = i;

System.out.println("The maximum segment sum is: " + maxSum);

System.out.println("The corresponding value for i and j are: " + start + " and " + end);

public static void main(String[] args) {

int[] A = {4, -5, 8, -1, 3, -4, 2, 0};

findMaxSegmentSum(A);

OUTPUT-->

You might also like