[Classic face question] the smallest absolute element in the sorted array

Source: Internet
Author: User

"title"

The topics are:

There is a sorted array (ascending), there may be positive, negative, or 0 in the array, the minimum number of absolute values of the elements in the array is required, the method cannot be compared in order (complexity needs to be less than O (n)) and can be implemented in any language

For example, the array { -20,-13,-4, 6, 77,200}, the smallest absolute value is-4.

"Analysis"

The given array is already ordered, and is ascending, without repeating elements.

A simple idea is to iterate through the array at once, and to find the minimum value of the absolute values of the elements of the arrays, so that the time complexity is O (n).

However, this wastes a condition of the topic: the array is already sequenced. Therefore, the original topic needs to be converted. Taking into account the array order, the use of binary search principle.

The element with the smallest absolute value in the array, and the nearest absolute value to 0, the smaller. Design the code based on this principle.

In three different situations:

(1) returns the first element value if it is all positive. if (A[0] >= 0)

(2) returns the last element value if it is all negative. if (A[n-1] <= 0)

(3) There is a positive negative, the use of binary search to find 0 of the insertion position, if you just find the position of 0, 0 is the smallest element of absolute value,

If no 0 is found, the left and right elements of the insertion position compare the absolute size, returning the smaller OK.

"Code"

/********************************** Date: 2015-01-29* sjf0115* title: Smallest element of absolute value in sorted array * Source: Baidu * Blog: ************************ /#include <iostream> #include <algorithm>using namespace std;int minabs (int a[],int n) {if (n = = 1)    {return a[0];    }//if//Only positive if (A[0] >= 0) {return a[0];    }//Only negative if (A[n-1] <= 0) {return a[n-1];    }//Find the insertion position of 0 int target = 0;    int left = 0,right = n-1;    int mid;        while (left <= right) {mid = left + (right-left)/2;        The intermediate element equals the target if (a[mid] = = target) {return a[mid];        }//target in the left half part else if (A[mid] > target) {right = Mid-1;        }//target in right half else{left = mid + 1;    }}//while//absolute value minimum if (ABS (A[left]) < ABS (A[right])) {return a[left]; } return a[right];}    int main () {//int a[] = { -6,-5,-4,-3,-2,-1};    int a[] = { -6,-5,-4,1,2,3}; int a[] = {1,2,3,4,5,6};    int a[] = { -20,-13,-4,6,77,200};    int n = 6;    Cout<<minabs (a,n) <<endl;; return 0;}


[Classic face question] the smallest absolute element in the sorted array

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.