Javatpoint Logo
Javatpoint Logo

Largest Number After Digit Swaps By Parity

Problem Statement:

We are given a positive integer num. We may swap any two digits of num that have the same parity (i.e., both odd digits or both even digits). Return the largest possible value of num after any number of swaps.

Java Approach Using Brute Force

Output:

Largest Number After Digit Swaps By Parity

Code Explanation:

  • The LargestInteger class defines a method largestInteger to find the largest integer by rearranging the digits of the input number. It converts the integer to a string and then to a character array.
  • The algorithm uses a modified bubble sort to rearrange the digits in descending order based on their values and parity, ensuring that even digits precede odd ones. After sorting, it converts the character array back to a string and then to an integer.

Time Complexity:

  • The time complexity is O(n^2), where n is the number of digits in the input integer. This is due to the nested loop used for the modified bubble sort.

Space Complexity:

  • The space complexity is O(n), representing the space used for the character array to store the digits of the input integer. The algorithm uses a constant amount of extra space for other variables.

Java Approach Using Priority Queue

Output:

Largest Number After Digit Swaps By Parity

Code Explanation:

  • The Java code defines a LargestIntegerSolution class with a method largestInteger that finds the largest integer by rearranging the digits of the input number using two priority queues-one for odd and one for even digits. The algorithm iterates through the input number's digits, populating the priority queues based on the digit's parity.
  • It then constructs the result by alternately selecting digits from the odd and even queues. The result is built in a way that maximizes the value of the resulting integer.

Time Complexity:

  • O(n log n), where n is the integer input's number of digits, is the time complexity. The insertion and removal operations on the priority queues, which each have a logarithmic time complexity, are the source of this difficulty.

Space Complexity:

The space complexity is O(n), which is the amount of space that the priority queues utilize to hold the input integer's digits. In addition, linear space complexity arises from the constant utilization of more space for other variables.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news