Given an array of integers A[] of size N, the task is to find the minimum sum that can be obtained by any pair of array elements which are at least K indices apart from each other.
Examples:
_Input: _A[] = {1, 2, 3, 4, 5, 6}, K = 2
_Output: _4
Explanation:
The minimum sum that can be obtained is by adding 1 and 3 that are at a distance of 2.
Input:_ A[] = {4, 2, 5, 4, 3, 2, 5}, K = 3_
Output:_ 4_
Explanation:
The minimum sum that can be obtained is by adding 2 and 2 that are at a distance of 4.
Naive Approach:
The simplest approach is to solve the problem is to iterate over the indices [i + K, N – 1] for every ith index and find the minimum element, say min. Check if min + A[i] is less than the minimum sum obtained so far and update minimum_sum accordingly. Finally, print the minimum_sum.
Below is the implementation of the above approach:
// Java Program to implement
// the above approach
**import**
java.util.*;
**class**
GFG {
// Function to find the minimum
// sum of two elements that
// are atleast K distance apart
**public**
**static**
**void**
findMinSum(``**int**
A[],
**int**
K)
{
// Length of the array
**int**
n = A.length;
**int**
minimum_sum
= Integer.MAX_VALUE;
// Iterate over the array
**for**
(``**int**
i =
0``; i < n; i++) {
// Initialize the min value
**int**
min = Integer.MAX_VALUE;
// Iterate from i + k to N
**for**
(``**int**
j = i + K; j < n; j++)
// Find the minimum
min = Math.min(min, A[j]);
**if**
(min == Integer.MAX_VALUE)
**continue**``;
// Update the minimum sum
minimum_sum = Math.min(minimum_sum,
A[i] + min);
}
// Print the answer
System.out.println(minimum_sum);
}
// Driver Code
**public**
**static**
**void**
main(String[] args)
{
**int**
A[] = {
4``,
2``,
5``,
4``,
3``,
2``,
5
};
**int**
K =
3``;
findMinSum(A, K);
}
}
Output:
4
Time Complexity:_ O(N2)_
Auxiliary Space:_ O(1)_
Efficient Approach:
The above approach can be optimized using a Suffix Array. Follow the steps below:
Below is the implementation of the above approach:
// Java Program to implement
// the above approach
**import**
java.util.*;
**class**
GFG {
// Function to find the minimum
// sum of two elements that
// are atleast K distance apart
**public**
**static**
**void**
findMinSum(``**int**
A[],
**int**
K)
{
// Length of the array
**int**
n = A.length;
**int**
suffix_min[] =
**new**
**int**``[n];
suffix_min[n -
1``] = A[n -
1``];
// Find the suffix array
**for**
(``**int**
i = n -
2``; i >=
0``; i--)
suffix_min[i]
= Math.min(suffix_min[i +
1``],
A[i]);
**int**
min_sum = Integer.MAX_VALUE;
// Iterate in the array
**for**
(``**int**
i =
0``; i < n; i++) {
**if**
(i + K < n)
// Update minimum sum
min_sum = Math.min(
min_sum, A[i]
+ suffix_min[i + K]);
}
// Print the answer
System.out.println(min_sum);
}
// Driver Code
**public**
**static**
**void**
main(String[] args)
{
**int**
A[] = {
1``,
2``,
3``,
4``,
5``,
6
};
**int**
K =
2``;
findMinSum(A, K);
}
}
Output:
4
_Time Complexity: _O(N)
_Auxiliary Space: _O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #greedy #mathematical #searching #suffix-array