DEV Community

Discussion on: 6-10PM challenge problem #002

Collapse
 
mohamedaminedahwathi profile image
Mohamed amin Dahwathi

java Sol :

public class HelloWorld{

 public static void main(String []args){
    int[] arr=new int[]{2, 7, 3, 4};
    int[] rez=new int[arr.length];
    int som=1;
    for(int i=0;i<arr.length;i++){
        if(i==0){
            for(int j=i+1;j<arr.length;j++)
                som*=arr[j];
            rez[i]=som;
        }else{
            rez[i]=(som/arr[i])*arr[i-1];
            som=rez[i];
        }
    }

     for(int i=0;i<arr.length;i++)
         System.out.println(rez[i]);

 }
 }
Collapse
 
akbhairwal profile image
akbhairwal

you could optimize this solution for O(n)

Collapse
 
mohamedaminedahwathi profile image
Mohamed amin Dahwathi

hello,
i am new but it is O(n) right ? without the last loop ofc