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 Micro and Sweet Distribution problem solution

YASH PAL, 31 July 202414 February 2026
In this HackerEarth Micro and Sweet Distribution problem solution It’s sweet distribution day in Micro’s school. He’s very happy. All the students in Micro’s class are sitting on chairs which are arranged in a matrix of size N x M i.e. there are N rows of chairs numbered from 1 to N and in each row there are M chairs numbered from 1 to M. Micro is sitting at coordinate (Dx, Dy) (Dthy chair of Dthx row). Teacher gives the box to a student sitting in one of the four corners: (1,1), (1,M), (N,1) or (N,M). Students have to take one sweet from the box and pass the box to the next student (student sitting to left, right, front or back). For a student sitting at coordinate , he’ll follow the following priority order:
 
If there is a student in the Right who has not received sweet, then pass it right (x, y+1).
If there is a student in the Left who has not received sweet, then pass it left (x, y-1).
If there is a student in the Front who has not received sweet, then pass it front (x-1,y).
If there is a student in the Back who has not received sweet, then pass it back (x+1,y).
Shout Over, meaning that all students have received sweets.
 
Now, Micro is curious to find out the direction in which he’ll have to pass the sweet box. Since there are a lot of students in Micro’s class, it will take long for the box to reach him, and you know Micro, he just can’t wait. So he asks you to find out the direction in which he’ll have to pass the box, or will he have to shout Over.
 
 
HackerEarth Micro and Sweet Distribution problem solution

 

 

HackerEarth Micro and Sweet Distribution problem solution.

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

int main(){
int t;cin>>t;
while(t--){
int n, m;cin>>n>>m;
int sx, sy;cin>>sx>>sy;
int x, y;cin>>x>>y;
if(sx == 1 and sy == 1){
if(x&1){
if(y == m){
if(x == n)cout<<"Over";
else cout<<"Back";
}
else cout<<"Right";
}
else{
if(y == 1){
if(x == n)cout<<"Over";
else cout<<"Back";
}
else cout<<"Left";
}
}
else if(sx == 1 and sy == m){
if(x&1){
if(y == 1){
if(x == n)cout<<"Over";
else cout<<"Back";
}
else cout<<"Left";
}
else{
if(y == m){
if(x == n)cout<<"Over";
else cout<<"Back";
}
else cout<<"Right";
}
}
else if(sx == n and sy == 1){
int dx = n-x+1;
if(dx&1){
if(y == m){
if(x == 1)cout<<"Over";
else cout<<"Front";
}
else cout<<"Right";
}
else{
if(y == 1){
if(x == 1)cout<<"Over";
else cout<<"Front";
}
else cout<<"Left";
}
}
else{
int dx = n-x+1;
if(dx&1){
if(y == 1){
if(x == 1)cout<<"Over";
else cout<<"Front";
}
else cout<<"Left";
}
else{
if(y == m){
if(x == 1)cout<<"Over";
else cout<<"Front";
}
else cout<<"Right";
}
}
cout<<endl;
}
return 0;
}
 

Second solution

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define pin(s) {cout<<(s)<<endl;continue;}
int main(){
int T,N,M,sx,sy,dx,dy;
cin>>T;
while(T--){
cin>>N>>M>>sx>>sy>>dx>>dy;
int ox = N+1-sx, oy = N%2==0?sy:M+1-sy;
if(ox==dx and oy==dy)
pin("Over")
if((dx-sx)%2==0 and dy==M+1-sy or (dx-sx)%2!=0 and dy==sy)
pin(sx==1?"Back":"Front")
if((dx-sx)%2==0)pin(sy==1?"Right":"Left")
else pin(sy==M?"Right":"Left")
}
}
 
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