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 Utkarsh in Gardens problem solution

YASH PAL, 31 July 202416 February 2026
In this HackerEarth Utkarsh in Gardens problem solution Utkarsh has recently put on some weight. In order to lose weight, he has to run on the boundary of gardens.
 
But he lives in a country where there are no gardens. There are just many bidirectional roads between cities.
 
Due to the situation, he is going to consider any cycle of length four as a garden. Formally a garden is considered to be an unordered set of 4 roads {r0, r1, r2, r3} where ri and ri+1 mod 4 share a city.
 
Now he wonders how many distinct gardens are there in this country.
 
 
HackerEarth Utkarsh in Gardens problem solution

 

 

HackerEarth Utkarsh in Gardens problem solution.

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define fre freopen("0.in","r",stdin);freopen("0.out","w",stdout)
#define abs(x) ((x)>0?(x):-(x))
#define MOD 1000000007
#define lld unsigned long long int
#define pii pair<int,int>
#define scan(x) scanf("%d",&x)
#define print(x) printf("%dn",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lldn",x)
int G[1003][50];
const int B = 30;
int countCommon(int i,int j){
int c = 0;
for(int k=0;k<=33;++k){
c += __builtin_popcount(G[i][k] & G[j][k]);
}
return c;
}
int addEdge(int i,int j){
int b = j/B;
int c = j%B;
G[i][b] |= (1LL<<c);
}
int main(){
int N,M,a,b;
int x;
cin>>N;
for(int i=1;i<=N;++i){
for(int j=1;j<=N;++j){
scan(x);
if(x==1){
addEdge(i-1,j-1);
}
}
}
lld ans = 0;
for(int i=1;i<=N;++i){
for(int j=i+1;j<=N;++j){
lld c = countCommon(i-1,j-1);
ans += (c*(c-1))/2;
}
}
cout<<(ans/2);
}
 

Second solution

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

const int MAXN = 2010;
bitset<MAXN> g[MAXN], com;
int n;
int main()
{
scanf("%d", &n);
assert(1 <= n && n <= 2000);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
assert(x == 0 || x == 1);
g[i][j] = x;
}
}
for (int i = 1; i <= n; i++) {
assert( g[i][i] == 0 );
for (int j = 1; j <= n; j++) {
assert(g[i][j] == g[j][i]);
}
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
long long cnt = 0;
cnt = (g[i] & g[j]).count();
ans += cnt*(cnt - 1) / 2;
}
}
cout<<ans/2<<endl;
return 0;
}
 
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