how to swap the first digit(if not largest among the digits) with largest digit in an integer

inevitableKris

EDIT : I am unable to keep track of the second largest digit in an integer and swap that with the first digit in the integer to make the integer the largest... (For Example if i have my input as 31548 i am unable to swap 3 with 8 as my code swaps 3 with 5 and gives 51348 and again it runs through the rest and then swaps with 8 and gives 81345 which i don't want...) and For sequences of digits with the highest digit already in the leftmost position like if my input is 877115755 the output should ignore the first three digits 877 and consider the smaller fourth digit 1 and swap the next occurence of 7 and output 877715155.

public static void main(String[] args) {

    int value = 621007349;

    String number = String.valueOf(value);

    int[] arr = new int[number.length()];

    int length = (int) (Math.log10(value) + 1);

    for (int i = 0; i < length; i++) {
        System.out.println("Char at " + number.charAt(i));
        arr[i] = number.charAt(i) - '0';
    }

    int var = 0;
    for (int i = 0; i < arr.length; i++) {
        int temp = arr[i];
        for (int j = i; j < arr.length-1; j++) {
            if (j < arr.length - 1) {
                if (arr[j + 1] > temp) {
                    if (var < arr[j + 1]) {
                        var = arr[j + 1];
                        System.out.println("Larg Val " + arr[j+1]);
                        System.out.println("First no "+arr[i]);

                        int tmp = arr[j+1];
                        arr[j+1] = arr[i];
                        arr[i] = tmp;
                    }
                }
            }
        }
    }

    for (int j = 0; j < arr.length; j++) {
        System.out.println("Values in arry are " + arr[j]);
    }
}

Input is 621007349 and Output should be 921007346

Please help me on where did i go wrong...

Lrrr

EDIT : Add a while loop to for replacing first possible number with the maximum number after that:

I remove the inner loop and change conditions a little(also remove swap from inside the loop to after loop) and now your code works fine:

public static void main (String[] args) throws java.lang.Exception
{
    int value = 877115755;

    String number = String.valueOf(value);

    int[] arr = new int[number.length()];

    int length = (int) (Math.log10(value) + 1);
    for (int i = 0; i < length; i++) {
        arr[i] = number.charAt(i) - '0';
    }
    int var = arr[0];
    int index = 0;
    int startIndex = 0;
    boolean changed = false;

    while(startIndex<arr.length-1){
        var = arr[startIndex];
        for (int i = startIndex+1; i < arr.length; i++) {
            int temp = arr[i];
            if (temp > var) {
                var = temp;
                index = i;
                changed = true;
                System.out.println("Larg Val "+arr[i]);
            }
        }
        if(changed)
            break;
        startIndex++;
    }

    //this is the swap part
    int tmp = arr[index];
    arr[index] = arr[startIndex];
    arr[startIndex] = tmp;  

    for (int j = 0; j < arr.length; j++) {
        System.out.println("Values in arry are " + arr[j]);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding the largest even digit in a given integer

From Java

largest number with each digit only swap at-most K times and only adjacent swap

From Dev

How to write a Regular Expression for integer 14 digits fix and first digit can be 1 or 2

From Dev

Find how many times the largest digit appears in an array

From Dev

Sort Map<String, Integer> Largest First Into List<String> Efficiently?

From Dev

Swap largest and last elements in array

From Dev

type 'int' error in python. largest sum of (len) digits in integer n function

From Dev

How do I find the largest integer less than x?

From Dev

How to find occurrences of the largest integer in C without array?

From Dev

How to return the largest integer in an Array that has 10 random integers in it?

From Dev

How to return string attached to largest integer in an array (Javascript)

From Dev

Get largest unsigned integer type

From Dev

Bitwise shift and the largest integer in Bash

From Dev

Largest integer in node.js

From Dev

How to set width of several views to the largest content width among them? (dynamically)

From Java

function to get second largest digit from a string

From Dev

Largest palindrome product of two 3 digit numbers

From Dev

Largest Possible Number after positing digit

From Dev

Largest Possible Number after positing digit

From Dev

Python searching for largest digit in a string of letters and numbers

From Dev

third largest and smallest number for pairwise swap of number

From Dev

How to swap first and last digit in number in c++

From Dev

How to swap first and last digit in number in c++

From Dev

How to locate largest directories

From Dev

How to find largest folder?

From Dev

How to find the largest contour?

From Dev

find the vector with the largest size among group of vectors c++

From Dev

Find files recursively, but choose largest from among those with duplicate names

From Dev

Largest one among specific strings having multiple dots

Related Related

  1. 1

    Finding the largest even digit in a given integer

  2. 2

    largest number with each digit only swap at-most K times and only adjacent swap

  3. 3

    How to write a Regular Expression for integer 14 digits fix and first digit can be 1 or 2

  4. 4

    Find how many times the largest digit appears in an array

  5. 5

    Sort Map<String, Integer> Largest First Into List<String> Efficiently?

  6. 6

    Swap largest and last elements in array

  7. 7

    type 'int' error in python. largest sum of (len) digits in integer n function

  8. 8

    How do I find the largest integer less than x?

  9. 9

    How to find occurrences of the largest integer in C without array?

  10. 10

    How to return the largest integer in an Array that has 10 random integers in it?

  11. 11

    How to return string attached to largest integer in an array (Javascript)

  12. 12

    Get largest unsigned integer type

  13. 13

    Bitwise shift and the largest integer in Bash

  14. 14

    Largest integer in node.js

  15. 15

    How to set width of several views to the largest content width among them? (dynamically)

  16. 16

    function to get second largest digit from a string

  17. 17

    Largest palindrome product of two 3 digit numbers

  18. 18

    Largest Possible Number after positing digit

  19. 19

    Largest Possible Number after positing digit

  20. 20

    Python searching for largest digit in a string of letters and numbers

  21. 21

    third largest and smallest number for pairwise swap of number

  22. 22

    How to swap first and last digit in number in c++

  23. 23

    How to swap first and last digit in number in c++

  24. 24

    How to locate largest directories

  25. 25

    How to find largest folder?

  26. 26

    How to find the largest contour?

  27. 27

    find the vector with the largest size among group of vectors c++

  28. 28

    Find files recursively, but choose largest from among those with duplicate names

  29. 29

    Largest one among specific strings having multiple dots

HotTag

Archive