Given input integer array arr, arrange the array in wave form, ie element at index idx should be greater than element at index idx-1 and index idx+1.
| Input Parameters |
Expected outputs |
| arr={1,2,3,4,5,6,7}
|
1,3,2,5,4,7,6
|
| arr={ 0,-1,-2,-3,-4}
|
-1,0,-3,-2,-4
|
| arr={-33,-59,-4,0,77,21,67}
|
-59,-4,-33,77,0,67,21
|
| arr={ 5,4,7}
|
4,7,5
|
| The java method should accept following input parameters: arr (int array).
|
| Iterate through arr using a for loop and loop variable idx. Start the for loop from index 1 and increment idx by 2 for each iteration:
If arr[idx-1] > arr[idx], swap the same.
Check that idx+1 should be < arr.length. If arr[idx+1] > arr[idx], swap the same.
|
| After above loop completes, array will be in wave form.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayWaveForm {
public int[] arrayArrayWaveForm(int[] arr) throws Exception{
int temp=0;
for (int idx=1; idx < arr.length; idx+=2) {
if (arr[idx-1] > arr[idx]) {
temp=arr[idx];
arr[idx]=arr[idx-1];
arr[idx-1]=temp;
}
if ((idx+1 < arr.length)&&(arr[idx] < arr [idx+1])) {
temp=arr[idx];
arr[idx]=arr[idx+1];
arr[idx+1]=temp;
}
}
return arr;
}
public static void main(String[] args) {
ArrayWaveForm ap=new ArrayWaveForm();
int[] arr;
try {
int[] intArray1 = {1,2,3,4,5,6,7};
printArraySummary(intArray1, "Original Array");
arr=ap.arrayArrayWaveForm(intArray1);
printArraySummary(intArray1, "Waveform Array");
int[] intArray2 = {0, -1,-2,-3,-4,};
printArraySummary(intArray2, "Original Array");
arr=ap.arrayArrayWaveForm(intArray2);
printArraySummary(intArray2, "Waveform Array");
int[] intArray3 = {-33,-59,-4,0,77,21,67};
printArraySummary(intArray3, "Original Array");
arr=ap.arrayArrayWaveForm(intArray3);
printArraySummary(intArray3, "Waveform Array");
int[] intArray4 = {5,4,7};
printArraySummary(intArray4, "Original Array");
arr=ap.arrayArrayWaveForm(intArray4);
printArraySummary(intArray4, "Waveform Array");
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}