Largest number by rearranging digits of a given positive or negative number Last Updated : 24 Feb, 2022 Comments Improve Suggest changes Like Article Like Report Given an integer N(positive or negative), the task is to find the maximum number that can be formed using all of the digits of this number. Examples: Input: -38290367Output: -20336789Explanation: As there is need to use all the digits, 0 cannot be the first digit because it becomes redundant at first position. Input: 1203465Output: 6543210 Approach: The digits in a number will range from 0-9, so the idea is to create a hash array of size 10 and store the count of every digit in the hashed array that occurs in the number. Follow the steps mentioned below to solve the problem: Then first check if the number is positive or negativeIf the number is positive, then as explained in this article, traverse the hashed array from index 9 to 0 and calculate the number accordingly.But if the number is negative, traverse the array from 0-9, such that:There can be leading 0s. So, find the smallest non-zero digit presentInsert the smallest non-zero digit in the start, and then add all other digits from 0 to 9 in that order to the final answerThen multiply the number by -1 to make it negativeReturn the resultant number Below is the implementation of the above approach C++ // C++ program to implement the approach #include <bits/stdc++.h> using namespace std; // Function to print the maximum number long long printMaxNum(long long num) { // Initialising hash array int hash[10] = { 0 }; long long n = num < 0 ? num * -1 : num; long long ans = 0; while (n) { hash[n % 10]++; n = n / 10; } // If positive number if (num > 0) { for (int i = 9; i >= 0; i--) for (int j = 0; j < hash[i]; j++) ans = ans * 10 + i; } // If negative number else { // If 0 is present in the number if (hash[0] > 0) { for (int i = 1; i < 10; i++) if (hash[i] > 0) { ans = i; hash[i]--; break; } } for (int i = 0; i < 10; i++) for (int j = 0; j < hash[i]; j++) ans = ans * 10 + i; ans = ans * -1; } return ans; } // Driver code int main() { int N = -38290367; // Function call cout << printMaxNum(N); return 0; } Java // Java program to implement the approach class GFG { // Function to print the maximum number static long printMaxNum(long num) { // Initialising hash array int[] hash = new int[10]; for (int i = 0; i < 10; i++) { hash[i] = 0; } long n = num < 0 ? num * -1 : num; long ans = 0; while (n > 0) { hash[(int)(n % 10)] += 1; n = n / 10; } // If positive number if (num > 0) { for (int i = 9; i >= 0; i--) for (int j = 0; j < hash[i]; j++) ans = ans * 10 + i; } // If negative number else { // If 0 is present in the number if (hash[0] > 0) { for (int i = 1; i < 10; i++) if (hash[i] > 0) { ans = i; hash[i]--; break; } } for (int i = 0; i < 10; i++) for (int j = 0; j < hash[i]; j++) ans = ans * 10 + i; ans = ans * -1; } return ans; } // Driver code public static void main(String args[]) { int N = -38290367; // Function call System.out.println(printMaxNum(N)); } } // This code is contributed by gfgking Python3 # Python program to implement the approach # Function to print the maximum number def printMaxNum(num): # Initialising hash array hash = [] for i in range(0, 10): hash.append(0) if(num < 0): n = num * -1 else: n = num ans = 0 while (n != 0): hash[int(n % 10)] = hash[int(n % 10)] + 1 n = n // 10 # If positive number if (num > 0): for i in range(9, -1, -1): for j in range(0, hash[i]): ans = ans * 10 + i # If negative number else: # If 0 is present in the number if (hash[0] > 0): for i in range(1, 10): if (hash[i] > 0): ans = i hash[i] = hash[i]-1 break for i in range(0, 10): for j in range(0, hash[i]): ans = ans * 10 + i ans = ans * -1 return ans # Driver code N = -38290367 # Function call print(printMaxNum(N)) # This code is contributed by Taranpreet C# // C# program to implement the approach using System; class GFG { // Function to print the maximum number static long printMaxNum(long num) { // Initialising hash array int[] hash = new int[10]; for (int i = 0; i < 10; i++) { hash[i] = 0; } long n = num < 0 ? num * -1 : num; long ans = 0; while (n > 0) { hash[n % 10]++; n = n / 10; } // If positive number if (num > 0) { for (int i = 9; i >= 0; i--) for (int j = 0; j < hash[i]; j++) ans = ans * 10 + i; } // If negative number else { // If 0 is present in the number if (hash[0] > 0) { for (int i = 1; i < 10; i++) if (hash[i] > 0) { ans = i; hash[i]--; break; } } for (int i = 0; i < 10; i++) for (int j = 0; j < hash[i]; j++) ans = ans * 10 + i; ans = ans * -1; } return ans; } // Driver code public static void Main() { int N = -38290367; // Function call Console.Write(printMaxNum(N)); } } // This code is contributed by Samim Hossain Mondal. JavaScript <script> // javascript program to implement the approach // Function to print the maximum number function printMaxNum(num) { // Initialising hash array var hash = Array.from({length: 10}, (_, i) => 0); for (var i = 0; i < 10; i++) { hash[i] = 0; } var n = num < 0 ? num * -1 : num; var ans = 0; while (n > 0) { hash[parseInt(n % 10)] += 1; n = parseInt(n / 10); } // If positive number if (num > 0) { for (var i = 9; i >= 0; i--) for (var j = 0; j < hash[i]; j++) ans = ans * 10 + i; } // If negative number else { // If 0 is present in the number if (hash[0] > 0) { for (var i = 1; i < 10; i++) if (hash[i] > 0) { ans = i; hash[i]--; break; } } for (var i = 0; i < 10; i++) for (var j = 0; j < hash[i]; j++) ans = ans * 10 + i; ans = ans * -1; } return ans; } // Driver code var N = -38290367; // Function call document.write(printMaxNum(N)); // This code is contributed by shikhasingrajput </script> Output-20336789 Time Complexity: O( length(N) )Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Rearrange Array in negative numbers, zero and then positive numbers order C code_r Follow Improve Article Tags : Mathematical Combinatorial Geeks Premier League DSA Geeks-Premier-League-2022 Practice Tags : CombinatorialMathematical Similar Reads Smallest number by rearranging digits of a given number Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of a given number. Examples: Input: n = 846903Output: 304689Input: n = 55010Output: 10055Input: n = -40505Output: -55400Steps to find the smallest number. Count the frequency of each digit in the number.If i 7 min read Rearrange Array in negative numbers, zero and then positive numbers order Given an arr[ ] of size N, containing positive, negative integers and one zero. The task is to rearrange the array in such a way that all negative numbers are on the left of 0 and all positive numbers are on the right. Note: It is not mandatory to maintain the order of the numbers. If there are mult 6 min read Find the Largest number with given number of digits and sum of digits Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max v 13 min read Rearrange positive and negative numbers using inbuilt sort function Given an array of positive and negative numbers, arrange them such that all negative integers appear before all the positive integers in the array without using any additional data structure like a hash table, arrays, etc. The order of appearance should be maintained. Examples: Input : arr[] = [12, 15 min read Reorder digits of a given number to make it a power of 2 Given a positive integer N, the task is to rearrange the digits of the given integer such that the integer becomes a power of 2. If more than one solution exists, then print the smallest possible integer without leading 0. Otherwise, print -1. Examples: Input: N = 460 Output: 64 Explanation: 64 is a 12 min read Find maximum number that can be formed using digits of a given number Given a number, write a program to find a maximum number that can be formed using all of the digits of this number.Examples: Input : 38293367Output : 98763332Input : 1203465Output: 6543210Simple Approach: The simple method to solve this problem is to extract and store the digits of the given number 6 min read Like