There are N boys standing in line in order 1 to n. For each ith boy (i from 1 to N) W[i] gives his weight. A boy in line can push another boy if his weight is more than or equal to the sum of weight of boys between them. We have to calculate for each boy, the number of boys he can get pushed by. My solution is taking too much time.
(1<=N<=10^5)
first line has N, second has weight of N boys in line.
For example:
INPUT:
5
2 5 8 10 12
OUTPUT:
2 3 3 2 1
My code:
class A {
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
long[] W=new long[n];
long[] sumtill_i=new long[n];
int[] pushedby=new int[n];
long sum=0;
st=new StringTokenizer(br.readLine());
for(int i=0;i<n;i++)
{
W[i]=Long.parseLong(st.nextToken());
sum+=W[i];
sumtill_i[i]=sum;
}
int j;
for(int i=0;i<n;i++)
{
j=n-1;
while(j>=0)
{
if(j>i)
{
if(W[j]>=(sumtill_i[j]-sumtill_i[i]-W[j]))
pushedby[i]++;
}
if(j<i)
{
if(W[j]>=(sumtill_i[i]-sumtill_i[j]-W[i]))
pushedby[i]++;
}
j--;
}
}
for(int i=0;i<n;i++)
System.out.print(pushedby[i]+" ");
}
}