public static void main(String[] args) {
double [] boxes;
boxes = new double[] {20, 10, 5, 40, 20, 41, 41, 2, 6, 7, 3, 4, 5, 6, 23, 34, 7, 8, 2, 2};
double heaviest = 0;
double normal = 0;
double heavy = 0;
double totalCost;
double a = 0;
double b = 0;
int repeatCount=0;
for (int i = 1; i < boxes.length; i++) {
if (boxes[i] > heaviest)
heaviest = boxes[i];
}
for(double element: boxes) {
if(element==heaviest) {
repeatCount = repeatCount+1;
}
}
System.out.println("Count :" +repeatCount);
for (int j =0; j<boxes.length; j++) {
if (boxes[j] < heaviest) {
a = boxes[j] * 2;
normal = normal+a;
} else {
b = (boxes[j] * 3.5);
heavy = heavy+b;
}
}
totalCost = normal+heavy;
System.out.println("total cost of the insuranse is "+ totalCost);
}
Part 1: I need to multiply the largest element by 3.5
and rest with 2
and then add the value to get the total.
Part 2: Also I want the number of occurrences of the largest element. OR We can also store the largest element in another array.
In my code, I'm done with the part 1 and for part 2, I'm taking the count of the occurrences of the largest element.
My question:
Is there any other way to do this for reducing the compilation time or for the code to be more optimized if there are more than 1000
elements?
I have also tried using the Collections.frequency(myArray, largestElement)
by converting array to list.