Hi I have reached to this solution . Can anyone suggest me a way where I can optimize it
public class Box {
public static void main(String[] args) throws Exception {
int N=5;
int[] M = {2,3,4,5,6};
int[] Q = {9,20};
int[] A = new int[M.length];
A[0] = M[0];
for(int i=1;i<M.length;i++) {
A[i] = A[i-1]+M[i];
}
for(int j=0;j<Q.length;j++) {
for(int k=1;k<=N;k++) {
if(Q[j]>A[N-1]) throw new Exception("Not Found");
if(Q[j]<=A[k-1]) {
System.out.println(k);
break;
}
if(Q[j]>A[k-1] && Q[j]<A[k]) {
System.out.println(k+1);
break;
}
if(Q[j]>A[k] && Q[j]<=A[k+1]) {
System.out.println(k+2);
break;
}
}
}
}}
