112
112
Two Sum
class Solution1 {
public:
for (int i = 0; i < n; i++) { // Outer loop to iterate through each element
};
int main() {
cout << "Indices: [" << result[0] << ", " << result[1] << "]" << endl; // Output result
class Solution2 {
public:
for (int i = 1; i < prices.size(); i++) { // Loop through the prices array
};
int main() {
cout << "Max Profit: " << solution.maxProfit(prices) << endl; // Output the result
// 3. Contains Duplicate
class Solution3 {
public:
for (int i = 1; i < nums.size(); i++) { // Loop through the sorted array
};
int main() {
// 4. Maximum Subarray
#include <iostream> // For input/output operations
class Solution4 {
public:
for (int i = 1; i < nums.size(); i++) { // Loop through the array starting from the second
element
};
int main() {
cout << "Max Subarray Sum: " << solution.maxSubArray(nums) << endl; // Output
result
// 5. Palindrome Number
#include <iostream> // For input/output operations
class Solution5 {
public:
return original == reversed; // Check if the original and reversed numbers are the
same
};
int main() {
// 6. Roman to Integer
#include <iostream> // For input/output operations
class Solution6 {
public:
for (int i = s.length() - 1; i >= 0; i--) { // Loop through the string from the end
if (currentValue < prevValue) { // If the current value is less than the previous value
} else {
}
return result; // Return the final result
};
int main() {
cout << "Integer: " << solution.romanToInt(s) << endl; // Output result
class Solution7 {
public:
};
int main() {
cout << "Longest Common Prefix: " << solution.longestCommonPrefix(strs) << endl; //
Output result
// 8. Valid Parentheses
class Solution8 {
public:
} else {
// Check if the current close parenthesis matches the top open parenthesis
if ((ch == ')' && top != '(') || (ch == '}' && top != '{') || (ch == ']' && top != '[')) {
};
int main() {
class Solution9 {
public:
for (int i = 1; i < nums.size(); i++) { // Loop through the array starting from the second
element
if (nums[i] != nums[i - 1]) { // If the current element is different from the previous
one
};
int main() {
cout << "Length: " << k << ", Array: ["; // Output result
cout << nums[i] << (i < k - 1 ? ", " : ""); // Print unique elements
class Solution10 {
public:
if (nums[i] != val) { // If the current element is not equal to the value to be removed
};
int main() {
cout << "Length: " << k << ", Array: ["; // Output result
for (int i = 0; i < k; i++) {
cout << nums[i] << (i < k - 1 ? ", " : ""); // Print remaining elements
// Class to implement the Find the Index of the First Occurrence in a String solution
class Solution11 {
public:
int strStr(string haystack, string needle) { // Function to find the index of the first
occurrence
if (m < n) return -1; // If the haystack is shorter than the needle, return -1
}
return -1; // Return -1 if no match is found
};
int main() {
cout << "Index: " << solution.strStr(haystack, needle) << endl; // Output result