+ 3
Trace the program
Explain flow of execution for me https://code.sololearn.com/cDbxB95K7VBf/?ref=app
4 Respostas
+ 4
public class Program
{
public static void main(String[] args) {
int n[]={1,2,5,1,2,3,4,2,4,1};
int occ[]=new int[6];
//initially occ array , all set to 0(defaultly) value after initialzation
for(int i=0;i<n.length;i++) //i=0 to n.length
++occ[n[i]];
//for all iterations :
/* i=0 => n[0] => 1
occ[1]=0
++occ[1] =>occ[1]=1
i=1 => n[1] => 2
occ[2]=0
++occ[2] =>occ[2]=1
i=2 => n[2] => 5
occ[5]=0
++occ[5] =>occ[5]=1
i=3 => n[3] => 1
occ[1]=1
++occ[1] =>occ[1]=2
i=4 => n[4] => 2
occ[2]=1
++occ[2] =>occ[2]=2
i=5 => n[5] => 3
occ[3]=0
++occ[3] =>occ[3]=1
i=6 => n[6] => 4
occ[4]=0
++occ[4] =>occ[4]=1
+ 4
i=6 => n[6] => 4
occ[4]=0
++occ[4] =>occ[4]=1
i=7 => n[7] => 2
occ[2]=2
++occ[2] =>occ[2]=3
i=8 => n[8] => 4
occ[4]=1
++occ[4] =>occ[4]=2
i=9 => n[9] => 1
occ[1]=2
++occ[1] =>occ[1]=3
*/
System.out.print(occ[1]); //3
System.out.print(occ[4]); //2
}
}
hope it helps....
+ 3
Thanks Jayakrishna🇮🇳 i got confused with incrementation of array values & we need to figure all this during a SoloLearn challenge within 30 sec 😂
+ 2
Good question for competitive exams.. tough as well. The logic is to find occ[] values at indexes 1 and 4 are incrementing as the number of times it's in array n. [ 1 is 3 times, 4 is 2 times..] You're welcome Adithya Keshav T