@@ -69,4 +69,51 @@ int main(){
6969 findPairs (a ,length , sum );
7070
7171}
72+ /*
73+ METHOD3: using quick sort technique
74+ but finding if a pair exists or not
75+ */
76+ #include <stdio.h>
77+ #include <stdlib.h>
7278
79+ int cmpfunc (const void * a , const void * b ){
80+ return (* (int * )a - * (int * )b );
81+ }
82+
83+ int findPairs (int arr [], int size , int sum ){
84+ int left , right ;
85+
86+ qsort (arr ,size ,sizeof (int ),cmpfunc );
87+
88+ left = 0 ;
89+ right = size - 1 ;
90+ while (left < right ){
91+ if (arr [left ]+ arr [right ]== sum ){
92+ return 1 ;
93+ }else if (arr [left ]+ arr [right ]< sum ){
94+ left ++ ;
95+ }else {
96+ right -- ;
97+ }
98+ }
99+ }
100+
101+ int main (){
102+ int size , index , sum , * arr ;
103+ printf ("enter number of elements in the array\n" );
104+ scanf ("%d" ,& size );
105+ //allocate memory for array
106+ arr = (int * )malloc (sizeof (int )* size );
107+ printf ("enter the elements of the array\n" );
108+ for (index = 0 ; index < size ; index ++ ){
109+ scanf ("%d" ,& arr [index ]);
110+ }
111+ printf ("enter the sum value\n" );
112+ scanf ("%d" ,& sum );
113+ if (findPairs (arr ,size ,sum )){
114+ printf ("Pairs found\n" );
115+ }else {
116+ printf ("Pairs not found\n" );
117+ }
118+
119+ }
0 commit comments