Two basic algorithm pen questions

Source: Internet
Author: User

1: Write a function to realize bitwise cyclic shift of integer arrays to the right, for example:

For 1, 2, 3, 4, and 5, you must move two places to the right cyclically. The result must be: 4, 5, 1, 2, 3.

The algorithm complexity is not greater than O (n), and the space complexity is as small as possible.

 

A:

Code 1

 1 public static void loopright (INT [] IA, int it)
2 {
3 // The number of mobile digits cannot be negative.
4 If (it <0)
5 {
6 console. writeline ("invalid Param! ");
7 return;
8}
9 // if the number of digits to be moved is greater than the length of the array, it is actually a loop. You only need to move the number of digits to the array.
10 if (it> IA. length)
11 {
12 It % = IA. length;
13}
14 int [] temp = new int [IA. Length];
15
16 int I;
17 // first move the latter part to the front of the temporary Array
18 For (I = 0; I <it; I ++)
19 {
20 temp [I] = Ia [IA. Length-It + I];
21}
22
23 // Add the first part to the end of the temporary Array
24 For (Int J = 0; j <IA. Length-it; I ++, J ++)
25 {
26 temp [I] = Ia [J];
27}
28 // copy the result to the returned array
29 for (Int J = 0; j <IA. length; j ++)
30 {
31 Ia [J] = temp [J];
32}
33}

 

This is the solution I came up with today, but the interviewer saw that the time complexity was satisfying, but the space complexity was too large. I had no idea, but I thought about it, at that time, my mind was messy and I didn't come up with a good solution. I checked the implementation on the Internet. As mentioned above, the final solution should be more efficient:

Array cyclic shift

"Assume that the original array sequence isABCD1234. The array sequence to be transformed is 1234.ABCDThat is, the loop shifts four places to the right. After comparison, it is easy to see that the order of the two segments remains unchanged: 1234 andABCDThe two sections can be considered as two parts. Right ShiftKThe bitwise process is to swap the two parts of the array. Perform the following steps to complete the transformation:

1. Sort in reverse orderABCD:ABCD1234 →Dcba1234;

2. Sort 1234 in reverse order:Dcba1234 →Dcba4321;

3. All Reverse Order:Dcba4321 → 1234ABCD.

Version 3

Reverse (char * arr, int B, int e)
{
For (; B <E; B ++, e --)
{
Char temp = arr [E];
Arr [e] = arr [B];
Arr [B] = temp;
}
}

 

Rightshift (

Char * arr, int N, int K)
{
K % = N;
Reverse (ARR, 0, N-k-1 );
Reverse (ARR, n-k, N-1 );
Reverse (ARR, 0, N-1 );
}


In this way, we can implement the right shift operation in linear time. "

 

 

Well, the above solution is really clever. I only used two temporary variables, but I still don't think it is easy to think of it at the test site.

 

2: There is an integer array that requires finding the second largest element in it. If all elements are equal, there is no second largest element. If there are multiple equal elements, it is regarded as one, find the second largest element. If multiple equal elements are tied for the second largest element, the second largest element exists. For example: 9, 9, 9, 8, 8, then the second largest is 8.

 

 

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.