Time Complexity and Space Complexity | Java 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
import java . lang . * ; import java . util . * ; class GFG { static boolean 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 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , - 2 , 1 , 0 , 5 } ; int z = 0 ; int n = a . length ; if ( findPair ( a , n , z ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }