Why min and max are initialized differently for even and odd sized arrays? Time Complexity is O(n) and Space Complexity is O(1).įor each pair, there are a total of three comparisons, first among the elements of the pair and the other two with min and max. By convention, we assume ans as max and ans as min We initialize both minimum and maximum element to the first element and then traverse the array, comparing each element and update minimum and maximum whenever necessary. Searching linearly: Increment the loop by 1 Searching linearly: Increment the loop by 1Ĭomparison in pairs: Increment the loop by 2ġ. You need to decrease the number of comparisons as much as you can. The bottleneck parameter in this problem is the number of comparisons that your algorithm requires to determine the maximum and the minimum element. The interviewer would not judge your algorithm for this question based on the time complexity as all solution has the time complexity of O(n). No, they can be positive, negative, or zero)Īre the array element sorted ? (Ans: No, they can be in any order) Possible follow-up questions to ask the interviewer :-Īre the array elements necessarily positive? ( Your algorithm should make the minimum number of comparisons. If it does, you're past the end of the array - keys can't be null in arrays.Given an array A of size n, you need to find the maximum and minimum element present in the array. To see if you've blown past the end of the array, use key($array) and see if it returns NULL. When you are implementing the Iterator interface, therefore, it's a real pain in the behind to use each(). You see, each() gets the value BEFORE advancing the pointer, and next() gets the value AFTER advancing the pointer. It took me a while to figure this out, but there is a more consistent way to figure out whether you really went past the end of the array, than using each(). Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |