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 Three rectangles problem solution

YASH PAL, 31 July 2024
In this HackerEarth Three rectangles problem solution, You are given a rectangle of height H and width W. You must divide this rectangle exactly into three pieces such that each piece is a rectangle of integral height and width. You are required to minimize Area(max) – Area(min) where Area(max) is the area of the largest rectangle and Area(min) is the area of the smallest rectangle, among all three rectangle pieces.
HackerEarth Three rectangles problem solution

HackerEarth Three rectangles problem solution.

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

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

void solve(){
ll h,w;
cin>>h>>w;
ll ans=1e18;
rep(i,1,h-1){
ll nh=h-i,nw=w;
ll maxi=max({i*nw,nh/2*nw,(nh-nh/2)*nw});
ll mini=min({i*nw,nh/2*nw,(nh-nh/2)*nw});
ans=min(ans,maxi-mini);
maxi=max({i*nw,nw/2*nh,(nw-nw/2)*nh});
mini=min({i*nw,nw/2*nh,(nw-nw/2)*nh});
ans=min(ans,maxi-mini);
}
rep(i,1,w-1){
ll nw=w-i,nh=h;
ll maxi=max({i*nh,nw/2*nh,(nw-nw/2)*nh});
ll mini=min({i*nh,nw/2*nh,(nw-nw/2)*nh});
ans=min(ans,maxi-mini);
maxi=max({i*nh,nh/2*nw,(nh-nh/2)*nw});
mini=min({i*nh,nh/2*nw,(nh-nh/2)*nw});
ans=min(ans,maxi-mini);
}
cout<<ans<<'n';
}

signed main(){
FastIO;
int t;
cin>>t;
while(t--) solve();
return 0;
}

Second solution

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1e5 + 14;

int solve(int h, int w) {
int ans = INT_MAX;
for (auto x : {(ll) w * h / (3 * h / 2), (ll) w * h / (3 * h / 2) + 1})
ans = min<ll>(ans, max({abs(x * ll(h / 2) - ll(w - x) * h), x * ll((h + 1) / 2) - ll(w - x) * h, h % 2 * x}));
return ans;
}

int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while (t--) {
int h, w;
cin >> h >> w;
cout << min({h % 3 == 0 || w % 3 == 0 ? 0 : min(h, w), solve(h, w), solve(w, h)}) << 'n';
}
}
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