We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Construct the Array

Construct the Array

Problem
Submissions
Leaderboard
Discussions
Editorial

Your goal is to find the number of ways to construct an array such that consecutive positions contain different values.

Specifically, we want to construct an array with elements such that each element between and , inclusive. We also want the first and last elements of the array to be and .

Given , and , find the number of ways to construct such an array. Since the answer may be large, only find it modulo .

For example, for , , , there are ways, as shown here:

image

Complete the function countArray which takes input , and . Return the number of ways to construct the array such that consecutive elements are distinct.

Constraints

Subtasks

  • For of the maximum score, and

Sample Input

, ,

Sample Output

Explanation

Refer to the diagram in the challenge statement.

Author

kemalkochekov

Difficulty

Medium

Max Score

35

Submitted By

13567

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature