Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Number of triangles problem solution

YASH PAL, 31 July 202413 February 2026
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';
}
}
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes