Recurse through a dynamic array

I am looking for a generalised solution for the following snippet:: var d = [[1,2,3], [1,2], [1,2,3,4,5,6]]; d[0].map((val1, index1) => { d[1].map((val2, index2) => { d[2].map((val3, index3) => { console.lo...
more »

2017-03-15 16:03 (2) Answers

Prime numbers generator explanation?

I was searching for an algorithm to generate prime numbers. I found the following one done by Robert William Hanks. It is very efficient and better than the other algorithms but I can not understand the math behind it. def primes(n): """ Returns...
more »

2017-03-15 14:03 (1) Answers

Algorithm translate a number to String

I need to design an algorithm where each number is encoded to an alphabet, for example: 1=A, 2=B, 3=C...26=Z Given a set of numbers, I have to translate them to a combination of strings. For example: 123 can be translated to - ABC(123), AW(...
more »

2017-03-13 17:03 (4) Answers

Computional complexity of sorted array

Currently, I am doing a project for my university, where I do implement known data structures (array, linked list, BST, etc.) and I have to measure the times some operations on them require. For example, the first one for me was array. I've measured ...
more »

2017-03-11 12:03 (2) Answers

Sort algorithm and distance between number

Let's say we have an array filled with long long unsigned int. The distance between each adjacent elements is small. For example we have : [0,1,0,1,0,1] We have another array of the same size and the distance between each adjacent element is now sig...
more »

2017-03-10 19:03 (1) Answers

Sorting biggest and smallest value in array C++

This is a very easy and common exercise although I'm coming across an error I can't seem to understand and I can't find an explanation anywhere as it might be too specific. The program simply prompts user to input how many pancakes were eaten by Per...
more »

2017-03-10 19:03 (3) Answers

javascript change elements in array

I have an array of objects. Then I want to add another object and stick it to the one that already existed in my array. Which means the index of the new object should be larger by one in relation to my already existed object and rest of the element's...
more »

2017-03-10 09:03 (2) Answers

fastest way to find unique pairs in a 2d vector

I'm looking for an efficient and fast algorithm to find the unique pairs in a 2d vector. For example: vec=[[1 5] [2 2] [1 5] [3 1] [6 3] [2 2]] I want to generate the 2d vector below which only have the unique pairs. vec=[[1 5] [2 2] [3 1] [6 3]] D...
more »

2017-03-06 03:03 (4) Answers

LSD Radix Sort for Integers

I'm having trouble wrapping my head around using radix sort for a group of fixed-length integers. In my below attempt to implement least significant digit radix sort, I have a function called num_at which returns the digit d in the number num. The co...
more »

2017-03-05 22:03 (0) Answers

How to Implement A Basic Search in Dicts

I have a list of dicts as below: [ {"name":"Some", "surname":"Body","age":22}, {"name":"Some", "surname":"One", "age":23}, {"name":"Any", "surname":"Body", "age":20} ] I need a function which makes search over dicts as below: def sear...
more »

2017-02-27 20:02 (1) Answers

Find on custom object

I provide a function object, find_name, to search for specific object matching a name, but it turns out I'm doing something wrong: #include <iostream> #include <string> #include <vector> #include <algorithm> struct Item { ...
more »

2017-02-26 18:02 (1) Answers

Ruby Recursive Algorithm Issue

Working on the algorithm: Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen. Note: A word cannot be split into two lines. The order of words in th...
more »

2017-02-26 01:02 (2) Answers

Generate numbers from n to m from a list

I'll start with an example; given n = 1 and m = 100 and a list [1, 2, 3] generate all numbers with 1 digit and two digits and so on but they need to be less then 100 in this case. Output: - 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33 Then we stop ...
more »

2017-02-25 22:02 (4) Answers

Using heap in ruby algorithm

Working on the following algorithm: Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of uni...
more »

2017-02-25 19:02 (2) Answers

Addition of every subset of two multiplied

I have an array with the elements {7,2,1} and the idea is to do 7 * 2 + 7 * 1 + 2 * 1 which is basically this algorithm: for(int i=0;i<n-1;++i) for(int k=i+1;k<n;++k) sum += a[i] * a[k]; Where a is the array in which I have the nu...
more »

2017-02-24 18:02 (4) Answers

Find Leaves of Binary Tree

Working on following problem: Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Example: Given binary tree 1 / \ 2 3 / \ ...
more »

2017-02-24 01:02 (2) Answers

Multiple subset sum calculation

I have 2 sets, set A contains set of random numbers and set B's elements are sum of set A's subsets. For example, A = [8, 9, 15, 15, 33, 36, 39, 45, 46, 60, 68, 73, 80, 92, 96] B = [183, 36, 231, 128, 137] I want to find which number is sum of w...
more »

2017-02-23 18:02 (2) Answers