Probability that a N digit number is palindrome
Given an integer N, the task is to find the probability that a number with a number of digits as N is a palindrome.
The number may have leading zeros.
Examples:
Input: N = 5
Output: 1 / 100
Input: N = 6
Output: 1 / 1000
Solution:
- As leading zeroes are allowed a total number of N digit numbers is 10N.
- A number is a palindrome when the first N/2 digits match with the last N/2 digits in reverse order.
- For an even number of digits, we can pick the first N/2 digits and then duplicate them to form the rest of N/2 digits so we can choose (N)/2 digits.
- For an odd number of digits, we can pick first (N-1)/2 digits and then duplicate them to form the rest of (N-1)/2 digits so we can choose (N+1)/2 digits.
- So the probability that an N digit number is palindrome is 10ceil( N / 2 ) / 10N or 1 / 10floor( N / 2 )
Below is the implementation of the approach:
C++
// C++ code of above approach #include <bits/stdc++.h> using namespace std; // Find the probability that a // n digit number is palindrome void solve( int n) { int n_2 = n / 2; // Denominator string den; den = "1" ; // Assign 10^(floor(n/2)) to // denominator while (n_2--) den += '0' ; // Display the answer cout << 1 << "/" << den << "\n" ; } // Driver code int main() { int N = 5; solve(N); return 0; } |
Java
// Java code of above approach import java.util.*; class GFG { // Find the probability that a // n digit number is palindrome static void solve( int n) { int n_2 = n / 2 ; // Denominator String den; den = "1" ; // Assign 10^(floor(n/2)) to // denominator while (n_2-- > 0 ) den += '0' ; // Display the answer System.out.println( 1 + "/" + den); } // Driver code public static void main(String[] args) { int N = 5 ; solve(N); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 code of above approach # Find the probability that a # n digit number is palindrome def solve(n) : n_2 = n / / 2 ; # Denominator den = "1" ; # Assign 10^(floor(n/2)) to # denominator while (n_2) : den + = '0' ; n_2 - = 1 # Display the answer print ( str ( 1 ) + "/" + str (den)) # Driver code if __name__ = = "__main__" : N = 5 ; solve(N); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Find the probability that a // n digit number is palindrome static void solve( int n) { int n_2 = n / 2; // Denominator String den; den = "1" ; // Assign 10^(floor(n/2)) to // denominator while (n_2-- > 0) den += '0' ; // Display the answer Console.WriteLine(1 + "/" + den); } // Driver code public static void Main(String[] args) { int N = 5; solve(N); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // Javascript implementation of the approach // Find the probability that a // n digit number is palindrome function solve(n) { let n_2 = parseInt(n / 2, 10); // Denominator let den; den = "1" ; // Assign 10^(floor(n/2)) to // denominator while (n_2-- > 0) den += '0' ; // Display the answer document.write(1 + "/" + den + "</br>" ); } let N = 5; solve(N); // This code is contributed by divyeshrabadiya07. </script> |
Output:
1/100
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Please Login to comment...