Finding the second largest element in array without sorting - c

I know about the single traversal method initialising two variables to INT_MIN. But my question is why do we initialise two variables to INT_MIN and also what is the purpose of INT_MIN here?
Why can't we initialise two variables to its first element like I have done in the code below? Because when I hand-checked the code manually, I found nothing wrong. So why doesn't the code run properly?
#include <stdio.h>
int main(void) {
int x[10];
int i, n;
int first = x[0];
int second = x[0];
printf("Input the size of array :");
scanf("%d", &n);
printf("Input %d elements in the array :\n", n);
for (i = 0; i < n; i++) {
printf("x[%d]: ", i);
scanf("%d", &x[i]);
}
for (i = 0; i < n; ++i) {
if (first < x[i]) {
second = first;
first = x[i];
} else
if (x[i] > second && x[i] != first) {
second = x[i];
}
}
if (second == first)
printf("There is no second largest element\n");
else
printf("\nThe Second largest element in the array is: %d", second);
return 0;
}

There are several problems in your code:
the array x is defined with a length of 10, but uninitialized when you set first and second to the value of its first element.
you do not test the return value of scanf(), leading to undefined behavior in case of input failure.
you do not test of n is in less or equal to 10 before reading values into x.
you need to special case n <= 0 as no values will be read into x.
Here is a modified version:
#include <stdio.h>
int main(void) {
int x[10];
int i, n, first, second;
printf("Input the size of array :");
if (scanf("%d", &n) != 1 || n < 0 || n > 10) {
printf("invalid input\n");
return 1;
}
if (n <= 0) {
first = second = 0;
} else {
printf("Input %d elements in the array:\n", n);
for (i = 0; i < n; i++) {
printf("x[%d]: ", i);
if (scanf("%d", &x[i]) != 1) {
printf("invalid input\n");
return 1;
}
}
first = second = x[0];
for (i = 1; i < n; ++i) {
if (first < x[i]) {
second = first;
first = x[i];
} else
if (x[i] > second && x[i] != first) {
second = x[i];
}
}
}
if (second == first)
printf("There is no second largest element\n");
else
printf("\nThe Second largest element in the array is: %d\n", second);
return 0;
}
Regarding an alternative implementation where first and second are initialized to INT_MIN and the loop starts at i = 0, the trick is INT_MIN is the smallest possible int value, so first will compare <= to all values of the array and therefore will not shadow a smaller value. It is also a good default return value for a function that finds the maximum value in an array when passed an empty array.
For your case study, the INT_MIN approach is does not work and the algorithm would fail on an array with a single repeated value: at the end of the scan, first would be set to that value and second would still be INT_MIN.
testing first == second would yield a second largest value equal to INT_MIN, which is incorrect.
testing second == INT_MIN to determine if all values are identical would be incorrect too as an array with values { 1, INT_MIN } would indeed have a second largest value equal to INT_MIN.
Your approach works correctly and the alternative would need to be written differently, with an extra variable. Indeed the solution presented in this article is incorrect, and so is this one, this one, this one and countless more random code across the Internet.

I've added some comments where I saw some problems. Hopefully I caught all the problems. Code below.
#include <stdio.h>
int main(void) {
// int x[10]; I moved this to under where you ask the user for the array size.
int i, n;
// int first=x[0]; This should be written after the user has inputted their numbers. Because what is in x[0]? user hasn't entered anything yet
// int second=x[0]; Same reason as ^
printf("Input the size of array :");
scanf("%d",&n);
int x[n]; // This should be here because you asked the user what the size of the array is.
printf("Input %d elements in the array :\n",n);
for(i=0; i<n; i++)
{
printf("x[%d]: ", i);
scanf("%d", &x[i]);
}
// You should put your first and second int's here
int first=x[0];
int second=x[0];
for (i=0; i<n ; ++i)
{
if (first<x[i])
{
second = first;
first = x[i];
}
else if (x[i] > second && x[i] != first)
{
second = x[i];
}
}
if (second == first)
printf("There is no second largest element\n");
else
printf("\nThe Second largest element in the array is: %d", second);
return 0;
}

int first=x[0];
int second=x[0];
x isn't initialized yet.

