Java program to find palindrome numbers in given range
Your temp is not incrementing by 1 in the loop.
// temp is initialized to start at first
while(temp != 0){
rem = temp % 10;
rev = rev * 10 + rem;
temp = temp / 10;
}
// at the end of this temp's value is entirely changed.
// above temp is incremented here, ideally temp at the start should be incremented. You can do this by initializing temp as i
temp++;
search palindrome number in a list of array. if there exist palindrome number in the list, return its size
Here is a pseudo code.
output = -1;
for (i = 0; i < list.length; i++){
num = list[i];
indices[] = \\ get all the indices which the "num" value appears, only include those indices that are greater than "i"
for (j = 0; j < indices.length; j++){
flag = true;
k = i;
for (l = indices[j]; l >= k; l--, k++){
if (list[k] != list[l]) {
flag = false;
break;
}
}
if (flag){
length = (indices[j] - i) + 1;
if (length != 1 && length > output) { // checking of length != 1 will exclude those palindromes of length 2
output = length;
}
}
}
}
return output;
Here is the full code.
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
int[] list = { 3, 5, 2, 2, 6, 3, 6, 3, 6, 3, 6, 2, 2, 1 };
System.out.print(palindrome(list));
}
public static int palindrome(int[] list) {
int output = -1;
for (int i = 0; i < list.length; i++) {
int num = list[i];
ArrayList<Integer> indices = getIndices(list, i, num);
for (int j = 0; j < indices.size(); j++) {
boolean flag = true;
int k = i;
for (int l = indices.get(j); l >= k; l--, k++) {
if (list[k] != list[l]) {
flag = false;
break;
}
}
if (flag) {
int length = (indices.get(j) - i) + 1;
if (length != 1 && length > output) {
output = length;
}
}
}
}
return output;
}
public static ArrayList<Integer> getIndices(int[] list, int start, int num) {
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = start + 1; i < list.length; i++) {
if (list[i] == num) {
result.add(i);
}
}
return result;
}
}
Palindrome Number
A little modified and tested code is here,
public boolean isPalindrome(int x)
{
int reverse = 0;
int remainder = 0;
int originalX = x;
while (x > 0)
{
remainder = x % 10;
reverse = reverse * 10 + remainder;
x = x / 10;
}
return (reverse == originalX);
}
Trying to find palindrome number
Because after your while loop ends, x will be 0
, you have to act on a copy instead
public boolean isPalindrome(int x) {
int num = x;
if(x<0 || x%10==0){
return false;
}
int rev = 0;
while(x!=0){
rev=(rev*10)+(x%10);
x/=10;
}
if(num==rev){
return true;
}
else{
return false;
}
}
Related Topics
Spring Boot API Call With Multiple @Requestparam
Deserialization With @Jsonsubtypes for No Value - Missing Property Error
Recyclerview Not Calling Oncreateviewholder or Onbindview
How to Link Feature and Step Definition in Cucumber
Spring Boot JPA - Onetomany Relationship Causes Infinite Loop
Codility Tape Equilibrium Getting Zero on Some Cases
Spring Security 401 Unauthorized Even With Permitall
In Activity.Oncreate(), Why Does Intent.Getextras() Sometimes Return Null
Javac: File Not Found: First.Java Usage: Javac <Options> <Source Files>
Multiple Queries Executed in Java in Single Statement
Regular Expression to Match a Backslash Followed by a Quote
Jackson Serializes a Zoneddatetime Wrongly in Spring Boot
Failed to Process Import Candidates for Configuration Class
How to Change the Background Color of a Textfield Without Changing the Border in Javafx