[Solved] Diagonal Intercoms with C++

Diagonal Intercoms: Mayflower Manor was a neatly gridded apartment which consists of 1000 by 1000 houses. The inmates of the flat requested the Apartment Association Members to connect the flat using intercom systems to communicate messages during any emergency situations or any common group announcements that should be made to all the inmates.

The association head considered their request but explained them that it would be too much of cost involving to put up the system for all the houses and that they could budget the system in such a way that, two houses that are diagonal can only be made to communicate.
Now as this is the only criteria, two houses communicate each other even if there is another house located between them. To calculate the total cost, the head wanted to calculate the number of houses that could be communicated.

Input Format:
First line of the input contains n, which represents the number of houses.
Each of the next n lines contain two space separated integers xi and yi which is the number of row and column where i-th house is positioned. It’s guaranteed that no two houses share the same position.

Output Format:
Output one integer – the number of pairs of houses that can communicate each other.

Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output.]

Sample Input and Output 1:
5
1 1
1 5
3 3
5 1
5 5

6

Sample Input and Output 2:
3
1 1
2 3
3 5

0

Solution

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //sauravhathi
    int n;
    cin >> n;
    vector<int> x(n);
    vector<int> y(n);
    for (int i = 0; i < n; i++)
    {
        cin >> x[i] >> y[i];
    }
    //sauravhathi
    int count = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (abs(x[i] - x[j]) == abs(y[i] - y[j]))
            {
                //sauravhathi
                count++;
            }
        }
    }
    cout << count;
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saransh Saurav

Saransh Saurav

Articles: 67

Leave a Reply

Your email address will not be published. Required fields are marked *