Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • 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

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

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes