Ich versuche, ein Problem zu lösen. Wir werden das maxProduct eines Arrays in O (n) finden, so dass keine doppelten for-Schleifen erlaubt sind, da es O (n²) wäre. Sie werden in meinem Code sehen, dass alle Elemente bis auf den ersten vervielfältigt sind und letzte Elemente. Wie kann ich das erste und letzte Element meines Arrays mit der Logik meines Codes multiplizieren?Multiplikation von Elementen in einem Array
hier ist mein Code:
public class Maxprod {
public static void main(String [] args){
Maxprod myclass = new Maxprod();
myclass.maxProduct();
}
public void maxProduct(){
int [] myarr = {4, -5, -7, 5};
int max = 0, p=0;
int q = 0;
for(int i=0; i <myarr.length-1; i++){
p = myarr[i]*myarr[i+1]; // 4 * 5 is missing here
if (p > max){
max = p;
}
}
System.out.println(max);
}
}