We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args)
{ int T = Convert.ToInt32(Console.ReadLine());
for (int a0 = 0; a0 < T; a0++)
{ int n = Convert.ToInt32(Console.ReadLine());
int largestPalindrome = FindLargestPalindrome(n);
Console.WriteLine(largestPalindrome); }
}
static bool IsPalindrome(int num) {
string numStr = num.ToString();
int len = numStr.Length;
int left =0;
int right =len-1;
static int FindLargestPalindrome(int n) {
for (int i = n - 1; i >= 101101; i--) {
if (IsPalindrome(i)) {
for (int j = 100; j <= 999; j++) {
if (i % j == 0 && i / j >= 100 && i / j <= 999)
{
return i;
}
}
}
}
return -1; // If no palindrome is found
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #4: Largest palindrome product
You are viewing a single comment's thread. Return to all comments →
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int T = Convert.ToInt32(Console.ReadLine()); for (int a0 = 0; a0 < T; a0++) { int n = Convert.ToInt32(Console.ReadLine()); int largestPalindrome = FindLargestPalindrome(n); Console.WriteLine(largestPalindrome); } }
static bool IsPalindrome(int num) { string numStr = num.ToString(); int len = numStr.Length; int left =0; int right =len-1;
}
static int FindLargestPalindrome(int n) { for (int i = n - 1; i >= 101101; i--) { if (IsPalindrome(i)) { for (int j = 100; j <= 999; j++) { if (i % j == 0 && i / j >= 100 && i / j <= 999) { return i; } } } } return -1; // If no palindrome is found } }