P
P
Pavel Subbotin2021-05-03 14:47:21
Java
Pavel Subbotin, 2021-05-03 14:47:21

How to make an array with the best players?

Guys, help with one thing. An array with the best players is given (there are 5 values ​​in total), then the number of the player who completed the game is given, and you need to write this number into the array so that all numbers move.
Example:
Given an array: [3, 8, 9, 10, 20]
And given for example the number 5
And you need the array to change to this [3, 5 , 8, 9, 10]
PS I hope I explained everything clearly

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jacen11, 2021-05-03
@Jacen11

use TreeSet for example

M
Mimocodil, 2021-05-04
@Ezekiel4

By looping through the array, you need to find out the insertion point. Having found it, simply insert a number and change all subsequent elements of the array to the previous ones one by one. I hope I explained clearly.

int[] insertToIntArr(int[] arr, int num) {
    for (int i = 0; i < arr.length; i++)
        if (arr[i] > num)
            arr[i] = arr[i] + num - (num = arr[i]);
    return arr;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question