Repository navigation
Expand file tree
/
Copy pathquestions.cpp
More file actions
70 lines (48 loc) · 1.56 KB
/
Copy pathquestions.cpp
File metadata and controls
70 lines (48 loc) · 1.56 KB
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
/* Question 1:
#include <vector>
int solution(std::vector<int> &A) { // & - address of operator
int negativeCount = 0; // Initialize variable negativeCount to 0. This variable counts the number of negative numbers in the array.
for (int num : A) {
if (num == 0) {
return 0;
} else if (num < 0) {
negativeCount++;
}
}
if (negativeCount % 2 == 0) { // Computes the remainder when negativeCount is divided by 2.
return 1;
} else {
return -1;
}
}*/
/* Question 2: A function that splits the string 'S' into a minimal number of substrings such that no letter occurs more than once in each substring.
#include <string>
#include <unordered_set>
int solution(std::string &S) {
std::unordered_set<char> seenChars; // Initialize an unordered set seenChars{}
int substrCount = 1; // We start with at least one substring
for (char c : S) { // Iterate over each character in the string 'S'.
// If the character has already been seen in the set seenChars
if (seenChars.find(c) != seenChars.end()) {
substrCount++; // Increment the substring count
seenChars.clear();
}
// Add the character to the set of seen characters
seenChars.insert(c);
}
return substrCount;
}
*/
/* Question 3: Debug
#include <vector>
#include <algorithm>
int solution(vector<int>& A) {
int ans = 0;
for (uint i = 1; i < A.size(); i++) {
if (ans > A[i]) {
ans = A[i];
}
}
return ans;
}
*/