Ada and the Staircase Codechef Solution

Hello Programmers In this post, you will know how to solve the Ada and the Staircase Codechef Solution. The Problem Code: ADASTAIR

Ada and the Staircase Codechef Solution
Ada and the Staircase Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Ezoicreport this adProblem

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?

Input

  • 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.

Output

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

Constraints

  • 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 

1
4 3
2 4 8 16

Sample Output 1 

3

Ada and the Staircase CodeChef Solution in JAVA

import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		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));
		}
		sc.close();
	}
	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 BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Ada School Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad