Top MNC interview question which will be very useful for beginners to crack the interview and for there future careers

test

Breaking

Post Top Ad

Your Ad Spot

Wednesday, December 30, 2020

Google Interview Question(Count pairs with given sum)

 Question:

Find the Count pairs with a given sum

Explanation:

In the given array and sum we need to the pairs should be matching with the sum of pairs.

For Example:

            Array [8, 5, 7, 2, 5]
           Sum=10

        The Count of pairs =2

Program:

#include<iostream>
using namespace std;
int getCount(int arr[], int n, int sum)
{
    int count = 0;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (arr[i] + arr[j] == sum)
                count++;
 
    return count;
}
int main()
{
    int arr[] = { 8, 5, 7, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 10;
    cout << "The Count of pairs is "
         << getCount(arr, n, sum);
    return 0;
}

                                                                ---------End---------

Post Top Ad

Your Ad Spot