📜  两指针技术的 C 程序

📅  最后修改于: 2022-05-13 01:54:55.784000             🧑  作者: Mango

两指针技术的 C 程序

两个指针确实是一种简单有效的技术,通常用于在排序数组中搜索对。
给定一个排序数组 A(按升序排序),有 N 个整数,找出是否存在任何一对元素(A[i],A[j]),使得它们的和等于 X。

让我们看看天真的解决方案

C
// Naive solution to find if there is a
// pair in A[0..N-1] with given sum.
#include 
  
int isPairSum(int A[],int  N,int X)
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) 
        {
            // as equal i and j means same element
            if (i == j) 
                continue;
            
            // pair exists
            if (A[i] + A[j] == X)
                return true; 
  
            // as the array is sorted
            if (A[i] + A[j] > X)
                break; 
        }
    }
  
    // No pair found with given sum.
    return 0;
}
  
// Driver Code
int main()
{
    int arr[]={3,5,9,2,8,10,11};
    int val=17;
    int arrSize = sizeof(arr)/sizeof(arr[0]);
    
    // Function call
    printf("%d",isPairSum(arr,arrSize,val));
  
    return 0;
}


C
#include 
  
// Two pointer technique based solution to find
// if there is a pair in A[0..N-1] with a given sum.
int isPairSum(int A[], int N, int X)
{
    // represents first pointer
    int i = 0;
  
    // represents second pointer
    int j = N - 1;
  
    while (i < j)
    {
        // If we find a pair
        if (A[i] + A[j] == X)
            return 1;
  
        // If sum of elements at current
        // pointers is less, we move towards
        // higher values by doing i++
        else if (A[i] + A[j] < X)
            i++;
  
        // If sum of elements at current
        // pointers is more, we move towards
        // lower values by doing j--
        else
            j--;
    }
    return 0;
}
  
// Driver code
int main()
{
    // array declaration
    int arr[] = { 3, 5, 9, 2, 8, 10, 11 };
      
    // value to search
    int val = 17;
      
    // size of the array
    int arrSize = sizeof(arr) / sizeof(arr[0]);
    
    // Function call
    printf("%d", isPairSum(arr, arrSize, val));
  
    return 0;
}
Output1Illustration :  Time Complexity:  O(n)How does this work? The algorithm basically uses the fact that the input array is sorted. We start the sum of extreme values (smallest and largest) and conditionally move both pointers. We move left pointer i when the sum of A[i] and A[j] is less than X. We do not miss any pair because the sum is already smaller than X. Same logic applies for right pointer j.More problems based on two pointer technique. Find the closest pair from two sorted arraysFind the pair in array whose sum is closest to xFind all triplets with zero sumFind a triplet that sum to a given valueFind a triplet such that sum of two equals to third elementFind four elements that sum to a given valuePlease refer complete article on Two Pointers Technique for more details!


输出
1

时间复杂度: O(n 2 )。

现在让我们看看两点技术是如何工作的。我们使用两个指针,一个代表数组的第一个元素,另一个代表数组的最后一个元素,然后我们将两个指针处的值相加。如果它们的和小于 X,那么我们将左指针向右移动,或者如果它们的和大于 X,那么我们将右指针向左移动,以便更接近和。我们不断移动指针,直到我们得到总和为 X。

C

#include 
  
// Two pointer technique based solution to find
// if there is a pair in A[0..N-1] with a given sum.
int isPairSum(int A[], int N, int X)
{
    // represents first pointer
    int i = 0;
  
    // represents second pointer
    int j = N - 1;
  
    while (i < j)
    {
        // If we find a pair
        if (A[i] + A[j] == X)
            return 1;
  
        // If sum of elements at current
        // pointers is less, we move towards
        // higher values by doing i++
        else if (A[i] + A[j] < X)
            i++;
  
        // If sum of elements at current
        // pointers is more, we move towards
        // lower values by doing j--
        else
            j--;
    }
    return 0;
}
  
// Driver code
int main()
{
    // array declaration
    int arr[] = { 3, 5, 9, 2, 8, 10, 11 };
      
    // value to search
    int val = 17;
      
    // size of the array
    int arrSize = sizeof(arr) / sizeof(arr[0]);
    
    // Function call
    printf("%d", isPairSum(arr, arrSize, val));
  
    return 0;
}
输出
1

插图 :

时间复杂度: O(n)

这是如何运作的?
该算法基本上使用输入数组已排序的事实。我们开始对极值(最小和最大)求和,并有条件地移动两个指针。当 A[i] 和 A[j] 之和小于 X 时,我们移动左指针 i。我们不会遗漏任何一对,因为和已经小于 X。同样的逻辑适用于右指针 j。

基于两个指针技术的更多问题。

  • 从两个排序数组中找到最近的对
  • 在数组中找到总和最接近 x 的对
  • 找到所有零和的三元组
  • 找到一个总和为给定值的三元组
  • 找到一个三元组,使得两个之和等于第三个元素
  • 找到总和为给定值的四个元素

有关更多详细信息,请参阅有关两指针技术的完整文章!