MIDDLE ELEMENT

If I have an array of 4 elements starting from 1 index then I want to print middle element then why it will print 2 why not 3rd index
Ex. (1,2,3,4)
Y-2 why not 3
Cout<<(arr[n/2])<<endl;

I know you might be saying 2/4=2 so it print 2nd element logically it is crct by practically I dont I assume it is not can anyone explain it I cant even find in google

If we have even number of elements we will have to print the middle two elements similarly in odd number of indexes we will have to print (n+1)/2 th element

eg : 1 2 3 4 5 ,here (5+1)/2 =3 is the middle term

1 2 3 4 , here {n/2=2} and {n/2+1=3} are the middle terms.

Thanks it was my first answer here at CodeChef

1 Like

Thx bro silly question but got confused😅

Welcome

1 Like