Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Utkarsh in Gardens problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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