Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Akash and Grid CodeChef Solution – Queslers

Problem : Akash and Grid CodeChef Solution

Akash is stuck in a N×NN×N grid, where NN is odd. The rows of the grid are numbered 11 to NN from top to bottom, and the columns are numbered 11 to NN from left to right. The cell at the intersection of the ii-th row and jj-th column will be denoted (i,j)(i,j).

The grid has a unique center cell — ((N+1)/2,(N+1)/2)((N+1)/2,(N+1)/2). For example, when N=5N=5 the center is cell (3,3)(3,3).

Akash is currently at cell (xs,ys)(xs,ys). He would like to reach the exit of the grid, which is located at the center. It is guaranteed that (xs,ys)(xs,ys) is not the center.

Suppose Akash is at cell (x,y)(x,y). He can make the following movements:

  • He can freely move along diagonals, i.e, to cells (x−1,y−1),(x−1,y+1),(x+1,y−1),(x+1,y+1)(x−1,y−1),(x−1,y+1),(x+1,y−1),(x+1,y+1)
  • He can move one step horizontally or vertically with the cost of 11 gold coin, i.e, to cells (x,y−1),(x,y+1),(x−1,y),(x+1,y)(x,y−1),(x,y+1),(x−1,y),(x+1,y)

Note that Akash is not allowed to make a move that will take him out of bounds of the grid.

Akash would like to minimize the number of coins required to reach the center. Please help him find this number.

Input Format

  • The first line of input contains a single integer TT, denoting the number of test cases. The description of TT test cases follows.
  • Each test case consists of a single line of input, containing three space-separated integers N,xs,ysN,xs,ys — the size of the grid and the coordinates of Akash’s starting cell.

Output Format

For each test case, output in a single line the minimum number of gold coins Akash needs to reach the center.


  • 1≤T≤1041≤T≤104
  • 3≤N<2⋅1043≤N<2⋅104
  • NN is always odd.
  • 1≤xs,ys≤N1≤xs,ys≤N

Sample Input 1 

3 2 1
5 3 1

Sample Output 1 



Test case 1: For a 3×33×3 grid, the center is at (2,2)(2,2). It is not possible to reach (2,2)(2,2) from (2,1)(2,1) using only diagonal moves. So, Akash will directly go to the center using 1 gold coin.

Test case 2: N=5N=5, so the center is (3,3)(3,3). Akash can go from (3,1)(3,1) to (2,2)(2,2) with a diagonal move, then from (2,2)(2,2) to (3,3)(3,3) with another diagonal move. So, he needs zero coins.

Akash and Grid CodeChef Solution in C++

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

int main ( ) {
    std::ios::sync_with_stdio(false) ;
    cin.tie(NULL) ;

    int T ; cin >> T ;
    while ( T -- ) {
        int n , i , j ;
        cin >> n >> i >> j ;
        i -- ; j -- ;
        int c = (n + 1) / 2 - 1 ;
        int x = abs(c - i) ;
        int y = abs(c - j) ;
        if ( x % 2 == y % 2 )   cout << "0\n" ;
        else    cout << "1\n" ;

Akash and Grid CodeChef Solution in Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
    public static void main (String[] args) throws java.lang.Exception
        // your code goes here
        BufferedReader bu=new BufferedReader(new InputStreamReader(;
        StringBuilder sb=new StringBuilder();
        int t=Integer.parseInt(bu.readLine());
            String s[]=bu.readLine().split(" ");
            int n=Integer.parseInt(s[0]),x=Integer.parseInt(s[1]),y=Integer.parseInt(s[2]);
            x=Math.abs(x-(n+1)/2); y=Math.abs(y-(n+1)/2);
            int min=Math.min(x,y);
            x-=min; y-=min;

Akash and Grid CodeChef Solution in Python

# cook your dish here
import sys
input =sys.stdin.readline
for _ in range(int(input())):
Akash and Grid CodeChef Solution Review:

In our experience, we suggest you solve this Akash and Grid CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Akash and Grid Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get

Akash and Grid Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Akash and Grid CodeChef Solution.


I hope this Akash and Grid CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More CodeChef Solutions >>

Olympics Ranking CodeChef Solution

Problem Difficulties CodeChef Solution

Chef and Bulb Invention CodeChef Solution

Array Filling CodeChef Solution

Special Triplets CodeChef Solution

Leave a Reply

Your email address will not be published. Required fields are marked *