Originally posted by Jyoti:
I think there is a problem in your for loop
it should be, for (int i=0; i<a;i++)
consider your array is {1,3,4}
So, According to your code this is happening :
1st iteration of for loop
i = 0
c = 0th element of the array i.e. 1
d= 1st element of the array i.e. 3
2nd iteration of for loop
i = 1
c = 1st element of the array i.e. 3
d= 2nd element of the array i.e. 4
After this i=2 which still satisfies your for loop condition , so one more iteration
3rd iteration of for loop
i = 2
c = 2nd element of the array i.e. 4
d= This tries to access 3rd element of the array , which is not there !!
thats why you are getting ArrayIndexOutofBound exception.
Hope this helps !!
Regards
Originally posted by Jyoti S:
How about this
[ March 10, 2008: Message edited by: Jyoti S ]
[ March 10, 2008: Message edited by: Jyoti S ]
Originally posted by Jyoti S:
How about this
no it does not work in all conditions.
rule is that we have to make sure {1,2,3} returns true but not {1,3,2}
public boolean scoresIncreasing(int[] scores) {
int a= scores.length;
boolean b = true;
for (int i=0; i<a;i++) { int c =scores[i];
int d= scores[i+1];
if (a>=2 && c>d)
{ b= false;
// If at any point in the array previous value is > then
// no need to check further just break for loop & return false
break;
}
}
return b;
}
- Marimuthu Madasamy
Originally posted by Marimuthu Madasamy:
your code is rocking really great, but can you find me where the mistake is in my code? just only one codition is faild from my code
and the codition faild was ({1, 1, 2, 4, 3, 7}) this
Originally posted by Marimuthu Madasamy:
hi i did not put break in my code that is why my one codition was not sucessful.
Don't get me started about those stupid light bulbs. |