#urgent help needed im so lost. I have to swap the lowestint to the front and highest int to the back
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
To swap the lowest integer to the front and the highest integer to the back while preserving the other values in the original array, you can follow these steps:
- Find the index of the lowest integer in the array.
- Find the index of the highest integer in the array.
- Swap the lowest integer with the element at index 0.
- Swap the highest integer with the element at index (array length - 1).
Here's an example implementation in Python:
def swap_lowest_highest(arr):
# Find index of lowest and highest integers
min_index = arr.index(min(arr))
max_index = arr.index(max(arr))
# Swap lowest integer to front
arr[0], arr[min_index] = arr[min_index], arr[0]
# Adjust max_index if it was affected by previous swap
if min_index == max_index:
max_index = 0
# Swap highest integer to back
arr[-1], arr[max_index] = arr[max_index], arr[-1]
return arr
# Example usage
original_array = [3, 7, 2, 9, 5]
result_array = swap_lowest_highest(original_array)
print(result_array)
Output:
[2, 7, 3, 5, 9]
This implementation swaps only the lowest and highest integers while preserving all other values in their original order.
Please share code
its all there but ill get a better pic
i cant find a way to delete smth from an array so im so confused
hi. can you please paste the code instead of doing screenshots
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
also
do not share everything you have on your source files
try to be precise on where you have issue
so we could help you fast
sure
for (int i = 0; i ‹ array. length; i++ )
{
if (min > array[i])
{
min = array[i]; it
//System.out.printIn(min);
if ( max < array[1])
{
max - arrayli];
//System.out.printin(max);
array [e] - min;
array[array. length - 1] - max;
//System.out .printin(max); // 13
//System.out.printin(min); // 2
for (int ct - 0; ct < array. length; ct++)
{
System.out.print(array [ct] + " ");
}
so at the bottom
it prints the original array with my modified min at the start and max at the end
but it doesnt remove the min and max values from the original list
can you write the array content like this : {1,2,3,4,..}
the size of the array is declared by user input and all the values inside the array
so its random each test case
alright. do you initialize min and max ?
yeah just above the code i posted im sorry i didnt include it
right
i don't think that is the problem here
if i understand correctly Jo gets the correct values to min and max, but does not swap them with the values that were at the positions before.
next, you need to look for minimum and maximum
no i have the min and max working
okay, next, you have to put min at 1st index and max at the end
array[0] = min;
array[array.length - 1] = max;
here you can see i print the min at the start and the max at the end but the original values remain in the list while i lose the others
i have that done as well yes
what is 'e' ?
its zero in my code not sure why its 0 there
then you need to not only record the min and max value, but also their index in the array.
and at the end set the values ate these positions to the values that were in the front/back
this is how it looks on my code
ah
yes im asking how i would do this ive been trying so many ways
ive created a dummy array and tried to = it to eachother etc but i cant get it to work
im just lost haha
seems the question wants your array sorted
just sort it
Arrays.sort()
we cant we have to switch them for some god knows reason otherwise it wont pass the test case
based on the "Expected answer" here, it looks it wants a sorted array
you need to remember the index of the smallest element like this:
int min = array[0];
int minindex = 0;
for (int i = 0; i ‹ array. length; i++ ) {
if (min > array[i]) {
min = array[i];
minindex = i;
}
}
yes but it wants you to swap them ive alrready tried to sort and got 0
hmm
please check the image that is in the post for more information
after you got the index, you can swap the elements with the help of a temporary variable:
int tmp = a;
a = b;
b = tmp;
Then you just have to do that for min and max
sorry do you mean to swap what with what?
the index of the min and the index of another part of the array?
you have to replace a and b with accesses to the array for the min index and the index 0
if you do that, you swap the values
you copy the value from one position to tmp
then you copy the value from the other position to the first position
then you copy the value form tmp to the second position
that would swap the two elements
for example
int[] array = {42,400,10};
int tmp = array[0];
array[0] = array[2];
array[2] = tmp;
// now array contains {10,400,42}
you can make that in a separate method for better readability
~~```java
static void swap(int[] array, int idx1, int idx2) {
int temp = array[idx1];
array[idx1] = array[idx2];
array[idx2] = temp;
}
👍
so im getting one number now!
but missing one stil
apologie for the screenshot i cant copy paste it ill have to type it out
yea, you have to do the same for the max too
since you also want to swap that one
oh wait
you just have a small bug in the last line of the picture
can you see the bug, @edgy pecan?
then think about what numbers are swapped here and in what order.
I’m lost haha
what is the highest number? what index does it have?
what is the lowes number? what index does it have?
now do the swapping on paper to understand what went wrong here.
public class MyTest {
public static void main(String[] args) {
int[] array = {10,2,4,5 };
int minIndex = 0;
int maxIndex = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] < array[minIndex]) {
minIndex = i;
}
if (array[i] > array[maxIndex]) {
maxIndex = i;
}
}
// Swap the minimum element with the first element
int temp = array[0];
array[0] = array[minIndex];
array[minIndex] = temp;
// Swap the maximum element with the last element
temp = array[array.length - 1];
array[array.length - 1] = array[maxIndex];
array[maxIndex] = temp;
// Print the modified array
System.out.println("Modified array:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
try this out
Because it’s index 0? The 10 I mean
yep, exactly because of that
if the max is at index 0 you have to do something differently, so an if might be good.
I’m running off to a train now so I won’t be able to do much more on it but ty for ur help
👍