Prints -1 if no second largest element is found.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int findNotSame(long long int a[],long int n)
{
long long int temp = a[0];
int flag = 0;
long int i;
for(i=0;i<n;i++)
{
if(a[i]!=temp)
return 1;
}
return 0;
}
long long int findMax(long long int a[],long int n)
{
long int i;
long long int max = a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max = a[i];
}
return max;
}
int main() {
long int i,j,n;
scanf("%ld",&n);
long long int a[n];
if(n<2) //There cannot be scond largest if there;s only one(or less) element.
{
printf("-1");
return 0;
}
for(i=0;i<n;i++) //Read elements.
scanf("%lld",&a[i]);
if (!findNotSame(a,n)) //Check if all the elements in array are same if so, then -1.
{
printf("-1");
return 0;
}
long long int max = findMax(a,n); //Find maximum element(first).
long long int max2 = -999999999999999; //Initialize another max which will be the second maximum.
for(i=0;i<n;i++) //Find the second max. element.
{
if(a[i]>max2 && a[i] != max)
max2 = a[i];
}
if(max == max2) //Incase if second max(largest) is same as maximum
max2 = -1;
printf("%lld",max2);
return 0;
}

All solution is better, but some fail when the same items available in the array
like,
int arr[] = {56, 41, 19, 33, 13, 23, 25, 56};
56 available in two times,
so for this solution,
int arr[] = {56, 41, 19, 33, 13, 23, 25,56};
var max = arr[0];
var secMax=-1;
var size = arr.length;
for(var l = 1; l < size; l++) {
if (max < arr[l]) {
secMax = max;
max = arr[l];
} else if (secMax < arr[l] && arr[l] != max) {
secMax = arr[l];
}
}
System.out.println("Second largest number :-" + secMax);

import ast
input_str = input()
input_list = ast.literal_eval(input_str)
if len(input_list)<2:
print("not present")
else:
i=input_list[0]
j=i
for index_val in input_list[1:]:
if i<index_val:
j=i
i=index_val
elif index_val>j and index_val!=i:
j=index_val
elif i==j and index_val<j:j=index_val
if i==j:
print("not present")
else:
print(j)

This works,
val arr=Array(4,1,2,4,5,5,7,18,10,5,7)
var firstAndSecondIndex:(Int,Int)=null
for(indexVal <- 2 to (arr.size -1))
firstAndSecondIndex match {
case null =>
println("0,0")
firstAndSecondIndex=(0,1)
case value =>
value match {
case value if arr(indexVal) == arr(value._1) || arr(indexVal) == arr(value._2) =>
println("equals")
case value if arr(indexVal) > arr(value._1) =>
println("1,0")
value match {
case value if arr(indexVal) > arr(value._2) && arr(value._1) > arr(value._2) =>
firstAndSecondIndex=(indexVal,value._1)
case value if arr(indexVal) > arr(value._2) && arr(value._1) < arr(value._2) =>
firstAndSecondIndex=(indexVal,value._2)
case value if arr(indexVal) < arr(value._2) =>
firstAndSecondIndex=(indexVal,value._2)
}
case value if arr(indexVal) < arr(value._1) =>
println("1,1")
value match {
case value if arr(indexVal) > arr(value._2) =>
firstAndSecondIndex=(indexVal,value._1)
case value if arr(indexVal) < arr(value._2) =>
println("not greater")
}
}
}
val secondLargest= arr(firstAndSecondIndex._1) < arr(firstAndSecondIndex._2) match {case true => arr(firstAndSecondIndex._1) case false => arr(firstAndSecondIndex._2)}

int arr[] = {56, 41, 19, 33, 13, 23, 25};
int max = 1;
int secondMax = 0;
for (int i = 0; i < arr.length; i++) {
int getValue = arr[i];
if (max == 1) {
max = getValue;
secondMax = arr[1];
} else {
if (max < getValue) {
secondMax = max;
max = getValue;
} else if (secondMax < getValue) {
secondMax = getValue;
} else {
// Nothing Do
}
}
}
System.out.println("" + secondMax);

