Ninja and the New Year Guests Problem

Ninja has organized a new year party and wants to verify if the guests are programmers by challenging them with a coding task. As an invited programmer, you're tasked to solve it.

You need to compute the number of permutations of integers from 0 to N-1 such that at least K positions in the permutation satisfy ARR[I] = I (where 0 ≤ I < N).

The result should be given modulo 109 + 7.

Input:

The first line contains an integer 'T', indicating the number of test cases.
Each test case consists of a single line with two integers 'N' and 'K'.

Output:

For each test case, print the number of valid permutations modulo 10^9 + 7.

Example:

Input:
'N' = 4 'K' = 3
Output:
1

Explanation: There is only one permutation [0, 1, 2, 3] such that the number of elements satisfying ARR[I] = I is K = 3.

Constraints:

  • 1 ≤ 'T' ≤ 10^4
  • 4 ≤ 'N' ≤ 10^18
  • 'N - 4' ≤ 'K' ≤ 'N'
  • 1 ≤ K
  • Time Limit: 1 second

Note:

You do not need to print anything. Just implement the provided function.
AnswerBot
4mo

Compute the number of valid permutations of integers from 0 to N-1 such that at least K positions satisfy ARR[I] = I.

  • Use dynamic programming to solve the problem efficiently.

  • Consider the cases where K...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Nagarro

Q. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
Q. Given the root of a binary search tree, and an integer k, return the kth smalles...read more
Q. What is abstraction in Java?
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits