top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Check if digit of a given number can be rearranged to form a palindrome?

0 votes
7,243 views
Check if digit of a given number can be rearranged to form a palindrome?
posted Nov 3, 2017 by Jordan White

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Try this code I tested in my system

 class Palindrome {
  private static boolean canMakeAPalindrome(int number) {
    int[] frequencyArr = createFrequencyArray(number);

    int oddCharCount = 0;

    for (int ch : frequencyArr) {

      // Count characters with odd occurrence.
      if (ch % 2 != 0)
        oddCharCount++;

      // If more than one character in the string has odd occurrence then
      // palindrome cannot be formed from given string
      if (oddCharCount > 1)
        return false;
    }

    return true;
  }

  private static int[] createFrequencyArray(int number) {
    int[] frequencyArr = new int[256];

    char[] charArray = String.valueOf(number).toCharArray();

    for (char ch : charArray)
      frequencyArr[ch]++;

    return frequencyArr;
  }

  public static void main(String[] args) {
      int number=1251256;

    System.out.println("Can palindrome be formed from '" + number(number));
  }

}
answer Nov 3, 2017 by Atindra Kumar Nath
O thanks fellas
Similar Questions
+1 vote

Given a singly linked list of integers, write a function in java that returns true if the given list is palindrome, else returns false

+3 votes

Rohit's teacher has asked him to write a function that takes as input parameters the first parameter will be an integer number representing. The number whose digitSum needs to be found the second parameter will be a char representing the
option which would either be 'e' or 'o' representing 'even' or 'odd' respectively.

Function signature public int digitSum(int n, char ch);

digitSum(9625, 'o')
Example 1: if given number is 9625 and the option is 'o' we must add only the odd digit i.e. 9+5=14
digitSum(2134, 'e')
Example 2: if given number is 2134 and the option  is 'e' we must add only the odd digit i.e. 2+4=6
+2 votes

Given 2 strings, a and b, return a string of the form shorterString+longerString+shorterString, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0). If input is "hi" and "hello", then output will be "hihellohi".

0 votes

C program to find and print second largest digit in the given number? (without using arrays, functions and using only one loop).

...