Interpreting Baidu interview questions

Source: Internet
Author: User

Question: an unordered array is used to find the number larger than or equal to the number on the left and smaller than or equal to the number on the right. Such as Arrays:, and 19. Output: 2, 8, 19.

 

Answer: Use a pointer array for two loops. The main method is difficult to explain and can be clearly illustrated. Two Arrays can be used for implementation, which is easy to understand. However, only one array is used to save space.

 

# Include <iostream>

# Define max (a, B) a> B? A: B;
# Define min (a, B) a <B? A: B;

Using namespace std;

Void func (int data [], int len)
{

Int * a = new int [len];

A [len-1] = data [len-1];

For (int I = len-2; I> 0; I --)
{
A [I] = min (data [I], a [I + 1]);
}
If (data [0] <= a [1])
{
Cout <"data [0]" <data [0] <endl;
}
A [0] = max (data [0], a [1]);

/* For (int I = 1; I <len-1; I ++)
{
Cout <"# data [" <I <"] =" <data [I] <Endl;
}*/

For (INT I = 1; I <len-1; I ++)
{
If (data [I] <= A [I + 1] & Data [I]> = A [I-1])
{
Cout <"data [" <I <"] =" <data [I] <Endl;
}
A [I] = max (data [I], a [I-1]);
}

// A [len-2] = max (data [Len-]
If (data [len-1]> = A [len-2])
Cout <"data [" <len-1 <"] =" <data [len-1] <Endl;

}

Int main ()

{
Int data [] = };

Func (data, 8 );
Return 0;
}

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.