id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-arrays > apcsaArraysSwapPairElementsWhenDisordered
prev  |  next  |  chance

This function swaps the even-index values of the array with their odd-index pairs, but only when the earlier value is greater than the later value. Thus, the element at index 0 is (potentially) swapped with the element at index 1, the element with index 2 is (potentially) swapped with the element at index 3, and so on. Note that this function returns a whole array, not just an int. Precondition: the input array's length will be an even number.


apcsaArraysSwapPairElementsWhenDisordered([3, 4]) → [3, 4]
apcsaArraysSwapPairElementsWhenDisordered([8, 5, 3, 4, 5, 1]) → [5, 8, 3, 4, 1, 5]
apcsaArraysSwapPairElementsWhenDisordered([9, 2, 4, 6]) → [2, 9, 4, 6]

...Save, Compile, Run (ctrl-enter)

public int[] apcsaArraysSwapPairElementsWhenDisordered(int[] nums) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 250

Copyright Nick Parlante 2017 - privacy