Find a pair of elements from an array whose sum equals a given number

后端 未结 30 1377
暗喜
暗喜 2020-11-22 10:14

Given array of n integers and given a number X, find all the unique pairs of elements (a,b), whose summation is equal to X.

The following is my solution, it is O(nLo

30条回答
  •  滥情空心
    2020-11-22 10:53

    this is the implementation of O(n*lg n) using binary search implementation inside a loop.

    #include 
    
    using namespace std;
    
    bool *inMemory;
    
    
    int pairSum(int arr[], int n, int k)
    {
        int count = 0;
    
        if(n==0)
            return count;
        for (int i = 0; i < n; ++i)
        {
            int start = 0;
            int end = n-1;      
            while(start <= end)
            {
                int mid = start + (end-start)/2;
                if(i == mid)
                    break;
                else if((arr[i] + arr[mid]) == k && !inMemory[i] && !inMemory[mid])
                {
                    count++;
                    inMemory[i] = true;
                    inMemory[mid] = true;
                }
                else if(arr[i] + arr[mid] >= k)
                {
                    end = mid-1;
                }
                else
                    start = mid+1;
            }
        }
        return count;
    }
    
    
    int main()
    {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        inMemory = new bool[10];
        for (int i = 0; i < 10; ++i)
        {
            inMemory[i] = false;
        }
        cout << pairSum(arr, 10, 11) << endl;
        return 0;
    }
    

提交回复
热议问题