-
Notifications
You must be signed in to change notification settings - Fork 1
/
MissingNumberFromSortedArray.cpp
117 lines (94 loc) · 2.42 KB
/
MissingNumberFromSortedArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
#include <stdio.h>
using namespace std;
int playwithstatic(){
static int number = 1;
return number++;
}
int bs(const int arr[], int key, int start, int end){
int mid;
while (start < end) {
mid = start + (end - start) / 2;
if ( key == arr[mid]) {
return mid;
} else if ( key < arr[mid] ){
end = mid;
} else {
start = mid;
}
}
return -1;
}
bool isPalindrome(string str) {
if (str.length() <= 1) return true;
auto first = str.begin();
auto last = str.end() - 1;
while (first < last)
{
cout << " first is " << *first << " last is " << *last << endl;
if ( *first != *last) {
return false;
}
++first;
--last;
}
return true;
}
/*
int main(){
int size = 7;
int arr[] = {1, 2, 3, 4, 7, 9, 12};
for (int i = 0; i < size; ++i) {
cout << "key is " << arr[i] << " pos is " << bs(arr, arr[i], 0, size) << endl;
}
return 0;
}
*/
bool isPalindromeSTL(string str){
string str2 = str;
reverse( str.begin(), str.end());
return !str.compare(str2); // return 0 if same strings
}
template <class TT>
void reverseString( TT &first, TT &last) {
while (first != last && first != --last) {
swap(first++, last);
}
}
template <class TT>
void swap(TT & a, TT & b){
TT c(b);
b = a;
a = c;
}
int main(){
int size = 5;
vector<int> vc(size);
iota(vc.begin(), vc.end(), 0); // fill an increasing sequence starting from zero
shuffle(vc.begin(), vc.end(), std::mt19937(random_device{}())); // shuffle sequence randomly with random_device seed to generate random number using mt19937 algorithm
std::sort(vc.begin(), vc.end(), []( int a , int b){ return a < b; });
for (auto p : vc) cout << p << " ";
cout << endl;
std::srand(std::time(0));
int randselect = rand() % size;
int numberRemoved = vc[randselect];
vc[randselect] = 0;
int result = 0;
for (int i = 0; i < size; i++)
{
cout << i << "is " << result << endl;
result ^= vc[i];
}
for (auto p : vc) cout << p << " ";
cout << endl;
cout << "random select " << numberRemoved << " to remove\n";
cout << "the number missed is " << result << endl;
cout << playwithstatic() << " " << playwithstatic() << " " << playwithstatic() << endl;
cout << " is string goog " << " palindrome " << isPalindromeSTL(string("goog"));
cout << " reverse string apple is " << endl;
return 0;
}