package com.example.time.complexity;
/*Question:
* An integer array of 5 length has some values, while traversing
* the array multiply all other(except the current element's value)
* array elements values and place the result in the current array
* element
*
* ex: a[] = {11,23,35,41,2}
* first traverse a[0]=2x3x4x5
* second traverse a[1]=1x3x4x5
* third traverse a[2]=1x2x4x5
* fourth traverse a[3]=1x2x3x5
* fifth traverse a[4]=1x2x3x4
*/
public class ComplexityTest1 {
public static void main(String args[]){
int[] i = {11,23,35,41,2};
int[] res = new int[i.length];
int len =i.length;
int temp=1;
for(int x=0;x<len;x++){
if(x+1==len){
temp=1;
}else{
temp=i[x+1];
}
for(int y=0;y<len;y++){
if(x!=y && (x+1)!=y){
//System.out.println(x+"."+temp+"x"+i[y]+"="+(temp*i[y]));
temp = temp*i[y];
}
}
res[x]=temp;
}
for(int x=0;x<len;x++){
System.out.println(res[x]);
}
}
}
No comments:
Post a Comment