Largest palindrome not exceeding N which can be expressed as product of two 3-digit numbers
Given a positive integer N, the task is to find the largest palindromic number less than N and it can be expressed as the product of two 3-digit numbers.
Examples:
Input: N = 101110
Output: 101101
Explanation: The number 101101 ( = 143 * 707) is the largest palindromic number possible satisfying the conditions.
Input: N = 800000
Output: 793397
Explanation: The number 793397 ( = 869 × 913) is the largest palindromic number possible satisfying the conditions.
Naive Approach: The simplest generate all possible pairs from the range [100, 999] and for each pair, check if their product is a palindrome or not and is less than N or not. Print the maximum of all such products obtained as the required answer.
Time Complexity: O(N * 9002)
Auxiliary Space: O(1)
Alternate Approach: The problem can also be solved based on the observation that all the multiples of 11 are palindromes. Therefore, iterate over the range [100, 999] and for every value in the range, iterate over the multiples of 11 in the range [121, 999], and check for the required condition in each iteration. Follow the steps below to solve the problem:
- Initialize a variable, say num, to store the largest palindromic number satisfying the given conditions.
- Iterate over the range [100, 999] using a variable, say i, and perform the following steps:
- Iterate over the range [121, 999] using a variable, say j in multiples of 11.
- Store the product of i and j in string x.
- If the value of X is less than N and X is a palindrome, then update the value of num if X > num.
- Otherwise, keep iterating for the next pair of integers.
- After the above steps, print the value of num.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void palindrome_prod(string N){
vector< int > palindrome_list;
for ( int i = 101; i < 1000; i++)
{
for ( int j = 121; j < 1000;
j += (i % 11 == 0) ? 1 : 11)
{
int n = i * j;
string x = to_string(n);
string y = x;
reverse(y.begin(), y.end());
if (x == y){
if (n < stoi(N)){
palindrome_list.push_back(i * j);
}
}
}
}
cout << (*max_element(palindrome_list.begin(),
palindrome_list.end()));
}
int main()
{
string N = "101110" ;
palindrome_prod(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void palindrome_prod(String N){
Vector<Integer> palindrome_list = new Vector<Integer>();
for ( int i = 101 ; i < 1000 ; i++)
{
for ( int j = 121 ; j < 1000 ;
j += (i % 11 == 0 ) ? 1 : 11 )
{
int n = i * j;
String x = String.valueOf(n);
String y = x;
reverse(y);
if (x == y){
if (n < Integer.valueOf(N)){
palindrome_list.add(i * j);
}
}
}
}
System.out.print(Collections.max(palindrome_list));
}
static String reverse(String input)
{
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String N = "101110" ;
palindrome_prod(N);
}
}
|
Python3
def palindrome_prod(N):
palindrome_list = []
for i in range ( 101 , 1000 ):
for j in range ( 121 , 1000 , ( 1 if i % 11 = = 0 else 11 )):
n = i * j
x = str (n)
if x = = x[:: - 1 ]:
if n < N:
palindrome_list.append(i * j)
print ( max (palindrome_list))
N = 101110
palindrome_prod(N)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static void palindrome_prod(String N){
List< int > palindrome_list = new List< int >();
for ( int i = 101; i < 1000; i++)
{
for ( int j = 121; j < 1000;
j += (i % 11 == 0) ? 1 : 11)
{
int n = i * j;
String x = String.Join( "" , n);
String y = x;
reverse(y);
if (x == y)
{
if (n < Int32.Parse(N))
{
palindrome_list.Add(i * j);
}
}
}
}
Console.Write(palindrome_list.Max());
}
static String reverse(String input)
{
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" , a);
}
public static void Main(String[] args)
{
String N = "101110" ;
palindrome_prod(N);
}
}
|
Javascript
<script>
function palindrome_prod(N){
var palindrome_list = [];
var i,j;
for (i = 101; i < 1000; i++)
{
for (j = 121; j < 1000;
j += (i % 11 == 0) ? 1 : 11)
{
var n = i * j;
var x = n.toString();
var y = x;
y = y.split( "" ).reverse().join( "" );
if (x == y){
if (n < Number(N)){
palindrome_list.push(i * j);
}
}
}
}
document.write(Math.max.apply( null , palindrome_list));
}
var N = "101110" ;
palindrome_prod(N);
</script>
|
Time Complexity: O(N*9002)
Auxiliary Space: O(1)
Last Updated :
31 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...