Skip to main content

Command Palette

Search for a command to run...

Maximum Swap

Published
2 min read
Maximum Swap
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an integer num. You can swap two digits at most once to get the maximum valued number.

Return the maximum valued number you can get.

LeetCode Problem - 670

class Solution {
    public int maximumSwap(int num) {
        // Convert the number to a character array for easy manipulation
        char[] numCharArray = Integer.toString(num).toCharArray();

        boolean flag = true; // Flag to check if any swap has been made
        int idx = -1; // Index of the digit to swap with
        int maxNum = 0; // Maximum digit found for potential swap

        int answer = 0; // Variable to hold the final answer

        // Loop through each digit in the character array
        for(int i = 0; i < numCharArray.length; i++) {
            int currentNum = numCharArray[i] - '0'; // Get the current digit

            // Inner loop to find a larger digit to swap with
            for(int j = i + 1; j < numCharArray.length; j++) {
                int nextBigNum = numCharArray[j] - '0'; // Get the next digit

                // Check if the next digit is larger than the current digit
                // or if we are not in the first swap and it's equal or greater
                if(nextBigNum > currentNum || (!flag && nextBigNum >= currentNum)) {
                    currentNum = nextBigNum; // Update the current number
                    maxNum = nextBigNum; // Track the maximum digit found
                    idx = j; // Update the index of the larger digit
                    flag = false; // Set the flag to indicate a swap has been found
                }
            }

            // If a larger digit was found to swap
            if(!flag) {
                // Swap the digits
                char temp = numCharArray[i];
                numCharArray[i] = numCharArray[idx];
                numCharArray[idx] = temp;   

                // Convert the character array back to an integer
                answer = convertCharArrayToInt(numCharArray);
                break; // Break the loop as we only want the first valid swap
            }
        }

        // If no swap was made, return the original number
        if(flag) return num;
        return answer; // Return the new number after the swap
    }

    // Helper method to convert a character array back to an integer
    public int convertCharArrayToInt(char[] arr) {
        int num = 0; // Initialize the number to 0
        for(char ch : arr) {
            // Build the number by shifting left and adding the new digit
            num = num * 10 + (ch - '0');
        }
        return num; // Return the constructed integer
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.