Ada and the Staircase Codechef Solution|Problem Code: ADASTAIR

Ada and the Staircase Codechef Solution


There is a big staircase with NN steps (numbered 11 through NN) in ChefLand. Let’s denote the height of the top of step ii by hihi. Chef Ada is currently under the staircase at height 00 and she wants to reach the top of the staircase (the top of the last step). However, she can only jump from height hihi to the top of a step at height hfhf if hf−hi≤Khf−hi≤K. To help Ada, we are going to construct some intermediate steps; each of them may be constructed between any two steps, or before the first step, and have any height. What is the minimum number of steps that need to be added to the staircase so that Ada could reach the top?


  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains two space-separated integers NN and KK.
  • The second line contains NN space-seperated integers h1,h2,…,hNh1,h2,…,hN.


For each test case, print a single line containing one integer — the minimum required number of steps.


  • 1≤T≤641≤T≤64
  • 1≤N≤1281≤N≤128
  • 1≤K≤1,0241≤K≤1,024
  • 1≤hi≤1,0241≤hi≤1,024 for each valid ii
  • hi<hi+1hi<hi+1 for each valid ii

Sample Input 1 

4 3
2 4 8 16

Sample Output 1 


Ada and the Staircase – CodeChef Solution in JAVA

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(;
		int T = sc.nextInt();
		for (int tc = 0; tc < T; tc++) {
			int N = sc.nextInt();
			int K = sc.nextInt();
			int[] h = new int[N];
			for (int i = 0; i < h.length; i++) {
				h[i] = sc.nextInt();
			System.out.println(solve(h, K));
	static int solve(int[] h, int K) {
		return IntStream.range(0, h.length).map(i -> computeIntermediateSteps(K, h[i] - (i == 0 ? 0 : h[i - 1]))).sum();
	static int computeIntermediateSteps(int K, int delta) {
		return Math.max(0, divideToCeil(delta, K) - 1);
	static int divideToCeil(int x, int y) {
		return x / y + (x % y == 0 ? 0 : 1);

Disclaimer: The above Problem (Ada and the Staircase) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment