Menu Close

Compute the number of possible non-increasing arrays

Arov was given a problem to solve, by his brother Dharma. 

The problem was like, given integers, N and K, Arov has to find the number (possibilities) of non-increasing arrays of length K, where each element of the array is between 1 and N (both inclusive). 

He was confused, regarding this problem.  So, help him solve the problem, so that, he can give the answer of the problem, to his brother Dharma. 

Since, the number of possible sub-arrays can be large, you have to answer the problem as
(“number of possible non-increasing arrays”) modulo 10^9 + 7.

Explanation:
Assume N=2 and K=5 as input.
In such case the Possible Arrays are as follows:

{1, 1, 1, 1, 1}

{2, 1, 1, 1, 1}

{2, 2, 1, 1, 1}

{2, 2, 2, 1, 1}

{2, 2, 2, 2, 1}

{2, 2, 2, 2, 2}

Hence, the answer is 6 (6 % (10^9  +  7)).


Input:
Two space-separated integers, N and K.

Output:
Output in a single line, the number of possible non-increasing arrays, modulo 10^9 + 7.

#include <stdio.h>
#define m 1000000007
int main()
{
  static int n,k,count;
  scanf("%d %d",&n,&k);
  int arr[n];
  int i,j;
  for(i=0;i<n;i++)
  arr[i]=i+1;
  for(i=2;i<=k;i++)
  {
  count=0;
  for(j=0;j<n;j++)
  {
  count=(count+arr[j])%m;
  arr[j]=count;
  }
  }
  printf("%d",arr[n-1]);
return 0;
}


INPUT_1:
17  8

OUTPUT:
735471


INPUT_2:
10  3

OUTPUT:
220


INPUT_3:
2  5

OUTPUT:
6


INPUT_4:
16  7

OUTPUT:
170544


ILLUSTRATION

Executed using gcc

Morae Q!

  1. Program to get dictionary items, use update module and items module.
  2. Regular expression allowing certain set of characters.
  3. Compute the Sum of Cosine Series.
  4. Find the Sum of Sine Series.
  5. Remove the Vowels from the string.
  6. Find All Non-Overlapping strings/characters using regular expressions .
  7. Check whether the given string is Panagram or not.
  8. Replacing strings using Regular Expression Regex.
  9. Check whether the given number is Strong number or not.
  10. Find the numbers divisible by a input numbers within the given range.
  11. Change the behaviour of the Regular Expression using flags.
  12. Find the exponentiation of a number.
  13. Find all prime numbers in a range using Sieve of Eratosthenes.
  14. Convert decimal equivalent into binary, hexadecimal and octal.
  15. Compute the area of a farmers field in feet.
  16. Compute time from number of days/hours to seconds.
  17. Precompiled patterns using Regular Expressions .
  18. Compute the price of the cake on the nth day.
  19. Compute the number of possible non-increasing arrays.
  20. Find the type of spinach using the calorie value.