1-continuous integer, out of order, repeating a number, missing a number, locate the two numbers in the same place

Source: Internet
Author: User

Problem description:

1. For example, if 4 is lost in the case of the drop-down list of the number of P = {3, 1, 2, 5, 3}, there are two three, and the two numbers are located in the same place.

2. For convenience, this continuous array starts from 1;

 

Solution:

1. element 3 is placed in the position of the array "p" [3-1]. This position originally stores 2, and then stores 2 in the "P" [2-1]...

Status Bar: {0-1} {0-1-1} {-1-1} {-1-1-1-1-1-1} {-1-1-1-1}

{, 3} {, 3} {, 3} {, 3} {, 3} {, 3}

Result bar: {3} {2 3} {1 2 3} {1 2 3 5} {1 2 3 5}

In the preceding process, an element at a certain position has two states, marked as 0 and 1 respectively. For example, after the first element 3 jumps to the P [3-1, the original position of the product was accessed, but it was not redirected (to this position). The status of the product was 0, and the position of the product was jumped to [3-1, the ID status is-1;

 

1. Input: int indium [] = {, 3 };

2. Output: duplicate element int CF, loss element int loss;

3. There is a problem, such as {3, 6, 5, 4, 1}, 3, jump to 5, jump to 1, and finally jump back to 3. This is an illusory process, with no access to steps 6 and 4 in the middle. How can we ensure that each element is traversed? We use two layers of nested loops;

4. The next hop element may have three states:
0: accessed but not redirected
-1: redirected
> 0: Neither accessed nor redirected

If the next hop is 0, it indicates that the element has been jumped out before, but it has not been jumped here. If it is skipped, it is changed to-1;

If the next hop is-1, it indicates that the element was previously accessed and accessed again. It can be seen that it is a duplicate element;

If the next hop is not 0 rather than-1, it indicates that it is a new element, jump to this element, and execute the next loop;

 

The idea is roughly the same as above, but it takes a lot of effort to write the code. It seems that it is still a thin Foundation, so I have to write more to speed up !!

# Include <stdio. h> # include <stdlib. h> # include <assert. h> int cf =-1, loss =-1; // Cf: duplicate element Loss: lost element int input [] = {2, 3, 6, 4, 2, 1, 7 }; void fun (INT indium [], int size) {assert (indium! = NULL & size> 1); int pre = indium [0], aft = indium [pre-1], I = 0, j = pre-1; // pre: current element value aft: the value of the next hop element is P 0 [0] = 0; while (j <size) {// the outer loop ensures that each element is traversed to I = J; printf ("outside loop: J = % d \ n", J); While (indium [I]! =-1) // The inner layer loop is a jump Loop Based on the relationship between the element and the lower mark of the array. {printf ("Indium [% d] = % d \ n", I, (If [I]); (if [I] = 0) {// 0 indicates that I has jumped to another position, but never jumped to another position; -1 indicates that other elements are redirected to this position. I have passed printf ("Indium [% d] = 0 is changed-1 \ n", I); indium [I] =-1; break;} else {pre = indium [I]; AFT = indium [pre-1]; printf ("pre = % d; AFT = % d \ n", pre, aft ); if (AFT =-1) {// if the next hop is-1, it indicates that the next hop element has been accessed, indicating that the indium [I] is a duplicate element cf = indium [I]; indium [I] = 0; // The position status changes to printf ("doubled element =, % d \ n", CF); break ;} else {// The Next Hop is not 0,-1 indicates that the next hop element is not accessed and loops to the next hop; indium [I] =-1; I = pre-1 ;}}} j ++;} I = 0; while (indium [I] =-1) I ++; loss = I + 1; printf ("loss element is = % d \ n", loss) ;}int main () {fun (input, 7); Return 0 ;}

Look at the results:

[[email protected] Desktop]# ./a.outoutside loop : j = 1inp[1] = 3pre=3 ; aft = 6inp[2] = 6pre=6 ; aft = 1inp[5] = 1pre=1 ; aft = 0inp[0] = 0inp[0] == 0 is changed -1outside loop : j = 2outside loop : j = 3inp[3] = 4pre=4 ; aft = 4outside loop : j = 4inp[4] = 2pre=2 ; aft = -1doubled element = , 2outside loop : j = 5outside loop : j = 6inp[6] = 7pre=7 ; aft = 7loss element is =  5[[email protected] Desktop]
Input: input [] = {2, 3, 6, 4, 2, 1, 7 };
doubled element =  2
Loss element is = 5

No problem so far!
 

 

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.