Baidu interview questions: Minimum Absolute Value

Source: Internet
Author: User

There is an sorted array (ascending). The array may have positive numbers, negative numbers, or 0. It is required to obtain the minimum absolute value of the elements in the array, the sequential comparison method cannot be used (the complexity must be less than O (n), and can be implemented in any language, for example, array {-20,-13,-4, 6, 77,200 }, the smallest absolute value is-4. The basic idea of Algorithm Implementation is to find the negative and positive number demarcation points. If it happens to be 0, it will be it. If it is a positive number, it will be compared with the absolute value of the negative number adjacent to the left side. If it is a negative number, compare the absolute value with the positive number on the right. Consider that the array only has positive or negative numbers. Based on this idea, I implemented an algorithm in Java. Everyone has better implementation methods. Please refer to [java] public class MinAbsoluteValue {private static int getMinAbsoluteValue (int [] source) {int index = 0; int result = 0; int startIndex = 0; int endIndex = source. length-1; // calculate the negative number and positive number of demarcation points while (true) {index = startIndex + (endIndex-startIndex)/2; result = source [index]; if (result = 0) {return 0;} else if (result> 0) {if (index = 0) {break ;} if (source [index-1]> 0) endIndex = index-1; else if (source [index-1] = 0) return 0; else break ;} else {if (index = endIndex) break; if (source [index + 1] <0) startIndex = index + 1; else if (source [index + 1] = 0) return 0; else break; }}// calculate the minimum number of absolute values based on the demarcation point if (source [index]> 0) {if (index = 0 | source [index] <Math. abs (source [index-1]) result = source [index]; else result = source [index-1];} else {if (index = source. length-1 | Math. abs (source [index]) <source [index + 1]) result = source [index]; else result = source [index + 1];} return result ;} public static void main (String [] args) throws Exception {int [] arr1 = new int [] {-23,-22,-3, 120}; int [] arr2 = new int [] {-23,-22,-12,-6,-4 }; int [] arr3 = new int [] {66,333,}; int value = getMinAbsoluteValue (arr1); System. out. println (value); value = getMinAbsoluteValue (arr2); System. out. println (value); value = getMinAbsoluteValue (arr3); System. out. println (value );}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.