Time Complexity and Space Complexity | C ++ program for the above approach ; Function to find a pair in the given array whose sum is equal to z ; Iterate through all the pairs ; Check if the sum of the pair ( a [ i ] , a [ j ] ) is equal to z ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findPair ( int a [ ] , int n , int z ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( i != j && a [ i ] + a [ j ] == z ) return true ; return false ; } int main ( ) { int a [ ] = { 1 , -2 , 1 , 0 , 5 } ; int z = 0 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( findPair ( a , n , z ) ) cout << " True " ; else cout << " False " ; return 0 ; }