In this HackerEarth Number of triangles problem solution you are given a polygon of N sides with vertices numbered from 1, 2, ..., N. Now, exactly 2 vertices of the polygons are colored black, and the remaining are colored white. You are required to find the number of triangles denoted by A such that:
  1. The triangle is formed by joining only the white-colored vertices of the polygon.
  2. The triangle shares at least one side with the polygon.


HackerEarth Number of triangles problem solution


HackerEarth Number of triangles problem solution.

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

int main() {
    
    int t;
    cin>>t;
    while(t--) {
        
        int n,b1,b2;
        cin>>n>>b1>>b2;
        long long one_side = 0;
        long long two_side = 0;

        int b1_one_side = n-4 + (2)*(n-4);
        int b2_one_side = n-4 + (2)*(n-4);
        int b1_b2_one_side = 0;
        if((b1+1)%n==b2%n||(b2+1)%n==b1%n) {
            b1_b2_one_side = n-4;
        }
        else if((b1+2)%n==b2%n||(b2+2)%n==b1%n) {
            b1_b2_one_side = 2;
        }
        else b1_b2_one_side = 4;
        one_side  = 1ll*n*(n-4) - b2_one_side - b1_one_side + b1_b2_one_side ;
        
        int b1_two_side = 3;
        int b2_two_side = 3;
        int b1_b2_two_side = 0;
        if((b1+1)%n==b2%n||(b2+1)%n==b1%n) {
            b1_b2_two_side = 2;
        }
        else if((b1+2)%n==b2%n||(b2+2)%n==b1%n) {
            b1_b2_two_side = 1;
        }
        two_side = n - b1_two_side - b2_two_side + b1_b2_two_side ;
        cout<<one_side+two_side <<endl;
    }
    return 0;
}


second solution

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

const int maxn = 1e3 + 4;
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        int n, b1, b2;
        cin >> n >> b1 >> b2;
        b1--, b2--;
        ll ans = 0;
        auto bad = [&](int i){
            return i == b1 || i == b2;
        };
        for(int i = 0; i < n; i++){
            if(!bad(i) && !bad((i + 1) % n))
                ans += n - 4;
            if(!bad(i) && !bad((i + 1) % n) && !bad((i + 2) % n))
                ans--;
        }
        cout << ans << '\n';
    }
}