Preparing for an interview? Check out Cracking the Coding Interview
Published on

AMAZON OA

Author
  • Shared Anonymously

This is the hackerrank FirstLadyOfSoftware question:
image

image

public class FirstLadyOfSoftware {
static final int MOD = 1000000007;

public static int findNumberOfWays(int n_intervals, int n_processes) {  
    if (n_intervals < n_processes) {  
        return 0;  
    }  

    int[][] dp = new int[n_processes + 1][n_intervals + 1];  

    // Base case: one process can be scheduled in any number of intervals  
    for (int j = 1; j <= n_intervals; j++) {  
        dp[1][j] = 1;  
    }  

    for (int i = 2; i <= n_processes; i++) {  
        for (int j = 1; j <= n_intervals; j++) {  
            dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD;  
            dp[i][j] = (dp[i][j] + dp[i - 1][j] * (j - 1)) % MOD;  
        }  
    }  

    return dp[n_processes][n_intervals];  
}  

public static void main(String[] args) {  
    // Example usage  
    int n_intervals = 2;  
    int n_processes = 1;  
    System.out.println(findNumberOfWays(n_intervals, n_processes)); // Output: 0  
}  

}

can someone help me with the solution im getting as as the o/p.

ReportMark as Helpful