Probability that a N digit number is palindrome

22 Jun, 2022
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 / 100Input: 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++
Java
Python3
C#
Javascript
// C++ code of above approach#include <bits/stdc++.h>using namespace std; // Find the probability that a// n digit number is palindromevoid 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 codeint main(){     int N = 5;     solve(N);     return 0;}
// Java code of above approachimport java.util.*; class GFG{ // Find the probability that a// n digit number is palindromestatic 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 codepublic static void main(String[] args){    int N = 5;     solve(N);}} // This code is contributed by Rajput-Ji
# Python3 code of above approach # Find the probability that a# n digit number is palindromedef 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 codeif __name__ == "__main__" :     N = 5;     solve(N); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{ // Find the probability that a// n digit number is palindromestatic 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 codepublic static void Main(String[] args){    int N = 5;     solve(N);}} // This code is contributed by PrinciRaj1992
<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>
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.
ankthon
Rajput-Ji
princiraj1992
nidhi_biet
divyeshrabadiya07
rohitsingh57
Numbers
Probability
Mathematical
Mathematical
Numbers
Probability
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Merge two sorted arrays with O(1) extra space
Segment Tree | Set 1 (Sum of given range)
Fizz Buzz Implementation
Check if a number is Palindrome
Count ways to reach the n'th stair
Product of Array except itself
Find Union and Intersection of two unsorted arrays
Median of two sorted arrays of same size