Professional Documents
Culture Documents
Lab Exercise 4
Lab Exercise 4
Problem 4.1:
Instruction: - Solve the above problem with the help of following example
Given Array is
Answer:
Algorithm:
#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:
Instruction: - Solve the above problem with the help of following example
Answer:
Algorithm:
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:
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:
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
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;
}