completed max,middle element method