Skip to content
Advertisement

Challenge: Sorting Arrays [closed]

I need a little help, I made a code to read a sequence of numbers, store them in an array and then print the even ones and then odd ones, but the test has a catch where I need to change the position of two values to pass 5 and 3:

Input data: 13452

Expected output: 42153

My output: 42135

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    
    int N;
    N = input.nextInt();
    int nums[] = new int[N];

    for (int i = 0; i < N; i++) {
        nums[i] = input.nextInt();
    }
    for (int i = 0; i < N; i++) {
        if (nums[i] % 2 == 0) {
            System.out.print(nums[i]);
            System.out.println();
        }
    }
    
    for (int i = 0; i < N; i++) {
        if (nums[i] % 2 != 0) {
            System.out.print(nums[i]);
            System.out.println();
        }
    }
}

Advertisement

Answer

What isn’t clear, except through what is considered your example of valid output, is when should the value of 3 and 5 (or 5 and 3) be swapped, before Even & Odd is determined or after Even & Odd is determined. Your example indicates after the Even & Odd are determined. Whichever it truly is, it’s just a matter of placing the swapping algorithm either before or after the Even/Odd algorithm.

The key here is to keep everything as an array throughout the Even/Odd algorithm instead of printing results. This can be done in a different int[] Array which can be copied over the existing array when the Even/Odd process is done using the Arrays#copyOf() method (or do it the long way). Why keep it all as an array? So that swapping can be done at an index level (swap by index). Swap by value can be done but there is a lot more code required since you only want to swap the first two Swap values encountered and not all of them. Here is an example:

Scanner input = new Scanner(System.in);

int n;
System.out.print("Enter length of Array: -> ");
n = input.nextInt();

int swapA = 3;   // (or 5)
int swapB = 5;   // (or 3)

int atIndexA = -1;  // Used for swapping
int atIndexB = -1;  // Used for swapping
int tmp = -1;       // Used for swapping 
    
// Create and fill Array
int nums[] = new int[n];
for (int i = 0; i < n; i++) {
    System.out.print("Enter number #" + (i+1) + ": -> ");
    nums[i] = input.nextInt();
}
System.out.println("Original Entry:  " + Arrays.toString(nums));
    
/* Get the even and odd values:
   Even numbers are placed into a temporary 
   array first and then the Odd numbers.
EVEN Numbers:                         */          
int[] tmpArray = new int[n];
int indexCounter = 0;
    
for (int i = 0; i < n; i++) {
    if (nums[i] % 2 == 0) {
        tmpArray[indexCounter] = nums[i];
        indexCounter++;
    }
}
// ODD Numbers:
for (int i = 0; i < n; i++) {
    if (nums[i] % 2 != 0) {
        tmpArray[indexCounter] = nums[i];
        indexCounter++;
    }
}
    
// Overwrite the nums[] array with tmpArray[]:
nums = Arrays.copyOf(tmpArray, n);
System.out.println("Evens & Odds:    " + Arrays.toString(nums));
    
/* Do the required swap (if the two digits exist).
   Get the index values of the first two swap values. */
for (int i = 0; i < n; i++) {
    if (nums[i] == swapA && atIndexA == -1) {
        atIndexA = i;
    } 
    else if (nums[i] == swapB && atIndexB == -1) {
        atIndexB = i;
    }
}
/* If either atIndexA or atIndexB equals -1 then one
   or both of the swap values do not exit within the 
   int[] Array.                 */
if (atIndexA != -1 && atIndexB != -1) {
    tmp = nums[atIndexA];
    nums[atIndexA] = nums[atIndexB];
    nums[atIndexB] = tmp;
}
    
/* You can delete this...It just indicates which swap 
   value(s) is not conatined within the int[] Array. */
if (atIndexA == -1) {
        System.out.println("Swap Value '" + swapA + "' is not within the Array!");
    }
    if (atIndexB == -1) {
        System.out.println("Swap Value '" + swapB + "' is not within the Array!");
}
        
// Display the overall Result.
System.out.println("Numbers Swapped: " + Arrays.toString(nums));   

Here is what your Console Window could display:

Enter length of Array: -> 5
Enter number #1: -> 1
Enter number #2: -> 3
Enter number #3: -> 4
Enter number #4: -> 5
Enter number #5: -> 2
Original Entry:  [1, 3, 4, 5, 2]
Evens & Odds:    [4, 2, 1, 3, 5]
Numbers Swapped: [4, 2, 1, 5, 3]
2 People found this is helpful
Advertisement