Skip to content

Instantly share code, notes, and snippets.

View misterpoloy's full-sized avatar
👽
Software Engineer

Juan P. Ortiz misterpoloy

👽
Software Engineer
View GitHub Profile
@misterpoloy
misterpoloy / nleftRotations.cpp
Created May 15, 2020 01:29
Rotate n times an array elements
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;
@misterpoloy
misterpoloy / KadanseAlgorithm.cpp
Created May 15, 2020 00:16
Max integer in a non empty subarray
#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);
}
@misterpoloy
misterpoloy / ArraySpiralTraverse.cpp
Created May 12, 2020 07:09
#CodeChallenge Spiral traverse write a function that takes in a n x m two-dimensional array and return a one dimensional array in spiral order.
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++) {
@misterpoloy
misterpoloy / isMonotonic.cpp
Created May 12, 2020 04:58
An array is said to be monotonic if its elements, from left to right, are entirely no-increasing or enteriley non-decreasing.
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++) {
@misterpoloy
misterpoloy / MoveToEnd.cpp
Created May 5, 2020 03:20
#CodeChallenge move element to the end
#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--;
@misterpoloy
misterpoloy / SmallestDifference.cpp
Created May 5, 2020 01:37
#CodeChallenge Find the smallest difference from one element in array 1 and the second element in array 2
#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());
@misterpoloy
misterpoloy / ThreeNumberSum.cpp
Created May 4, 2020 00:12
Three Num Sum Code Challenge
#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++) {
@misterpoloy
misterpoloy / ShiftStrings.cpp
Last active May 3, 2020 23:27
Shift string Code Challenge
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;
@misterpoloy
misterpoloy / ProductSum.cpp
Created April 29, 2020 16:43
Sum each element of an array
#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>)) {
@misterpoloy
misterpoloy / DepthSearchNode.cpp
Created April 25, 2020 11:05
Implement Depth Search in a Node
// 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 {