Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices.
Example:
Input : N = 1
Output: 5
Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers.
Input: N = 2
Output: 20
Explanation: There are 20 valid numbers of length 2.
Approach : The problem can be solved with the help of combinatorics. The digits at odd indices have 4 choices and the digits at even indices have 5 choices.
Follow the steps to solve the problem:
- There are 5 choices for even indices (1, 3, 5, 7, 9 ) and 4 choices for odd indices (2, 3, 5, 7 ).
- For a number of length N, there will be N/2 odd indices and (N/2 + N%2) even indices.
- So, the number of ways to fill N/2 odd indices are 4 N/2.
- And the number of ways to fill even indices are 5(N/2 + N%2).
- Hence, the total count of all the valid numbers will be 4N/2 * 5 (N/2 + N%2).
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int find_Numb_ways( int n)
{
int odd_indices = n/2;
int even_indices = (n / 2) + (n % 2);
int arr_odd = pow (4, odd_indices);
int arr_even = pow (5, even_indices);
return arr_odd * arr_even;
}
int main()
{
int n = 4;
cout << find_Numb_ways(n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int find_Numb_ways( int n)
{
int odd_indices = n/ 2 ;
int even_indices = (n / 2 ) + (n % 2 );
int arr_odd = ( int )Math.pow( 4 , odd_indices);
int arr_even = ( int )Math.pow( 5 , even_indices);
return arr_odd * arr_even;
}
public static void main(String[] args) {
int n = 4 ;
System.out.print(find_Numb_ways(n));
}
}
|
Python3
def count(N):
odd_indices = N / / 2
even_indices = N / / 2 + N % 2
arrange_odd = 4 * * odd_indices
arrange_even = 5 * * even_indices
return arrange_odd * arrange_even
if __name__ = = "__main__" :
N = 4
print (count(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int find_Numb_ways( int n)
{
int odd_indices = n/2;
int even_indices = (n / 2) + (n % 2);
int arr_odd = ( int )Math.Pow(4, odd_indices);
int arr_even = ( int )Math.Pow(5, even_indices);
return arr_odd * arr_even;
}
public static void Main()
{
int n = 4;
Console.Write(find_Numb_ways(n));
}
}
|
Javascript
<script>
function find_Numb_ways(n)
{
var odd_indices = n/2;
var even_indices = (n / 2) + (n % 2);
var arr_odd = Math.pow(4, odd_indices);
var arr_even = Math.pow(5, even_indices);
return arr_odd * arr_even;
}
var n = 4;
document.write(find_Numb_ways(n));
</script>
|
Time Complexity: O(logn), because it is using inbuilt pow function
Auxiliary Space : O(1), (No additional space required)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Aug, 2022
Like Article
Save Article