Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

LAB EXERCISE 4

Problem 4.1:

Objective:-Given an array S of unsorted elements. Design an algorithm and


implement that to find a pair x, y such that x≠y from S that minimizes |x-y|.The worst
case running time of the algorithm should be O (nlgn).

Instruction: - Solve the above problem with the help of following example

Given Array is

S = {4, 15, 8, 1, 19, 0, 12}, the output should be 0 and 1.

Answer:

Algorithm:

function find_min_pair (S):


n = length of S
sort S using any O (nlgn) sorting algorithm
min_diff = infinity
x = -1
y = -1
for i from 0 to n-2:
diff = S [i+1] - S [i]
if diff < min_diff:
min_diff = diff
x = S [i]
y = S [i+1]
return x and y

Program implementation in c++ :

#include <iostream>
#include <algorithm>
using namespace std;
void find_min_pair(int S[], int n)
{
sort(S, S + n);
int min_diff = INT_MAX;
int x = -1, y = -1;
for (int i = 0; i < n - 1; i++)
{
int diff = S[i + 1] - S[i];
if (diff < min_diff)
{
min_diff = diff;
x = S[i];
y = S[i + 1];
}
}
cout << "The pair with the minimum difference is (" << x << ", "
<< y << ")\n";
}
int main()
{
int S[] = {4, 15, 8, 1, 19, 0, 12};
int n = sizeof(S) / sizeof(S[0]);
find_min_pair(S, n);
return 0;
}

Problem 4.2:

Objective:-Given two arrays A1 , A2 of size n and a number x, design an algorithm to


find whether there exists a pair of elements one from A1 and other from A2 whose sum
is equal to x. Also find the indices of those elements

Instruction: - Solve the above problem with the help of following example

Given Arrays are

A1={4,5,8,1,3,9,0,2} and A2 ={2,3,35,32,12,9,2} and x = 41

The output should be yes with i1=5 and i2=3.

For x=25, the output should be no.

Answer:

Algorithm:

1. Sort array A1 in ascending order.


2. Initialize two pointers, i and j, to the start of arrays A1
and A2, respectively.
3. While i < size(A1) and j < size(A2):
a. Calculate temp = x - A2[j].
b. Search for temp in A1 using binary search.
c. If found:
- Print "Yes with i1=" + i + " and i2=" + j.
- Return.
d. Otherwise:
- If A2[j] < temp, increment j.
- Otherwise, increment i.
4. If no such pair is found, print "No".

Implementation in c++:

#include <bits/stdc++.h>
using namespace std;
int binarySearch(int a[], int l, int r, int x)
{
while (l <= r)
{
int m = l + (r - l) / 2;
if (a[m] == x)
return m;
if (a[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
void findPairsOptimized(int A[], int B[], int n, int K)
{
sort(A, A + n); // Sort array A
for (int i = 0; i < n; i++)
{
int temp = K - B[i];
if (binarySearch(A, 0, n - 1, temp) != -1)
{
cout << "Yes with i1=" << i << " and i2=" << binarySearch(A, 0, n - 1,
temp);
cout << endl;
return;
}
}
cout << "No" << endl;
}
int main()
{
int A[] = {4, 5, 8, 1, 3, 9, 0, 2};
int B[] = {2, 3, 35, 32, 12, 9, 2};
int n = sizeof(A) / sizeof(A[0]);
int x = 41;
findPairsOptimized(A, B, n, x);
return 0;
}
Problem 4.3:

Objective:-Develop an algorithm to solve the maximum segment sum problem.

Instruction: - Solve the above problem with the help of following example

Let A [0,…, n-1] be a given array of real numbers (positive and negative).For given
0<=i<=n-1 and 0<=j<=n-1,segment sum is defined as follows:

Segment sum (i, j) =A[i] +A [i+1] +…A[j]. if i<=j.

The problem is to find the maximum value of segment sum over all i and j. Also find
the value of i and j for which segment sum is maximum

Given Array is A= {4,-5, 8,-1, 3,-4.2, 0, 2};

The maximum segment sum for this array is 10.

The corresponding value for i and j are 2 and 4 respectively.

Answer:

Algorithm:

1. Initialize variables:
- maxSum = INT_MIN (to store the maximum segment sum)
- currSum = 0 (to track the current segment sum)
- startIdx = 0 (to store the starting index of the maximum
segment)
- endIdx = 0 (to store the ending index of the maximum
segment)
2. Iterate over the array A from left to right:
a. Update currSum:
- If currSum < 0, set currSum = A[i].
- Otherwise, add A[i] to currSum.
b. Update maxSum:
- If currSum > maxSum, set maxSum = currSum and
update startIdx and endIdx.
3. Print maxSum, startIdx, and endIdx.
4. The maximum segment sum is A[startIdx] + A[startIdx+1] + ... +
A[endIdx].

Implementation in c++:

#include <iostream>
#include <vector>
using namespace std;
pair<int, pair<int, int>> maxSegmentSum(vector<double>& A)
{
int n = A.size();
double maxSum = A[0];
int startIdx = 0, endIdx = 0;
double currSum = A[0];
int currStart = 0;
for (int i = 1; i < n; ++i)
{
if (currSum < 0)
{
currSum = A[i];
currStart = i;
}
else
{
currSum += A[i];
}
if (currSum > maxSum)
{
maxSum = currSum;
startIdx = currStart;
endIdx = i;
}
}
return {maxSum, {startIdx, endIdx}};
}
int main()
{
vector<double> A = {4, -5, 8, -1, 3, -4.2, 0, 2};
auto result = maxSegmentSum(A);
cout << "Maximum segment sum: " << result.first << endl;
cout << "Indices (i, j): " << result.second.first << ", " <<
result.second.second << endl;
return 0;
}

You might also like