SOPC003A Editorial

PROBLEM LINK:

Practice
Contest

Author: Shriram Chandran
Editorialist: Shriram Chandran

DIFFICULTY:

Cakewalk

PREREQUISITES:

Nil.

PROBLEM:

Given an array, find the number of pairs of numbers such that each of them is within the range of half to twice of the other number.

EXPLANATION:

In the easy version, there is enough time to perform an O(n^2) search over the entire array, and find out for each pair if the number of possible teams.

Use two variables i and j, 1 \leq i < j \leq N, and increment a count variable count whenever a_i <= 2a_j and a_j <= 2a_i.

SOLUTION:

Setter's Solution
#include<bits/stdc++.h>
#define ll long long
using namespace std;

void solve()
{
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    ll ans=0;
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            if(a[i]<=2*a[j]&&a[j]<=2*a[i])
                ans++;
    cout<<ans<<endl;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
        solve();
    return 0;
}