Home > Back-end >  we have array of 3 digit numbers sort only the middle digit of the array of numbers for eg:i/p =540,
we have array of 3 digit numbers sort only the middle digit of the array of numbers for eg:i/p =540,

Time:01-19

we have an array of 3 digit numbers sort only the middle digit of the arrayof numbers I/p=540,984,902 O/p=500,944,982 and the middle number is change by using one array

I tried only the swap options .i am excepting by using one array

CodePudding user response:

Something like this ?

int[] numbers = new int[]{540, 984, 902};// or however many 3-digit numbers

int[] mids = new int[numbers.length];
for (int i = 0; i < numbers.length; i  ) {
  mids[i] = (numbers[i] / 10) % 10;
}

Arrays.sort(mids);

for (int i = 0; i < numbers.length; i  ) {
  numbers[i] = (numbers[i] / 100) * 100   mids[i] * 10   numbers[i] % 10;
  System.out.println("a["   i   "] "   numbers[i]);
}
  • Related