Crossfit Open 18.4
Crossfit Open 18.4

CrossFit Games Open 18.4 Strategy and Analysis

18.4 CrossFit Open includes the following workouts:

Do 2 rounds of the following in 14 minutes:

  1. 21 deadlifts at 225lb
  2. 21 handstand push ups
  3. 15 deadlifts at 225lb
  4. 15 handstand push ups
  5. 9 deadlifts at 225lb
  6. 9 handstand push ups
  7. 21 deadlifts at 315lb
  8. 50 foot handstand walk
  9. 15 deadlifts at 315lb
  10. 50 foot handstand walk
  11. 9 deadlifts at 315lb
  12. 50 foot handstand walk

Read more

Min Max Sum from hackerrank on C++

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Full question is over here:hackerrank

Working solution

#include <bits/stdc++.h>

using namespace std;

void miniMaxSum(vector <int> arr) {
    // Complete this function
    long long min64bit = LLONG_MAX;
    long long max64bit = 0;
    long long temp64bit = 0;
    for (int i=0; i<arr.size(); i++){
        //sum everything but i
        temp64bit = 0;
        for (int j=0; j<arr.size(); j++){
            if (i==j)
                continue;
            temp64bit +=arr[j];
        }
        if (temp64bit > max64bit)
            max64bit = temp64bit;
        if (temp64bit < min64bit)
            min64bit = temp64bit;
    }
    cout<<min64bit<<" "<<max64bit;
}

int main() {
    vector<int> arr(5);
    for(int arr_i = 0; arr_i < 5; arr_i++){
       cin >> arr[arr_i];
    }
    miniMaxSum(arr);
    return 0;
}

Merge Sort in Objective-C

An implementation of the merge sort. Coded according to a Top-Down C example from Wikipedia. https://en.wikipedia.org/wiki/Merge_sort

// Array A[] has the items to sort; array B[] is a work array.
- (void)topDownMergeSort:(NSMutableArray*)A array:(NSMutableArray*) B int:(int) n{
    [self copyArray:A begin:0 end:n array:B];      // duplicate array A[] into B[]
    [self topDownSplitMerge:B begin:0 end:n array:A];// sort data from B[] into A[]
}

- (void)topDownSplitMerge:(NSMutableArray*)B begin:(int)iBegin end:(int)iEnd array:(NSMutableArray*)A {
    if(iEnd - iBegin < 2)                       // if run size == 1
        return;                                 //   consider it sorted
    // split the run longer than 1 item into halves
    int iMiddle = (iEnd + iBegin) / 2;              // iMiddle = mid point
    // recursively sort both runs from array A[] into B[]
    [self topDownSplitMerge:A begin:iBegin end:iMiddle array:B];// sort the left  run
    [self topDownSplitMerge:A begin:iMiddle end:iEnd array:B];// sort the right run
  
    // merge the resulting runs from array B[] into A[]
    [self topDownMerge:B begin:iBegin middle:iMiddle end:iEnd array:A];
}

- (void)topDownMerge:(NSMutableArray*)A begin:(int)iBegin middle:(int)iMiddle end:(int)iEnd array:(NSMutableArray*)B {
    int i = iBegin;
    int j = iMiddle;
    
    // While there are elements in the left or right runs...
    for (int k = iBegin; k < iEnd; k++) {
        // If left run head exists and is <= existing right run head.
        if (i < iMiddle && (j >= iEnd || A[i] <= A[j])) {
            B[k] = A[i];
            i = i + 1;
        } else {
            B[k] = A[j];
            j = j + 1;
        }
    }
}

- (void)copyArray:(NSMutableArray*)A begin:(int)iBegin end:(int)iEnd array:(NSMutableArray*)B {
    for (int i = iBegin; i<iEnd; i++){
        [B setObject:[A objectAtIndex:i] atIndexedSubscript:i];
    }
}

Algorithm can be tested with the following input:

 NSMutableArray* a =[[NSMutableArray alloc] initWithObjects:@10,@1, @20, @15, @30, nil];
    NSMutableArray* b = [[NSMutableArray alloc] init];
    
    [self topDownMergeSort:a array:b int:a.count];
    NSLog(@"%@", a);

