-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstruct the Array.java
55 lines (42 loc) · 1.4 KB
/
Construct the Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//https://www.hackerrank.com/challenges/construct-the-array/problem
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the countArray function below.
static long countArray(int n, int k, int x) {
// Return the number of ways to fill in the array.
long temp,one,other;
temp=one=1;
other=0;
long mod =1000000007;
for(int i=1;i<n;i++)
{
temp= one;
one=(other*(k-1))%mod;
// other=(temp+(((k-2)*(other))%mod))%mod;
other =(temp +((k-2)*(other)%mod))%mod;
}
if(x==1)
return one;
else
return other;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] nkx = scanner.nextLine().split(" ");
int n = Integer.parseInt(nkx[0]);
int k = Integer.parseInt(nkx[1]);
int x = Integer.parseInt(nkx[2]);
long answer = countArray(n, k, x);
bufferedWriter.write(String.valueOf(answer));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}