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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Construct the Array

Construct the Array

Problem
Submissions
Leaderboard
Discussions
Editorial
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Construct the Array
Exit Full Screen View
  • 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.

  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy