Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

GeeksforGeeks
A computer science portal for geeks Home Q&A Interview Corner Ask a question Feedback Contribute About us Subscribe Arrays Articles Bit Magic C/C++ Puzzles GFacts Linked Lists MCQ Misc Output Strings Trees

Find the maximum element in an array which is first increasing and then decreasing
January 14, 2012 Given an array of integers which is initially increasing and then decreasing, find the maximum value in the array.
Ipt ar]={,1,2,8,10 20 40 50 3 2 1 nu: r[ 8 0 0 0 0, 0, 0, 0, , , } Otu:50 upt 0 Ipt ar]={,3 5,1,9 7 6 nu: r[ 1 , 0 0 , , } Otu:5 upt 0 Cre cs (odcesn pr) onr ae N eraig at Ipt ar]={0 2,3,4,5} nu: r[ 1, 0 0 0 0 Otu:5 upt 0
www.geeksforgeeks.org/archives/17028 1/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

Cre cs (oicesn pr) onr ae N nraig at Ipt ar]={2,10 8,2,0 nu: r[ 10 0, 0 0 } Otu:10 upt 2

Method 1 (Linear Search) We can traverse the array and keep track of maximum and element. And finally return the maximum element. #nld <ti.> icue sdoh itfnMxmmitar] itlw ithg) n idaiu(n r[, n o, n ih { itmx=arlw; n a r[o] iti n ; fr( =lw i< hg;i+ o i o; = ih +) { i (r[]>mx f ari a) mx=ari; a r[] } rtr mx eun a; } / Die pormt cekaoefntos* * rvr rga o hc bv ucin / itmi( n an) { itar]={,3,4,5,6,7,2,2} n r[ 1 0 0 0 0 0 3 0; itn=szo(r)szo(r[]; n iefar/iefar0) pit(Temxmmeeeti %" fnMxmmar 0 n1) rnf"h aiu lmn s d, idaiu(r, , -); gthr) eca(; rtr 0 eun ; } Time Complexity: O(n)

Method 2 (Binary Search) We can modify the standard Binary Search algorithm for the given type of arrays. i) If the mid element is greater than both of its adjacent elements, then mid is the maximum. ii) If mid element is greater than its next element and smaller than the previous element then maximum lies on left side of mid. Example array: {3, 50, 10, 9, 7, 6} iii) If mid element is smaller than its next element and greater than the previous element then maximum lies on right side of mid. Example array: {2, 4, 6, 8, 10, 3, 1} #nld <ti.> icue sdoh
www.geeksforgeeks.org/archives/17028 2/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

