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

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

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