user2991964
user2991964

Reputation: 11

find highest and 2nd highest digit in a number

After finding the highest digit in a number,how to find the 2nd highest digit in that same number using only loops and if statements?

    public static int maximum(int max){

while(num != 0){
            int rightDigit = num % 10;
            num /= 10;
            if(rightDigit > max)
                rightDigit = max;
        }
        return max;
        }

Upvotes: 1

Views: 17763

Answers (7)

solanki dev
solanki dev

Reputation: 27

static void Main(string[] args)
{




    int max = 0, temp = 0, secondMax = 0, number = 0;

    number = 6541891;
    while (number != 0)
    {
        temp = number % 10;

        if (max == 0)
        {

            max = temp;
            secondMax = temp;

        }
        else if (temp > max)
        {
            int lastmax = max;

            max = temp;

            if (lastmax > secondMax)
            {
                secondMax = lastmax;
            }


        }

        if ((temp > secondMax && temp < max) || secondMax >= max)
        {
            secondMax = temp;
        }

        number = number / 10;
    }

    int Result = secondMax;

}

Upvotes: 0

saran3h
saran3h

Reputation: 14022

    public static int nthHighest(int[] arr, int n) {
         List<Integer> lst = Arrays.asList(ArrayUtils.toObject(arr)); //use apache commons library
         Collections.sort(lst);
         return lst.get(arr.length-n);
    }

Upvotes: -1

aga
aga

Reputation: 29416

Sorting the list of number's digits and getting the 1st and 2nd biggest digits will give you at best O(n * log n) time complexity (assuming you will use Quick Sort).
You can achieve somewhat better performance if you will use another approach: partition (reorder) your array (as in quick sort), so you'll have a pivot value which divides your array in two parts: those which are less than pivot are in the left part (left sub-array), those which are bigger are in the right part (right sub-array). Check the index of the pivot:

  • if it's equal to the size of an array of digits minus 2, than it's the second biggest element (the first biggest is next to it, in the right sub-array);
  • if the index of pivot is less than size of an array of digits minus 2, repeat the partitioning for the right sub-array;
  • if the index of pivot is bigger than size of an array of digits minus 2, repeat the partitioning for the left sub-array;

At some point your pivot will be the the second element from the end of an array, which means that it's the second biggest element, and the biggest number is in the end of an array (because of the way you get the pivot). The time complexity will be better than for a quick sort, because after each partition you're partitioning only one sub-array, not both of them.

You can extend this approach to get not only the 1-st and 2-nd largest digits, but k-th (arbitrary highest) digit, and not only largest, but smallest also.

Check out the piece of code I've written couple of days ago:

public Long selectKthElement(int left, int right, int k, Type type) {
    int med = partitionIt(left, right);

    if ((type.equals(Type.Smallest) && med == k - 1) || (type.equals(Type.Largest) && med == nElems - k)) {
        return theArray[med];
    } else if (type.equals(Type.Smallest) && med > k || type.equals(Type.Largest) && med > nElems - k) {
        return selectKthElement(left, med - 1, k, type);
    } else if (type.equals(Type.Smallest) && med < k || type.equals(Type.Largest) && med < nElems - k){
        return selectKthElement(med + 1, right, k, type);
    } else {
        // impossible case, but the source code won't compile w/o the else
        return null;
    }
}

Here theArray is an array of numbers' digits, partitionIt method reorders an array and returns the index of median, you can either figure out how to write the implementation of it yourself, or search through the web.

Upvotes: 0

Alexey Odintsov
Alexey Odintsov

Reputation: 1715

int num = 1395248, n, i, n2;
for (n2 = i = n = 0; num > 0; i = num % 10, n2 = n < i ? n : n2, n = n < i ? i : n, num /= 10);
System.out.println(n);
System.out.println(n2);

Upvotes: 0

knordbo
knordbo

Reputation: 552

Is this what you want? first element in array is largest, second element is second largest. Returns -1 if no such element.

public static void main(String[] args) {
    int[] tab = maximum(12);
    System.out.println("Largest digit: " + tab[0]);
    System.out.println("Second largest digit: " + tab[1]);
}
public static int[] maximum(int max){
    int num = max;
    int largest = -1;
    int secondLargest = -1;
    while(num != 0){
        int rightDigit = num % 10;
        num /= 10;

        if(rightDigit > largest) {
            secondLargest = Math.max(secondLargest, largest);
            largest = rightDigit;

        } else if(rightDigit > secondLargest)
            secondLargest = rightDigit;
    }
    return new int[]{largest,secondLargest};
    }

Upvotes: 0

Kalai.G
Kalai.G

Reputation: 1610

Assume maxValue is the highest one and you can easily identify the second highest

   if (times[i] > maxValue) {
        secondhighest  = maxValue;
        maxValue = times[i];
    } else if (times[i] > secondhighest) {
        secondhighest  = times[i];
    }

Upvotes: 0

LionC
LionC

Reputation: 3106

Use a List to store all digits and sort it, that way you have access to the highest, second highest to lowest digit as you wish. To sort a List use Collections.sort(List list)

Upvotes: 3

Related Questions