Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / rotated_binary_search.cpp
Created August 19, 2016 21:17
Binary search on k-rotated array
#include <iostream>
int at(int* arr, int n, int k, int m) {
return arr[((m + (n - (k % n))) % n)];
}
int rotated_binary_search(int* arr, int n, int k, int key) {
int low = 0, high = n-1, mid, v;
while (low <= high) {
@dgodfrey206
dgodfrey206 / rotationidx.cpp
Last active August 19, 2016 02:31
Formula to generate index of element of array after k insertions
#include <iostream>
using namespace std;
// returns the element at index m in arr after k rotations
int at(int* arr, int n, int k, int m) {
return arr[((m + (n - (k % n))) % n)];
}
int main() {
int n, k, q, i, j, m;
@dgodfrey206
dgodfrey206 / liststuff.cpp
Created August 15, 2016 21:11
Random linked list stuff
#include<iostream>
struct node{
int data;
node* next;
};
node* NewNode(int data){
node* n=new node;
n->data=data;
@dgodfrey206
dgodfrey206 / downup.cpp
Last active March 26, 2022 16:12
Prints string removing tail then adds tail
#include <cstring>
using namespace std;
void downup(const char str[]) {
int n = strlen(str);
int i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < n-i; ++j)
cout << str[j];
cout << '\n';
@dgodfrey206
dgodfrey206 / GuessNumber.cpp
Created July 27, 2016 23:04
A console based number guessing game
// NumberGuessing.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <vector>
#include <numeric>
#include <limits>
#include <string>
struct ListNode {
int val;
struct ListNode* next;
ListNode(int val) : val(val), next(0) {}
};
struct ListNode* newNode(int data) {
return new struct ListNode(data);
}
#include <iostream>
#include <vector>
using namespace std;
inline int max(int a, int b) {
return a > b ? a : b;
}
int trunc(int v) {
if (abs(v) == 1) return abs(v);
@dgodfrey206
dgodfrey206 / maxSubArray.cpp
Last active April 28, 2016 20:53
Calculating the length of the longest contiguous subsequence
#include <iostream>
#include <vector>
using std::max;
// Let A be an array of length N
// Let M(i) be the largest contiguous subsequence ending at index i
// M(0) = 1
// M(i) = max(M(i),M(j)+1) for j<i and A[j]<=A[i]
int maxSubArray(int A[], int N) {
@dgodfrey206
dgodfrey206 / morse.cpp
Created April 18, 2016 00:01
Morse code translator
#include <algorithm>
#include <streambuf>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <utility>
#include <string>
#include <vector>
#include <cctype>
#include <memory>
@dgodfrey206
dgodfrey206 / unary2base10.cpp
Created February 28, 2016 03:31
Converting unary to base 10
#include <iostream>
#include <string>
#include <sstream>
std::string UnaryToBinary(std::string str) {
std::string binary;
binary.reserve(30);
char flag;
for (std::stringstream ss(str); ss >> str; ) {