Wednesday, 18 October 2017

Find First Pair with given sum in the array Problem Solution Using C++

Input: 
           range = 6
           sum = 10
          ar = [3,6,8,5,4,2]
Output:
          Pair Found At Index 1 And 4

Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int n, ar[10000],sum;
 cin >> n >> sum;

 for(int i = 0; i < n; i++){
  cin >> ar[i];
 }
 for(int i = 0; i < n; i++){
  for(int j = i+1; j < n; j++){
   if(ar[i] + ar[j] == sum){
    cout << "Pair Found At Index " << i << " And " << j << endl;
    return 0;
   }
  }
 }
 cout << "Pair Not Found." << endl;
 return 0;
}

No comments:

Post a Comment