This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<int> rotLeft(vector<int> a, int d) { | |
vector<int> rotations(a.size(), -1); | |
for (int i = 0; i < a.size(); i++) { | |
// Get right moves formula = a.length - left Rotaions | |
int rightMoves = a.size() - d; | |
// Get the next index position of our current number | |
int newIndex = (i + rightMoves) % a.size(); // Circular array right | |
rotations[newIndex] = a[i]; | |
} | |
return rotations; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
using namespace std; | |
int kadanesAlgorithm(vector<int> array) { | |
int maxCurrentSum = array[0]; | |
int maxSoFar = array[0]; | |
for (int i = 1; i < array.size(); i++) { | |
maxCurrentSum = max(array[i] + maxCurrentSum, array[i]); | |
maxSoFar = max(maxSoFar, maxCurrentSum); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace std; | |
void fillArray(vector<vector<int>> array, vector<int>& results, int startColumn, int endColumn, int startRow, int endRow) { | |
if (startColumn > endColumn || startRow > endRow) return; | |
for (int i = startColumn; i <= endColumn; i++) { | |
cout << array[startRow][i] << endl; | |
results.push_back(array[startRow][i]); // TOP | |
} | |
for (int i = startRow + 1; i <= endRow; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace std; | |
// O(n) time | O(i) space -where n is the length of the arrat | |
bool isMonotonic(vector<int> array) { | |
if (array.size() < 2) return true; | |
int is_non_increasing = true; | |
int is_non_decreasing = true; | |
for (int i = 1; i < array.size(); i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
using namespace std; | |
vector<int> moveElementToEnd(vector<int> array, int toMove) { | |
int left = 0; | |
int right = array.size() - 1; | |
while (left < right) { | |
if (array[right] == toMove) { | |
right--; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <algorithm> | |
#include <climits> | |
using namespace std; | |
vector<int> smallestDifference(vector<int> arrayOne, vector<int> arrayTwo) { | |
sort(arrayOne.begin(), arrayOne.end()); | |
sort(arrayTwo.begin(), arrayTwo.end()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
// O(n^2) time | O(n) space | |
vector<vector<int>> threeNumberSum(vector<int> array, int targetSum) { | |
vector<vector<int>> triplets; | |
sort(array.begin(), array.end()); | |
for (int i = 0; i < array.size() - 2; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace std; | |
string caesarCypherEncryptor(string str, int key) { | |
for (int i = 0; i < str.size(); i++) { | |
int charCode = (int)str[i]; | |
int nextCode = charCode + (key % 26); // Protect from max alphabet | |
int modus = (nextCode % 'z') + 'a' - 1; // Protect from max Z, and min A | |
// Add min in this case, a. | |
int resultCode = nextCode > 'z' ? modus : nextCode; | |
str[i] = (char)resultCode; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <any> | |
#include <vector> | |
#include <typeinfo> | |
using namespace std; | |
int productSum(vector<any> array, int multiplier = 1) { | |
int result = 0; | |
for (auto element : array) { | |
if (element.type() == typeid(vector<any>)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.algoexpert.io/questions/Depth-first%20Search | |
#include <vector> | |
using namespace std; | |
// Do not edit the class below except | |
// for the depthFirstSearch method. | |
// Feel free to add new properties | |
// and methods to the class. | |
class Node { |