+ 2

Why is it when this code runs it sez time limit exceeded?

public class Program { public static int Findmax(int[]arr,int num){ int i=0; while(i<arr.length){ if(arr[i]==num) { return (i) ; } } return (-1); } public static void main(String[] args) { int[]myarr={25,9,5,7,63,58,18,35}; int max=0; for(int i=1;i<myarr.length;i++){ if(myarr[i]>max) { max=myarr[i] ; System.out.println ("Maximum number is at index "+ Findmax (myarr,max)); } } } }

20th Mar 2017, 4:24 PM
Ricardo Chitagu
Ricardo Chitagu - avatar
4 Respostas
+ 11
You don't increment i in the while loop > infinite loop.
20th Mar 2017, 4:29 PM
Tashi N
Tashi N - avatar
+ 10
I refactored the whole code: https://code.sololearn.com/cQ15M3CNpP3W/?ref=app Hope this helps...
20th Mar 2017, 5:18 PM
Tashi N
Tashi N - avatar
+ 2
OK Tashi.. so if I remove it.. it returns an error. what shud I put instead?
20th Mar 2017, 4:42 PM
Ricardo Chitagu
Ricardo Chitagu - avatar
+ 1
it's supposed to provide the index of the max number
20th Mar 2017, 4:25 PM
Ricardo Chitagu
Ricardo Chitagu - avatar