Assume you have a array of integers. You want to organize it such that all numbe
ID: 3832317 • Letter: A
Question
Assume you have a array of integers. You want to organize it such that all numbers that are smaller than -100 go to the bottom, numbers between -100 and +100 in the middle and numbers larger than 100 in the top.
Write a Java code that uses no more than three additional Array to solve the problem.
NB: You do not need to write the code for Array, you are using a Array from the library with a name MAC286Array that exposes the following interface: MAC286Array() constructor, pop, push, isEmpty, size(), peek()).
Explanation / Answer
import java.io.*;
class Arrange {
public static void main(String[] args){
MAC286Array arr1 = new MAC286Array(); // Assuming arr1 is the input array that has to be arranged
MAC286Array arr2 = new MAC286Array();
MAC286Array arr3 = new MAC286Array();
MAC286Array arr4 = new MAC286Array();
int data;
arr1.push(-101);
arr1.push(-89);
arr1.push(102);
arr1.push(90);
arr1.push(-102);
while (!arr1.IsEmpty()){
data = arr1.pop();
if (data < -100)
arr2.push(data);
if (data > -100 && data < 100)
arr3.push(data);
if (data > 100)
arr4.push(data);
}
while (!arr2.IsEmpty()){
arr1.push(arr2.pop());
}
while (!arr3.IsEmpty()){
arr1.push(arr3.pop());
}
while (!arr4.IsEmpty()){ //After this loop arr1 will be arranged as required
arr1.push(arr4.pop());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.