itfnMxmmitar] itlw ithg) n idaiu(n r[, n o, n ih { / Bs Cs:Ol oeeeeti peeti arlw.ih* * ae ae ny n lmn s rsn n r[o.hg]/ i (o = hg) f lw = ih rtr arlw; eun r[o] / I teeaetoeeet adfrti getrte * f hr r w lmns n is s rae hn tefrteeeti mxmm* h is lmn s aiu / i (hg = lw+1 & arlw > arhg] f (ih = o ) & r[o] = r[ih) rtr arlw; eun r[o] / I teeaetoeeet adscn i getrte * f hr r w lmns n eod s rae hn tescn eeeti mxmm* h eod lmn s aiu / i (hg = lw+1 & arlw <arhg] f (ih = o ) & r[o] r[ih) rtr arhg] eun r[ih; itmd=(o +hg)2 n i lw ih/; /lw+(ih-lw/;/ *o hg o)2*

/ I w rahapitweearmd i getrta bt o * f e ec on hr r[i] s rae hn oh f isajcn eeet armd1 adarmd1,te armd t daet lmns r[i-] n r[i+] hn r[i] i temxmmeeet/ s h aiu lmn* i (armd >armd+1 & armd >armd-1) f r[i] r[i ] & r[i] r[i ] rtr armd; eun r[i]

/ I armd i getrta tenx eeetadsalrta te * f r[i] s rae hn h et lmn n mle hn h eeette mxmmle o lf sd o md* lmn hn aiu is n et ie f i / i (r[i]>armd+1 & armd <armd-1) f armd r[i ] & r[i] r[i ] rtr fnMxmmar lw md1; eun idaiu(r, o, i-) es / we armd i getrta armd1 adsalrta ar le / hn r[i] s rae hn r[i-] n mle hn r rtr fnMxmmar md+1 hg) eun idaiu(r, i , ih;

/ Die pormt cekaoefntos* * rvr rga o hc bv ucin / itmi( n an) { itar]={,3 5,1,9 7 6; n r[ 1 , 0 0 , , } itn=szo(r)szo(r[]; n iefar/iefar0) pit(Temxmmeeeti %" fnMxmmar 0 n1) rnf"h aiu lmn s d, idaiu(r, , -); gthr) eca(; rtr 0 eun ; } Time Complexity: O(Logn) This method works only for distinct numbers. For example, it will not work for an array like {0, 1, 1, 2, 2, 2, 2, 2, 3, 4, 4, 5, 3, 3, 2, 2, 1, 1}.
www.geeksforgeeks.org/archives/17028 3/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Send 2 people

You may also like following posts 1. 2. 3. 4. 5. Search an element in a sorted and pivoted array Check for Majority Element in a sorted array Given an array arr[], find the maximum j i such that arr[j] > arr[i] Maximum and minimum of an array using minimum number of comparisons Find a Fixed Point in a given array

20 comments so far
1. jk says: March 23, 2012 at 10:04 AM itmxita itb{eun( >b ?a:b} n a(n , n )rtr a ) ;

itmxlmnRcit ary itsatne,itedne) n aEeete(n* ra, n trIdx n nIdx { i(trIdx= edne)rtr arysatne] fsatne = nIdx eun ra[trIdx; i(ra[trIdx >arysatne +1)rtr arysat farysatne] ra[trIdx ] eun ra[tr i(ra[nIdx >aryedne -1)rtr aryedne] faryedne] ra[nIdx ] eun ra[nIdx itmdne =(trIdx+edne)2 n iIdx satne nIdx/; i(ra[iIdx >arymdne -1 & arymdne]>a farymdne] ra[iIdx ] & ra[iIdx r i(ra[iIdx >arymdne -1 | arymdne]<a farymdne] ra[iIdx ] | ra[iIdx r i(ra[iIdx <arymdne -1 | arymdne]>a farymdne] ra[iIdx ] | ra[iIdx r rtr mxmxlmnRcary satne,mdne -1,mxl eun a(aEeete(ra, trIdx iIdx ) aE } itmxlmn(n*ary itsz) n aEeetit ra, n ie { rtr mxlmnRcary 0 sz -1; eun aEeete(ra, , ie ) }

itmi( n an) { itar]={,1 1 2 2 2 2 2 3 4 4 5 5 3 3 2 2 n r[ 0 , , , , , , , , , , , , , , , , pit(%\" mxlmn(r,1); rnf"dn, aEeetar 9)


www.geeksforgeeks.org/archives/17028 4/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

} Reply 2. kartikaditya says: February 20, 2012 at 3:39 AM pbi casMxnnAde { ulc ls aIIcnDc piaesai itgtaIIcnDcita] itsat it rvt ttc n eMxnnAde(n [, n tr, n i (tr >ed { f sat n) rtr ItgrMNVLE eun nee.I_AU; }

itmd=(tr +ed > 1 n i sat n) > ; wie(tr <ed { hl sat n) i ([i]>amd+1){ f amd [i ] ed=md n i; }es i ([i]<amd+1){ le f amd [i ] sat=md+1 tr i ; }es { le itlfMx=gtaIIcnDca sat md-1; n eta eMxnnAde(, tr, i ) itrgta =gtaIIcnDca md+1 ed; n ihMx eMxnnAde(, i , n) i (eta = ItgrMNVLE { f lfMx = nee.I_AU) rtr rgta; eun ihMx } i (ihMx= ItgrMNVLE { f rgta = nee.I_AU) rtr lfMx eun eta; } rtr (eta >rgta)?lfMx:rgta; eun lfMx ihMx eta ihMx } md=(tr +ed > 1 i sat n) > ; } rtr amd; eun [i]

pbi sai itgtaIIcnDcita] { ulc ttc n eMxnnAde(n [) rtr gtaIIcnDca 0 alnt -1; eun eMxnnAde(, , .egh ) }

pbi sai vi mi(tigag[){ ulc ttc od anSrn rs] Sse.u.rnl(eMxnnAde(e it]8 1,2,8 ytmotpitngtaIIcnDcnw n[{, 0 0 0 Sse.u.rnl(eMxnnAde(e it]1 3 5,1, ytmotpitngtaIIcnDcnw n[{, , 0 0 Sse.u.rnl(eMxnnAde(e it]1,2,3,4 ytmotpitngtaIIcnDcnw n[{0 0 0 Sse.u.rnl(eMxnnAde(e it]10 10 8, ytmotpitngtaIIcnDcnw n[{2, 0, 0 Sse.u.rnl(eMxnnAde(e it]0 1 1 2 2 ytmotpitngtaIIcnDcnw n[{, , , , }

www.geeksforgeeks.org/archives/17028

5/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

Reply 3. Krishs says: January 27, 2012 at 6:36 PM Well simple enough ... itmx (itar],itln) n a_ n r[ n e { itidx=0; n ne fr idx=0 idx<ln;idx+) o( ne ; ne e ne+ { i(idx! (e -)& aridx >aridx1 ){ f ne = ln 1 & r[ne] r[ne+] } rtr arln1; eun r[e-] } Reply kartik says: January 27, 2012 at 11:49 PM This looks similar to method 1 (linear search). Time complexity of this would be O(n). Reply 4. Wayne says: January 24, 2012 at 12:40 AM If the array elements aren't necessarily distinct, then in the extreme case, all elements are the same. Method 2 would degrade to O(N) solution since you have to scan both sides linearly until you meet anything breaking the even.

/ Pseyu cd hr (o mydlt teelnsi ntwiigc * at or oe ee Yu a eee hs ie f o rtn Reply 5. sandeep says: January 18, 2012 at 5:13 PM what if the numbers are repeated.like (0 1 1 2 2 2 2 2 3 4 4 5 3 3 2 2 1 1) and you find mid whose left and right both element are equal to mid how will you decide which part to check.. left or right? Reply GeeksforGeeks says: January 18, 2012 at 9:51 PM
www.geeksforgeeks.org/archives/17028 6/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

@sandeep: Thanks for pointing out this case. The method 2 doesn't always work if there are duplicates in array. We have added the same to the original post. Keep it up! Reply Shreyas says: January 30, 2012 at 11:27 AM @Sandeep: they said increasing and then decreasing so no duplicates allowed. 1 2 3 0 -1 but can never be 1 2 2 3.

/ Pseyu cd hr (o mydlt teelnsi ntwii * at or oe ee Yu a eee hs ie f o rt Reply 6. shaan7 says: January 17, 2012 at 11:43 AM Why not start from the beginning and when you find a number less than previous number, the previous number is max? Reply Nithish says: January 17, 2012 at 2:01 PM @shaan: That would have a worst case complexity of o(N). Consider the example given in the problem itself - Input: arr[] = {10, 20, 30, 40, 50}. Reply shaan7 says: January 17, 2012 at 2:41 PM Ah yes, my bad, actually I was suggesting an improvement for Method1. Reply abbie says: February 17, 2012 at 1:09 PM @Nithish: since the array is first increasing and then decreasing so we need not to compare all the elements in Method 1, the moment we find any element greater than current max element we can stop the iteration in Method 1. It'll reduce the no of comparisons though it'll not improve the complexity that much. Correct me if I am wrong. Thanx
www.geeksforgeeks.org/archives/17028 7/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

Reply 7. Priyanka says: January 17, 2012 at 12:39 AM #nld<ti.> icuesdoh #eien8 dfn vi mi( od an) { itan; n [] itl0ihn1m n =,,=-,; fri0ini+ o(=;<;+) saf"d,ai) cn(%"&[]; wiel=) hl(<h { m(+)2 =lh/; i(=n1 fm=-) bek ra; es i([]am1 & am>[-]& mn1& m0 le fam>[+] & []am1 & <- & >) bek ra; es i([]am1 & mn1 le fam<[+] & <-) lm1 =+; es i([]am1 & mn1 le fam>[+] & <-) hm1 =-; } pit(%"am) rnf"d,[]; } Reply gowtham says: February 12, 2012 at 10:02 AM

ia nwhr s cretm i ia wog @rynayuhv u m e ee o orc e f m rn. piak o ae s Reply gowtham says: February 12, 2012 at 10:06 AM i am new here so correct me if i am wrong. @priyanka you have used m <>/> p<p

<i cas"el" dv ls=rpy> < cas"omn-el-ik he=108rpyoo=4 a ls=cmetrpyln" rf"72?eltcm77

8. Ankur says:
www.geeksforgeeks.org/archives/17028 8/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

January 16, 2012 at 3:19 AM Iterative code below which is easier to understand

itfnMxmmita] itn{ n idaiu(n [, n ) itlw0 n o=; ithg=-; n ihn1 itmd n i; wielw=ih{ hl(o<hg) mdlw(ihlw/; i=o+hg-o)2 i(md= | amd1<[i] & (i=n1| amd>[i+ f(i=0 | [i-]amd) & md=- | [i]amd1 cu<amd<"" ot<[i]< ; rtr amd; eun [i] } /Icesn Pr o Ary/ *nraig at f ra* es i(md= | amd1<[i] & (i=n1| amd<[ le f(i=0 | [i-]amd) & md=- | [i]a cu<amd<"" ot<[i]< ; lwmd1 o=i+; } /Dcesn Pr o Ary* *eraig at f ra / es i(md= | amd1>[i] & (i=n1| amd>[ le f(i=0 | [i-]amd) & md=- | [i]a hg=i-; ihmd1 } } Reply 9. flyinghearts says: January 15, 2012 at 8:13 PM itfnMxmmcntitar] sz_ ln n idaiu(os n r[, iet e) { asr(e >0; setln ) sz_ lw=0 hg =ln-1 iet o , ih e ; wie(o ! hg){ hl lw = ih sz_ md=lw+(ih-lw /2 iet i o hg o) ; i (r[i]<armd+1)lw=md+1 f armd r[i ] o i ; es hg =md le ih i; } rtr arlw; eun r[o] } Reply 10. sachin says: January 15, 2012 at 5:24 AM This will not will for
www.geeksforgeeks.org/archives/17028 9/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

int[] a = {10,15,20,12,5,6,25}; It will return 20 instead of 25

/ Pseyu cd hr (o mydlt teelnsi ntwiigc * at or oe ee Yu a eee hs ie f o rtn Reply Nithish says: January 15, 2012 at 5:51 AM @sachin: The question reads - "Given an array of integers which is initially increasing and then decreasing,...". So your input is not for this question.

/ Pseyu cd hr (o mydlt teelnsi ntwii * at or oe ee Yu a eee hs ie f o rt Reply sachin says: January 15, 2012 at 5:55 AM Ohh I see ! thanks nitish

/ Pseyu cd hr (o mydlt teelnsi nt * at or oe ee Yu a eee hs ie f o Reply

Comment
Name (Required) Website URI code between sourcecode tags) Email (Required) Your Comment (Writing code? please paste your

[oreoelnug=C] succd agae"" / Pseyu cd hr (o mydlt teelns * at or oe ee Yu a eee hs ie i ntwiigcd)* f o rtn oe / [succd] /oreoe

Have Your Say

www.geeksforgeeks.org/archives/17028

10/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

Search

Popular Tags
GATE Java Dynamic Programming Backtracking Pattern Searching Divide & Conquer Graph Operating Systems Recursion

Popular Posts
All permutations of a given string Memory Layout of C Programs Understanding extern keyword in C Median of two sorted arrays Tree traversal without recursion and without stack! Structure Member Alignment, Padding and Data Packing Intersection point of two Linked Lists Lowest Common Ancestor in a BST. Check if a binary tree is BST or not Sorted Linked List to Balanced BST

www.geeksforgeeks.org/archives/17028

11/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

250

Subscribe

Forum Latest Discussion


LIS in nlogn time
Last Post By: kartik Inside: Interview Questions

tree to file
Last Post By: Dheeraj Inside: Interview Questions
www.geeksforgeeks.org/archives/17028 12/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

Count internal nodes in a binary tree


Last Post By: kartik Inside: Interview Questions

Ancestor of two given leaf nodes


Last Post By: dead Inside: Trees specific questions

combine elements of an array, so as to minimize weights.


Last Post By: rohanag Inside: Interview Questions

FB interview question
Last Post By: ranganath111 Inside: Interview Questions

Testing of factorial of a number


Last Post By: rukawa Inside: Interview Questions

numeric puzzle
Last Post By: asm Inside: Algorithms

Forum Categories
Interview Questions C/C++ Programming Questions Algorithms Trees specific questions Linked List specific questions Multiple Choice Questions Object oriented queries GPuzzles
www.geeksforgeeks.org/archives/17028 13/14

5/12/12

Find the maximum element in an array which is first increasing and then decreasing | GeeksforGeeks

Operating Systems Miscellaneous Java specific Questions Perl specific Questions

Recent Comments
Venki on Structure Member Alignment, Padding and Data Packing avi on Structure Member Alignment, Padding and Data Packing atul on A Program to check if strings are rotations of each other or not Venki on Structure Member Alignment, Padding and Data Packing rahul on Dynamic Programming | Set 13 (Cutting a Rod) Sandeep on Dynamic Programming | Set 3 (Longest Increasing Subsequence) Yueming on Dynamic Programming | Set 3 (Longest Increasing Subsequence) avi on Structure Member Alignment, Padding and Data Packing @geeksforgeeks, Some rights reserved Powered by WordPress & MooTools, customized by geeksforgeeks team

www.geeksforgeeks.org/archives/17028

14/14

You might also like