Java code to find the largest and second largest number in an array without sorting and using a single loop:
package programs;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int largest = -1;
int secondlargest = -1;
int numberPos = -1;
int numberPos1 = -1;
int[] arr = {22, 33, 9000, 70, 9000, -1, -10, -3, 22, 99, 100, 10000};
for (int i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
numberPos = i;
largest = arr[i];
}
}
for (int i = 0; i < arr.length; i++) {
if (secondlargest < arr[i] && secondlargest < largest && arr[i] != largest) {
secondlargest = arr[i];
numberPos1 = i;
}
}
System.out.println("Largest number is "+largest+" with position "+numberPos);
System.out.println("Second largest is "+secondlargest+" with position "+numberPos1);
}
}

Related

How to check if array elements are perfect numbers?

I'm trying to make a program where the array size is not entered by the user,but the elements are, until 0 is entered.Now I want to check for each element which one is a perfect number,for that I have to do the sum of the divisors.Problem is I can't manage to do the sum of divisors for each element in the array,instead it adds all the divisors of all the elements in the array.
#include <stdio.h>
int main()
{
int n = 1000, i, j, sum = 0;
int v[n];
for (i = 1; i < n; i++)
{
scanf("%d", &v[i]);
if (v[i] == 0)
{
break;
}
for (j = 1; j < v[i]; j++)
{
if (v[i] % j == 0)
{
printf("%d", j);
sum = sum + j;
}
}
}
printf("\n%d",sum);
return 0;
}
OUTPUT
Brut force check can be very expensive. It is faster to build the table of perfect numbers using Euclides formula and then simple check if the number is perfect.
static unsigned long long getp(int x)
{
return (2ULL << (x - 2)) * ((2ULL << (x - 1)) - 1);
}
int isperfect(unsigned long long x)
{
const int primes[] = {2, 3, 5, 7, 13, 17, 19, 31};
static unsigned long long array[sizeof(primes) / sizeof(primes[0])];
int result = 0;
if(!array[0])
{
for(size_t index = 0; index < sizeof(primes) / sizeof(primes[0]); index++)
{
array[index] = getp(primes[index]);
}
}
for(size_t index = 0; index < sizeof(primes) / sizeof(primes[0]); index++)
{
if(x == array[index])
{
result = 1;
break;
}
}
return result;
}
The array of perfect numbers is build only one time on the first function call.
And some usage (your code a bit modified)
int main(void)
{
size_t n = 1000, i;
unsigned long long v[n];
for (i = 1; i < n; i++)
{
scanf("%llu", &v[i]);
if (v[i] == 0)
{
break;
}
printf("%llu is %s perfect number\n", v[i], isperfect(v[i]) ? "" : "not");
}
return 0;
}
https://godbolt.org/z/exMs345xb

No values returned from function in C

