1
+ # Given an array of n+2 elements with elements value lying beween 1 to n.
2
+ # All elements occur once except two.Print the two repeating numbers.
3
+
4
+ #Approach 1: Using sum and product of array (Making two equation)
5
+ #Time-complexity: O(n) Auxiliary-space: O(1)
6
+ #Caveat: If array is large this can lead to integer overflow.
7
+
8
+ def find_duplicates ( a )
9
+ len = a . length
10
+ n = len -2
11
+ sum_of_series = ( n *( n +1 ) ) /2 #Sum of series 1 to n
12
+
13
+ #Find sum and product of array elements
14
+ sum = 0
15
+ product = 1
16
+
17
+ for x in a
18
+ sum +=x
19
+ product *=x
20
+ end
21
+
22
+ sum -=sum_of_series #sum is a+b now,where a and b are repeating numbers
23
+ product /=fact ( n ) #product is a*b now,where a and b are repeating numbers
24
+
25
+ temp = Math . sqrt ( ( sum **2 ) -( 4 *product ) ) . to_i # temp is a-b
26
+
27
+ print "#{ ( sum +d ) /2 } #{ ( sum -d ) /2 } "
28
+ end
29
+
30
+ def fact ( x )
31
+ return 1 if ( x ==0 || x ==1 )
32
+ factorial = 1
33
+ while x >1
34
+ factorial *=x
35
+ x -=1
36
+ end
37
+ return factorial
38
+ end
39
+
40
+
41
+ find_duplicates ( [ 1 , 2 , 3 , 4 , 5 , 5 , 6 , 7 , 8 , 8 ] ) # => 5 8
42
+
43
+
44
+ #Approach 2: Use count array
45
+ #Time-complexity: O(n) Auxiliary-space: O(n) {for count array}
46
+
47
+ def find_duplicates ( a )
48
+ len = a . length
49
+ n = len -2
50
+ count = Array . new ( n , 0 ) # count has length n
51
+
52
+
53
+ for i in 0 ...len
54
+ if count [ a [ i ] -1 ] ==1
55
+ print "#{ a [ i ] } "
56
+ else
57
+ count [ a [ i ] -1 ] +=1
58
+ end
59
+ end
60
+ return
61
+ end
62
+
63
+ find_duplicates ( [ 1 , 2 , 3 , 4 , 5 , 5 , 6 , 7 , 8 , 8 ] ) # => 5 8
64
+
65
+ #Approach 3: Using Array Indexing
66
+ #Time-complexity: O(n) Auxiliary-space: O(1)
67
+ #Caveat: This algorithm modifies the array-elements
68
+
69
+ def find_duplicates ( a )
70
+ len = a . length
71
+
72
+ for i in 0 ...len
73
+ if a [ a [ i ] . abs ] <0
74
+ print "#{ a [ i ] . abs } "
75
+ else
76
+ a [ a [ i ] . abs ] = -a [ a [ i ] . abs ]
77
+ end
78
+ end
79
+ return
80
+ end
81
+
82
+ find_duplicates ( [ 1 , 2 , 3 , 4 , 5 , 5 , 6 , 7 , 8 , 8 ] ) # => 5 8
83
+
84
+ #Approach 4: Using bitwise XOR,take xor of all elements and all numbers from 1 to n then,
85
+ #find rightmost set bit,divide the range in two sets and take xor of each set, first with xor then with range.
86
+ #Time-complexity: O(n) Auxiliary-space: O(1)
87
+
88
+ def find_duplicates ( a )
89
+ len = a . length
90
+ n = len -2
91
+ xor = 0
92
+ x , y = 0 , 0 #variables to store duplicates
93
+
94
+ #xor of all numbers from 1 to n
95
+ for i in 1 ..n
96
+ xor ^=i
97
+ end
98
+ #xor of all array elements
99
+ for i in 0 ...len
100
+ xor ^=a [ i ]
101
+ end
102
+ #Rightmost set bit
103
+ set_bit_pos = xor & ~( xor -1 )
104
+ #Divinding array in two sets ,one with set bit at set_bit_pos and other with 0.
105
+ for i in 0 ...len
106
+ if ( a [ i ] & set_bit_pos == 0 )
107
+ x ^=a [ i ] # XOR of first-set(with 0 at set_bit_pos) in array
108
+ else
109
+ y ^=a [ i ] # XOR of second-set(with 1 at set_bit_pos) in array
110
+ end
111
+ end
112
+
113
+ for i in 0 ..n
114
+ if ( i & set_bit_pos == 0 )
115
+ x ^=i # XOR of first-set(with 0 at set_bit_pos) in range
116
+ else
117
+ y ^=i # XOR of second-set(with 1 at set_bit_pos) in range
118
+ end
119
+ end
120
+ print "#{ x } #{ y } "
121
+ return
122
+ end
123
+
124
+ find_duplicates ( [ 1 , 2 , 3 , 4 , 5 , 5 , 6 , 7 , 8 , 8 ] ) # => 5 8
0 commit comments