+ 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)); } } } }
4 Respostas
+ 11
You don't increment i in the while loop > infinite loop.
+ 10
I refactored the whole code:
https://code.sololearn.com/cQ15M3CNpP3W/?ref=app
Hope this helps...
+ 2
OK Tashi.. so if I remove it.. it returns an error. what shud I put instead?
+ 1
it's supposed to provide the index of the max number