1-continuous integer, out of order, missing two numbers, locate the two numbers in the original place

Source: Internet
Author: User

Problem description:

1. For example, if the number is 1, 5 is lost in the drop-down list, the numbers are found in the same place.

2. In order to facilitate discussion of this continuous array from 1 to 7 as of, the array is extended to 7-bit indium = {2, 3, 6, 4, 7, 0 };

 

Solution:

1. Use the relationship between array element values and array subscript to traverse the entire array. If the element is not traversed, the element is lost. The specific process is as follows:

1. Read arrays from the beginning. In this example, [0] = 2. Based on the value of 2, jump to the position where the subscript of the array is P 1 [2-1]. Then, 3 is obtained;

2. modify the status: during this process, the position of the P 0 is exceeded, but not exceeded. 0 indicates this status. Modify indium [0] = 0;

3. If the position of P [2-1] Is jumped in, it indicates that the position has been accessed. Based on the relationship between the array element value and the array underlying object, it can be proved that element 2 exists. -1 indicates this status. Modify indium [2-1] =-1;

4. and so on until the final array elements are similar to {0,-1,-1,-0th,-1,-1}, and the positions of array 4th and are 0, prove that elements 1 and 5 are missing;

 

# Include <stdio. h> # include <stdlib. h> # include <assert. h> int input [] = {2, 3, 6, 4, 7, 0}; // void fun (INT indium [], int size) {assert (indium! = NULL & size> 1); int I = 0, j = 0, pre =-1; int isnew = 1; // The internal loop while cannot determine whether the new elements read are redirected (should be set to-1) or the new elements read by the External Loop while (should be set to 0) while (j <size) {I = J; while (indium [I]! =-1) {If (indium [I] = 0) {indium [I] =-1;} // else {int pre = indium [I]; if (isnew! = 1) {indium [I] =-1;} // The element is not 0, not-1, and jumps from other elements, this element is accessed-1. If isnew = 1 (true) is a new read element, it indicates that it has not been accessed; else {indium [I] = 0 ;} I = pre-1;} isnew = 0;} isnew = 1; j ++;} I = 0; while (I <size) {printf ("final result = indium [% d] = % d \ n", I, indium [I]); I ++ ;}}
final result ==== inp[0] = 0final result ==== inp[1] = -1final result ==== inp[2] = -1final result ==== inp[3] = -1final result ==== inp[4] = 0final result ==== inp[5] = -1final result ==== inp[6] = -1[[email protected] Desktop]# 

 

Improvement Methods:

1. In fact, we do not need to store the 0 and-1 states. When the position is read, it is set to-1. If it is not read, nothing is done;

2. The External Loop ensures that each element is traversed;

3. The final array elements are similar to {2,-1,-1,-0th,-1,-1}, indicating that the elements 1 and 5 at the positions of array 4th and are missing;

# Include <stdio. h> # include <stdlib. h> # include <assert. h> int input [] = {2, 3, 6, 4, 7, 0}; // filling input [] with 0; void fun (INT indium [], int size) {assert (indium! = NULL & size> 1); int I = 0, j = 0; int isnew = 1; // used to determine that the bit element is redirected from another position (the bit element is marked as-1) or is the external loop I ++ auto-incrementing to this bit (the bit element is a new element without being marked as-1) while (j <size) {I = J; while (indium [I]! =-1) {int TMP = indium [I]; If (isnew! = 1) {indium [I] =-1;} // when the value of the P [5] = 0, I = P [I]-1 =-1, therefore, if (tmp-1) <0) break; else I = tmp-1; isnew = 0 ;} isnew = 1; j ++;} I = 0; while (I <size) {printf ("Finally = indium [% d] = % d \ n ", i, indium [I]); I ++ ;}} int main () {fun (input, 7); Return 0 ;}

 

[[email protected] Desktop]# ./a.outfinally ==== inp[0] = 2finally ==== inp[1] = -1finally ==== inp[2] = -1finally ==== inp[3] = -1finally ==== inp[4] = 7finally ==== inp[5] = -1finally ==== inp[6] = -1[[email protected] Desktop]# 

 

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.