Hi friends,
I am solving this problem. Here is my code:
int n = reader.readInt();
int k = reader.readInt();
int[] val = new int[n];
int c0 = 0;
int mxv = 0;
for (int i = 0; i < val.length; i++) {
val[i] = reader.readInt();
if (val[i] == 0) {
c0++;
}
if (val[i] > mxv) {
mxv = val[i];
}
}
int[][] dp = new int[n][k];
int[] num = new int[mxv + 1];
int mod = 5000000;
for (int i = 1; i <= n; i++) {
dp[i - 1][0] = 1;
}
num[0] = c0;
for (int j = 2; j <= k; j++) {
for (int i = 2; i <= n; i++) {
if (val[i - 2] > 0) {
int tmp = val[i - 2];
while (tmp < num.length) {
num[tmp] += dp[i - 2][j - 2];
if (num[tmp] >= mod) {
num[tmp] -= mod;
}
tmp += (tmp & -tmp);
}
}
int tmp = val[i - 1] - 1;
int sum = num[0];
while (tmp > 0) {
sum += num[tmp];
if (sum >= mod) {
sum -= mod;
}
tmp -= (tmp & -tmp);
}
dp[i - 1][j - 1] = sum;
}
Arrays.fill(num, 0);
}
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += dp[i - 1][k - 1];
if (sum >= mod) {
sum -= mod;
}
}
writer.println(sum);
I get wrong answer. Where am I going wrong. Any help will be appreciated.