📌  相关文章
📜  给定一个绝对排序数组和一个数字 K,找到总和为 K 的对

📅  最后修改于: 2021-09-03 13:44:23             🧑  作者: Mango

给定一个绝对排序数组arr[]和一个数字K ,任务是在给定数组中找到一对总和为K的元素。绝对排序数组是一个数字数组,其中|arr[i]| ≤ |arr[j]|每当i < j
例子:

方法:对于排序数组,请使用本文中讨论的方法。在绝对排序数组的情况下,对根据它们的属性通常有三种情况:

  1. 这对中的两个数字都是负数。
  2. 这对中的两个数字都是正数。
  3. 一个是消极的,另一个是积极的。

对于情况 (1) 和 (2),通过仅限制考虑正数或负数来单独使用双指针方法。
对于情况 (3),使用相同的双指针方法,其中我们有一个用于正数的索引和一个用于负数的索引,它们都从可能的最高索引开始,然后向下。
下面是上述方法的实现:

CPP
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Index Pair Structure
struct indexPair {
    int index_1, index_2;
};
 
// Function to find positive and
// Negative pairs
indexPair findPositiveNegativePairs(
    const vector& arr, int k)
{
 
    // result.index_1 for positive number &
    // result.index_2 for negative number
    indexPair result = indexPair{
        static_cast(arr.size() - 1),
        static_cast(arr.size() - 1)
    };
 
    // Find the last positive or zero number
    while (result.index_1 >= 0
           && arr[result.index_1] < 0) {
        --result.index_1;
    }
 
    // Find the last negative number
    while (result.index_2 >= 0
           && arr[result.index_2] >= 0) {
        --result.index_2;
    }
 
    // Loop to find the pair with
    // Desired Sum
    while (result.index_1 >= 0
           && result.index_2 >= 0) {
 
        // Condition if the current index pair
        // have the desired sum
        if (arr[result.index_1]
                + arr[result.index_2]
            == k) {
            return result;
        }
 
        // Condition if the current index pairs
        // sum is greater than desired sum
        else if (arr[result.index_1]
                     + arr[result.index_2]
                 > k) {
 
            // Loop to find the next
            // negative element from last
            do {
                --result.index_1;
            } while (result.index_1 >= 0
                     && arr[result.index_1] < 0);
        }
 
        // Condition if the current index pairs
        // sum is less than desired sum
        else {
 
            // Loop to find the next
            // positive or zero number from last
            do {
                --result.index_2;
            } while (result.index_2 >= 0
                     && arr[result.index_2] >= 0);
        }
    }
    return { -1, -1 };
}
 
// Function to find positive-positive number
// pairs or negative-negative number pairs
template 
indexPair findPairsOfSameSign(
    const vector& arr, int k, T compare)
{
 
    // Intializing the index pairs with
    // 0 and the end of array length - 1
    indexPair result
        = indexPair{
              0,
              static_cast(arr.size() - 1)
          };
 
    // Loop to find the first positive or negative
    // number in the array according to the given
    // comparison template function
    while (result.index_1 < result.index_2
           && compare(arr[result.index_1], 0)) {
        ++result.index_1;
    }
 
    // Loop to find the last positive or negative
    // number in the array according to the given
    // comparison template function
    while (result.index_1 < result.index_2
           && compare(arr[result.index_2], 0)) {
        --result.index_2;
    }
 
    // Loop to find the desired pairs
    while (result.index_1 < result.index_2) {
 
        // Condition if the current index pair
        // have the desired sum
        if (arr[result.index_1]
                + arr[result.index_2]
            == k) {
            return result;
        }
 
        // Conditon if the current index pair
        // is greater than or equal to the desired
        // sum according to the compare function
        else if (compare(arr[result.index_1]
                             + arr[result.index_2],
                         k)) {
 
            // Loop to find the next positive-positive
            // or negative-negative pairs
            do {
                ++result.index_1;
            } while (result.index_1 < result.index_2
                     && compare(arr[result.index_1], 0));
        }
 
        // Condition if the current index pair is
        // greater than or equal to the desired
        // sum according to the compare function
        else {
 
            // Loop to find the next positive-positive
            // or negative-negative pairs
            do {
                --result.index_2;
            } while (result.index_1 < result.index_2
                     && compare(arr[result.index_2], 0));
        }
    }
    return { -1, -1 };
}
 
// Function to find the pairs whose sum
// is equal to the given desired sum K
indexPair FindPairs(const vector& arr, int k)
{
    // Find the positive-negative pairs
    indexPair result = findPositiveNegativePairs(arr, k);
 
    // Condition to check if positive-negative
    // pairs not found in the array
    if (result.index_1 == -1
        && result.index_2 == -1) {
 
        return k >= 0
                   ? findPairsOfSameSign(
                         arr, k, less())
                   : findPairsOfSameSign(
                         arr, k, greater_equal());
    }
    return result;
}
 
// Driver Code
int main()
{
    vector A;
    A.push_back(-49);
    A.push_back(75);
    A.push_back(103);
    A.push_back(-147);
    A.push_back(164);
    A.push_back(-197);
    A.push_back(-238);
    A.push_back(314);
    A.push_back(348);
    A.push_back(-422);
    int K = 167;
    indexPair result = FindPairs(A, K);
 
    cout << result.index_2 << ' '
         << result.index_1;
 
    return 0;
}


输出:
3 7

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live