Why it gives an error?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why it gives an error??

import java.util.Arrays; import java.util.Scanner; class Main{ static int x,p,s,k; static int[]arr=new int[3]; public static int f1(int arrsize, int i,int p){ ++k; int u=0; s=u; for(;p<arrsize;){ x=arr[p]; } for(int y=0;y<arrsize;y++){ if(x=arr[y]){ ++u; } if(y==arrsize-1){ f1(arrsize,i,++p); } } if(s<u){ s=u; } if(k==arrsize){ return s; } } public static void main(String args[]){ int ans; Scanner cout=new Scanner(System.io); int arrsize=cout.nextint(); for(int i=0;i<arrsize;i++){ arr[i]=i; } for(int i=0;i<=0;i++){ ans=f1(arrsize,i,p); } } System.out.println(arr[ans]); }

2nd Sep 2021, 4:32 PM
Shruti Shukla
Shruti Shukla - avatar
1 Answer
+ 6
Shruti Shukla You have many mistakes, rewrite your code in Playground and share here if you have any problem. //1 - there is System.in not System.io //2 - method f1 should have return statement outside the condition because it is not necessary that your condition will execute //2 - there is nextInt() not nextint(), i is in caps //4 - here if(x=arr[y]) should be ==, single equal used for assignment not comparison //5 - IndexOutOfBoundException on this line for(int i=0;i<arrsize;i++) because you have defined size 3 but input value can be more than 3 //6 - this line is wrong for(int i=0;i<=0;i++) check condition i <= 0 I think this should not be //7 - System.out.println(arr[ans]) is outside the method which is wrong. I already told you last time. //8 - int ans must be initialise To make better readability write code like this: https://code.sololearn.com/cssRgLVyCDqy/?ref=app There is execution timeout because logic is running till infinite because of logic problem. So check again.
2nd Sep 2021, 5:27 PM
A͢J
A͢J - avatar