Given an integer N, the task is to count all possible sequences of length N such that all the elements of the sequence are from the range [1, N] and the sum of the elements of the sequence is even. As the answer coulb be very large so print the answer modulo 109 + 7.
Input: N = 3
All possible sequences of length 3 will be (1, 1, 2), (1, 3, 2),
(3, 1, 2), (3, 3, 2), (1, 2, 1), (1, 2, 3), (3, 2, 1), (3, 2, 3),
(2, 1, 1), (2, 1, 3), (2, 3, 1), (2, 3, 3) and (2, 2, 2).
Input: N = 5
Approach: To get even sum for any sequence, the number of odd elements must be even. Let’s choose to put x number of odd elements in the sequence where x is even. The total number of ways to put these odd numbers will be C(N, x) and in each position, y number of elements can be put where y is the count of odd numbers from 1 to N and the remaining positions can be filled with even numbers in the same way. So if x odd numbers are to be taken then their contribution will be C(N, x) * yx * (N – y)(N – x).
Below is the implementation of the above approach:
Time Complexity: O(N*logN)
- Count total number of digits from 1 to n
- Count the total number of squares that can be visited by Bishop in one move
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of sub-sequences which satisfy the given condition
- Count total set bits in all numbers from 1 to n | Set 2
- Count total divisors of A or B in a given range
- Count total unset bits in all the numbers from 1 to N
- Count of sub-sets of size n with total element sum divisible by 3
- Number of N length sequences whose product is M
- Print all non-increasing sequences of sum equal to a given number x
- Minimum number of consecutive sequences that can be formed in an array
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Find the total number of composite factor for a given number
- Find the total Number of Digits in (N!)N
- Total number of divisors for a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.