I was trying to write a function that checks if an array has consecutive numbers (not necessarily in order), return 0 if not and 1 if yes.
For example:
24, 21, 22, 23
The return value would be 1.
22,22,22
Would return 0.
The problem is that it return 1 for something like:
22,22,22
I could really use the help looking into it.
This is the main function (no need to check it or anything, it's fine):
#include <stdio.h>
#include <stdlib.h>
/* Function declarations */
void Ex1();
void Ex2();
void Ex3();
void Ex4();
void Ex5();
/* Declarations of other functions */
int f3(int *, int);
/* ------------------------------- */
//
int main() {
int select = 0, i, all_Ex_in_loop = 0;
printf("Run menu once or cyclically?\n"
"(Once - enter 0, cyclically - enter other number) ");
if (scanf_s("%d", &all_Ex_in_loop) == 1)
do {
for (i = 1; i <= 5; i++)
printf("Ex%d--->%d\n", i, i);
printf("EXIT-->0\n");
do {
select = 0;
printf("please select 0-5 : ");
scanf_s("%d", &select);
} while ((select < 0) || (select > 5));
switch (select) {
case 1: Ex1(); break;
case 2: Ex2(); break;
case 3: Ex3(); break;
case 4: Ex4(); break;
case 5: Ex5(); break;
}
} while (all_Ex_in_loop && select);
return 0;
}
and this is the function (UPDATED):
void Ex3() {
int n, i, res;
printf("Enter the size of the Array: ");
scanf_s("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
if (!arr) {
printf("ERROR - not enough memory.");
exit(1);
}
printf("Enter an Array >>> ");
for (i = 0; i < n; i++)
scanf_s("%d", &arr[i]);
res = f3(arr, n);
printf("res = %d\n", res);
free(arr);
}
int f3(int *arr, int size) {
int i, min = arr[0], max = arr[0];
for (i = 1; i < size; i++) {
if (arr[i] < min)
min = arr[i];
if (arr[i] > max)
max = arr[i];
}
int *CounterArray = (int *)calloc(max + 1, sizeof(int));
if (!CounterArray) {
printf("ERROR - not enough memory.");
exit(1);
}
for (i = 0; i < size; i++) {
CounterArray[arr[i]]++;
}
for (i = min; i <= max; i++)
if (CounterArray[i] == 0)
return 0;
free(CounterArray);
return 1;
}
There are multiple problems in your code:
in function f3() you allocate an array of max + 1 integers. This would be a problem is all numbers in the array are negative. You should instead check that max - min + 1 is exactly the size of the array and allocate size elements.
you reject the array if and only if an element in the range has a 0 count. You should instead check that all elements have a 1 count. This explains why the function fails for 22,22,22.
Here is a modified version:
int f3(const int *arr, int size) {
int result = 1;
if (size <= 0)
return 1;
int i, min = arr[0], max = arr[0];
for (i = 1; i < size; i++) {
if (min > arr[i])
min = arr[i];
if (max < arr[i])
max = arr[i];
}
if (max - min + 1 != size)
return 0;
unsigned char *CounterArray = calloc(size, sizeof(*CounterArray));
if (!CounterArray) {
printf("ERROR - not enough memory.");
exit(1);
}
for (i = 0; i < size; i++) {
// increment the count of this element
if (CounterArray[arr[i] - min]++) {
// if the count was already non zero, the test fails.
result = 0;
break;
}
}
free(CounterArray);
return result;
}
You return zero under the condition that
if (CounterArray[i] == 0)
but in your test case of 22,22,22 you have min and max both equal 22 and CounterArray[22] equal 3, not 0.
You need to re-think the condition...

Smallest odd number in given array

This code is supposed to find the smallest odd number in given array and store it in min but when I try to print min it always prints 0.
int smallestodd(int x[5]){
int j;
int k[5];
int p = 0;
int r = 0;
for(int h =0; h<5;h++){
j = x[h] % 2;
if(j == 1){
int temp =x[h];
k[p] =temp;
p++;
}
}
int min = k[0];
while(k[r] !=0){
if(k[r] < min ){
min = k[r];
r++;
}
}
return min;
}
Assuming there is an odd number in the array -- let's say trying to find the minimum odd number in an array with just even numbers (or no numbers) is UB :)
index = 0;
while (arr[index] % 2 == 0) index++; // skip even numbers
min = arr[index++]; // first odd number
while (index < length) {
if (arr[index] % 2) {
if (arr[index] < min) min = arr[index];
}
index++;
}
this code avoid overflow in search and return 1 when found or 0 if array has only even numbers.
int getMinOdd(int arr[], int length, int *value) {
int found = 0;
for(int idx=0; idx < length; idx++) {
if (arr[idx] % 2) {
if (!found || *value > arr[idx]) {
*value = arr[idx];
}
found = 1;
}
}
return found;
}
It's quite simple actually. You need to just check 2 conditions on your array.
int smallestOdd(int arr[]){
int min = 99999; //Some very large number
for(int i = 0; i < (length of your array); i++) {
if(arr[i]%2 != 0 && arr[i] < min) { //Check if number is odd and less than the current minimum value
min = arr[i];
}
}
return min;
}
Use this using statement as first :
Using System Linq;
Console.WriteLine(myArray.Where(i => i%2 == 1).Min());

C function to print maximum and minimum numbers

'm currently writing a C program that prompts a user to enter five numbers and to display the maximum number. So here's what i came up with
int max_num(float num_arr[])
// this is a number array
{
int max = 0;
for (int k = 0; k <= 4; k++)
{
max = num_arr[0];
if (max > num_arr[k])
{
max = num_arr[k];
printf("The maximum number is is %d\n", max);
}
}
}
Any help is appreciated.
Your function will not work for numbers entered less than 0, also int type is wrong for the fuunction since it's not returning anything. Here is a better version of the same:
void max_num(float num_arr[], int size)
{
if(size>0) float max = num_arr[0];
for (int k = 0; k <= size; k++) {
if (max < num_arr[k]) {
max = num_arr[k];
}
}
printf("The maximum number is %f\n", max);
}
And if you need to return max:
float max_num(float num_arr[], int size)
{
if(size>0) float max = num_arr[0];
for (int k = 0; k <= size; k++) {
if (max < num_arr[k]) {
max = num_arr[k];
}
}
return max;
}
You are setting
max = num_arr[0];
in every loop iteration, so you are losing the max value every time and set it
to the first value in the array.
The correct version is:
for(int k = 0;k<5;k++)
{
if(num_arr[k] > max)
max = num_arr[k];
}
printf("The maximum number is is %d\n", max);
Having a <= in the condition is not incorrect, but it's not very readable either,
it's hard to miss the = sign. It's always better to just use <, in this case
k<5.
Also it's better practice to pass the size of the array to the functions, so you
don't have to hard code the maximal number of items to loop through (like you
did in your code). Like this:
#include <stdio.h>
float max_num(float *array, size_t len)
{
if(array == NULL)
{
fprintf(stderr, "invalid argument, array == NULL\n");
return 0;
}
if(len == 0)
{
fprintf(stderr, "Invalid array size, cannot be 0\n");
return 0;
}
float max = array[0];
for(size_t i = 0; i < len; ++i)
{
if(array[i] > max)
max = array[i];
}
return max;
}
int main(void)
{
float nums[] = { 1.1, -2.2, 8, 6, 99 , -12 };
float max = max_num(nums, sizeof nums / sizeof *nums);
printf("The maximal number in the array is: %f\n", max);
return 0;
}
Rather than if(max > num_arr[k]) it should be if(num_arr[k] > max)

Project Euler Problem 4

I've created a solution to problem 4 on Project Euler.
However, what I find is that placing the print statement (that prints the answer) in different locations prints different answers. And for some reason, the highest value of result is 580085. Shouldn't it be 906609? Is there something wrong with my isPalindrome() method?
#include <stdio.h>
#include <stdbool.h>
int isPalindrome(int n);
//Find the largest palindrome made from the product of two 3-digit numbers.
int main(void)
{
int i = 0;
int j = 0;
int result = 0;
int palindrome = 0;
int max = 0;
//Each iteration of i will be multiplied from j:10-99
for(i = 100; i <= 999; i++)
{
for(j = 100; j <= 999; j++)
{
result = i * j;
if(isPalindrome(result) == 0)
{
//printf("Largest Palindrome: %d\n", max); //906609
//printf("Result: %d\n", result); //580085
if(result > max)
{
max = result;
//printf("Largest Palindrome: %d\n", max); //927340
}
printf("Largest Palindrome: %d\n", max); //906609
}
}
}
//printf("Largest Palindrome: %d\n", max); //998001
system("PAUSE");
return 0;
} //End of main
//Determines if number is a palindrome
int isPalindrome(int num)
{
int n = num;
int i = 0;
int j = 0;
int k = 0;
int count = 0;
int yes = 0;
//Determines the size of numArray
while(n/10 != 0)
{
n%10;
count++;
n = n/10;
}
int numArray[count];
//Fill numArray with each digit of num
for(i = 0; i <= count; i++)
{
numArray[i] = num%10;
//printf("%d\n", numArray[i]);
num = num/10;
}
//Determines if num is a Palindrome
while(numArray[k] == numArray[count])
{
k = k + 1;
count = count - 1;
yes++;
}
if(yes >= 3)
{
return 0;
}
}//End of Function
I remember doing that problem a while ago and I simply made a is_palindrome() function and brute-forced it. I started testing from 999*999 downwards.
My approach to detect a palindrome was rather different from yours. I would convert the given number to a string and compare the first char with the nth char, second with n-1 and so on.
It was quite simple (and might be inefficient too) but the answer would come up "instantly".
There is no problem in the code in finding the number.
According to your code fragment:
.
.
}
printf("Largest Palindrome: %d\n", max); //906609
}
}
}
//printf("Largest Palindrome: %d\n", max); //998001
system("PAUSE");
.
.
.
you get the result as soon as a palindrome number is found multiplying the number downwards.
You should store the palindrome in a variable max and let the code run further as there is a possibility of finding a greater palindrome further.
Note that for i=800,j=500, then i*j will be greater when compare with i=999,j=100.
Just understand the logic here.
A few issues in the isPalindrome function :
the first while loop doesn't count the number of digits in the number, but counts one less.
as a result, the numArray array is too small (assuming that your compiler supports creating the array like that to begin with)
the for loop is writing a value past the end of the array, at best overwriting some other (possibly important) memory location.
the second while loop has no properly defined end condition - it can happily compare values past the bounds of the array.
due to that, the value in yes is potentially incorrect, and so the result of the function is too.
you return nothing if the function does not detect a palindrome.
//Determines if number is a palindrome
bool isPalindrome(int num) // Change this to bool
{
int n = num;
int i = 0;
int j = 0;
int k = 0;
int count = 1; // Start counting at 1, to account for 1 digit numbers
int yes = 0;
//Determines the size of numArray
while(n/10 != 0)
{
// n%10; <-- What was that all about!
count++;
n = n/10;
}
int numArray[count];
//Fill numArray with each digit of num
for(i = 0; i < count; i++) // This will crash if you use index=count; Array indices go from 0 to Size-1
{
numArray[i] = num%10;
//printf("%d\n", numArray[i]);
num = num/10;
}
//Determines if num is a Palindrome
/*
while(numArray[k] == numArray[count-1]) // Again count-1 not count; This is really bad though what if you have 111111 or some number longer than 6. It might also go out of bounds
{
k = k + 1;
count = count - 1;
yes++;
}
*/
for(k = 1; k <= count; k++)
{
if(numArray[k-1] != numArray[count-k])
return false;
}
return true;
}//End of Function
That's all I could find.
You also need to change this
if(isPalindrome(result) == 0)
To
if(isPalindrome(result))
The Code's Output after making the modifications: Link
The correct printf is the one after the for,after you iterate through all possible values
You are using int to store the value of the palidrome but your result is bigger then 65536,
you should use unsigned
result = i * j;
this pice of code is wrong :
while(n/10 != 0) {
n%10;
count++;
n = n/10;
}
it should be:
while(n != 0) {
count++;
n = n/10;
}
As well as the changes that P.R sugested.
You could do something like this to find out if the number is palindrome:
int isPalindrom(unsigned nr) {
int i, len;
char str[10];
//convert number to string
sprintf(str, "%d", nr);
len = strlen(str);
//compare first half of the digits with the second half
// stop if you find two digits which are not equal
for(i = 0; i < len / 2 && str[i] == str[len - i - 1]; i++);
return i == len / 2;
}
I converted the number to String so I'd be able to go over the number as a char array:
private static boolean isPalindrom(long num) {
String numAsStr = String.valueOf(num);
char[] charArray = numAsStr.toCharArray();
int length = charArray.length;
for (int i = 0 ; i < length/2 ; ++i) {
if (charArray[i] != charArray[length - 1 - i]) return false;
}
return true;
}
I got correct answer for the problem, but I want to know is my coding style is good or bad from my solution. I need to know how can I improve my coding,if it is bad and more generic way.
#include<stdio.h>
#define MAX 999
#define START 100
int main()
{
int i,j,current,n,prev = 0;
for(i = START;i<=MAX;i++)
{
for(j=START;j<=MAX;j++)
{
current = j * i;
if(current > prev) /*check the current value so that if it is less need not go further*/
{
n = palindrome(current);
if (n == 1)
{
printf("The palindrome number is : %d\n",current);
prev = current; // previous value is updated if this the best possible value.
}
}
}
}
}
int palindrome(int num)
{
int a[6],temp;
temp = num;
/*We need a array to store each element*/
a[5] = temp % 10;
a[4] = (temp/10) %10;
a[3] = (temp/100) %10;
a[2] = (temp/1000) %10;
a[1] = (temp/10000) %10;
if(temp/100000 == 0)
{
a[0] = 0;
if(a[1] == a[5] && a[2] == a[4])
return 1;
}
else
{
a[0] = (temp/100000) %10;
if(a[0] == a[5] && a[1] == a[4] && a[2] == a[3])
return 1;
else
return 0;
}
}
No, the largest should be: 906609.
Here is my program:
def reverse_function(x):
return x[::-1]
def palindrome():
largest_num = max(i * x
for i in range(100, 1000)
for x in range(100, 1000)
if str(i * x) == reverse_function(str(i * x)))
return str(largest_num)
print(palindrome())

Resources