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.
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;
for (int i = 0; i < len / 2; i++) {
if (numStr[i] != numStr[len - 1 - i]) {
return false;
}
}
return true;
}
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 →
In C#:
using System;
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); } }
}