Result that is printed:
2017-12-17 10:57:53.010807-0500 testCode[9627:2980041] (
1,
10,
15,
20,
30
)

Clone Undirected Graph with Javascript

A question that I got asked on the interview at Facebook on 2016 that I wasn’t able to finish coding within interview time frame. Finally got a chance to put it into code.

I used a LeetCode as a JS platform to test a code: https://leetcode.com/problems/clone-graph/description/

/**
 * Definition for undirected graph.
 * function UndirectedGraphNode(label) {
 *     this.label = label;
 *     this.neighbors = [];   // Array of UndirectedGraphNode
 * }
 */

/**
 * @param {UndirectedGraphNode} graph
 * @return {UndirectedGraphNode}
 */
var cloneGraph = function(graph) {
    //test for an empty input
    if (!graph) return null;

    var hashMap = new Map();
    var mainQueue = [];
    var start = graph;
    
    mainQueue.push(graph);
    hashMap.set(graph, new UndirectedGraphNode(graph.label));
    
    //use Breadth First Search to visit all nodes
    while(mainQueue.length > 0) {
        //take first element in the queue
        graph = mainQueue[0];
      
        //remove visited node from the queue
        mainQueue.splice(0,1);

        for (var i=0; i<graph.neighbors.length; i++){
            //add all unvisited neighbor nodes to the queue
            if (!hashMap.has(graph.neighbors[i])) {
                 mainQueue.push(graph.neighbors[i]);
                 hashMap.set(graph.neighbors[i], new UndirectedGraphNode(graph.neighbors[i].label));
             }
            //push neighbors into new graph
            hashMap.get(graph).neighbors.push(hashMap.get(graph.neighbors[i]));
        }
    }
   
    return hashMap.get(start);
    
};

//run a test
var aNode = new UndirectedGraphNode('0');
var bNode = new UndirectedGraphNode('1');
var cNode = new UndirectedGraphNode('2');

aNode.neighbors.push(bNode, cNode);
bNode.neighbors.push(cNode);
cNode.neighbors.push(cNode);

console.log(aNode);
console.log(cloneGraph(aNode));

Find a second largest number in array with Javascript

Back to the simple coding questions. This time with javascript – it seems the easiest language to test with.
Code for the problem can be run over here: http://js.do/code/svetliy-second-largest

var first_largest;
var second_largess;

function large(a_list){
if (a_list.length == 0 || a_list.length == 1)
	return "None";


if (a_list[0] > a_list[1]){
    first_largest = a_list[0];
    second_largest = a_list[1];
} else {
    first_largest = a_list[1];
    second_largest = a_list[0];
}

for (var i=2; i<a_list.length; i++){
    if (a_list[i] > first_largest){
        second_largest = first_largest;
        first_largest = a_list[i];
        continue;
    }

    if (a_list[i] > second_largest){
    second_largest = a_list[i];
    }
} 

return(second_largest);
}

var a_list = [1, 3, 4, 5, 0, 2];
document.write(large(a_list));

Finding a next lucky number with C++.

This is an easy task for Monday from Week of Code at Hackerrank.

The task is to find a next lucky number based on given 6 digit integer.

#include <bits/stdc++.h>
#include <string> 

using namespace std;

bool isLucky(int x) {
    
    int digit6 = x % 10;
    int digit5 = (int)((x % 100) / 10);
    int digit4 = (int)((x % 1000) / 100);
    int digit3 = (int)((x % 10000) / 1000);
    int digit2 = (int)((x % 100000) / 10000);
    int digit1 = (int)(x / 100000);
    
    if ((digit1 + digit2 + digit3) == (digit4 + digit5 + digit6))
        return true;
    else 
        return false;

}

string onceInATram(int x) {
    int i = x+1;
    
    while(true){
        if (isLucky(i))
            break;
        else 
            i++;
    }
   
    return to_string(i);
}

int main() {
    int x;
    cin >> x;
    string result = onceInATram(x);
    cout << result << endl;
    return 0;
}