Find the number of duplicates (or missing) in a continuous positive integer sequence.

Source: Internet
Author: User

There is a simple problem: given n-m + 2 (or N-m) composed of Positive IntegersDisordered SequenceThe elements are positive integers from m to n (n> m> = 1), and only one is repeated (or missing. How to find the number? (Assume that the missing number is not N or M)

ByIvonyThe difference or algorithm proposed.

1. Because the difference or algorithm in the closed range of [M, N] is not considered for the moment, [M-1] ^ [1, N] is used to indirectly obtain [m, n] the exclusive or of this closed interval.

2. Find the exclusive or of the array to be evaluated, and compare the result with the exclusive or result of [M, N] above, you can get the unique number of duplicates (or the missing number)

1 using system. LINQ; 2 using system; 3 namespace consoleapplication3 4 {5 class program 6 {7 static void main (string [] ARGs) 8 {9 int [] intergers = {11, 8, 9, 6, 12, 7, 5, 10, 810, 8}; // [5, 14], one more console. writeline (getrepeatorlack (intergers); 11 console. readkey (); 12} 13 // a disordered sequence consisting of n-m + 2 (or N-m) integers, whose elements are different integers from m to n, and only one is repeated (or missing. This algorithm returns the number of 14 Private Static int getrepeatorlack (INT [] arrints) 15 {16 int temp = 0; 17 int upper = arrints. length; 18 // temp is the variance of all numbers in the array arrints or result 19 for (INT I = 0; I <upper; I ++) 20 {21 temp = temp ^ arrints [I]; 22} 23 return temp ^ getxor (arrints. min (), arrints. max () + 1 ); 24} 25 /// <summary> 26 /// returns the XOR of several consecutive integers from start to end. Result 27 /// </Summary> 28 // <param name = "start"> Start position (inclusive) </param> 29 // <Param name = "end"> end position (not included) </param> 30 /// <returns> [start, end-1] closed interval variance or result </returns> 31 Private Static int getxor (INT start, int end) 32 {33 If (Start> = end) 34 {35 throw new argumentexception ("end must be greater than start"); 36} 37 If (start + 1 = END) 38 {39 return 0; 40} 41 return getxorfrom1ton (start) ^ getxorfrom1ton (end ); 42} 43 // <summary> 44 // returns the XOR or result of several consecutive integers from 1 to end. 45 /// </Summary> 46 // <param name = "end"> end position (not included) </param> 47 // <returns> [1, end-1] returns an exception or result of a closed interval </returns> 48 Private Static int getxorfrom1ton (INT end) 49 {50 if (end <= 1) 51 {52 throw new argumentexception ("the end parameter must be greater than or equal to 2"); 53} 54 if (2 = end) 55 {56 return 0; 57} 58 int mod = (end-1) % 4; 59 switch (MOD) 60 {61 case 0: 62 Return end-1; 63 Case 1: 64 return 1; 65 Case 2: 66 return end; 67 case 3: 68 return 0; 69 default: 70 return 0; 71} 72} 73 74 # region [1, N] The returns or differences of continuous positive integers in the closed range are regular 75 // we found [1, n] The variance or result of a continuous positive integer in the closed interval is regular, that is, 76 // remainder [1, n] exclusive or result xor77 // 0 n78 // 1 179 // n mod 4 = 2 N + 180 // 3 081 // so the following method can be transformed into the above 82 // Private Static int getxor (INT start, int end) 83 // {84 // int result = 0; 85 // If (start + 1 = END) 86 // {87 // return 0; 88 //} 89 // For (INT I = start; I <end; I ++) 90 // {91 // result = Result ^ I; 92 //} 93 // return result; 94 //} 95 # endregion96 97} 98}

 

Find the number of duplicates (or missing) in a continuous positive integer sequence.

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.