An algorithm is a precise plan (in the format of a sequence of steps) on how to solve a particular problem.

learn more… | top users | synonyms

1
vote
0answers
11 views

Deepest left leaf node in a binary tree

Given a Binary Tree, find the deepest leaf node that is left child of its parent. This question is attributed to GeeksForGeeks. Looking for code-review, optimizations and best practices. ...
3
votes
0answers
25 views

Flatten a multilevel linked list

Given such a structure the output should be ...
2
votes
2answers
24 views

Calculating the number of prime numbers to solve the puzzle

How can the following program execution time improved? I have used dynamic programming in both "recursive" as well as "prime" function, but I'm not getting the efficient execution time. There is ...
2
votes
0answers
22 views

Reverse values of alternate nodes of the tree

Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree. Given tree: ...
0
votes
0answers
23 views

Find POJO by one field in List [on hold]

I often find myself having to write helper functions like these ...
0
votes
0answers
22 views

Return the next right node

Given a binary tree, return the next right node. This question is attributed to GeeksForGeeks. For example, consider the following Binary Tree. Output for 2 is 6, output for 4 is 5. Output for 10, 6 ...
5
votes
2answers
60 views

Possible combinations following a rule

The problem is the following: We have a number of months to buy a number of supplies. The sooner we finish buying them, the better, but we don't know how much we can buy per month and would like to ...
0
votes
0answers
25 views

Add two numbers represented by linkedlist

Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. It is not allowed to modify the ...
4
votes
1answer
50 views

Seeking improved Objective-C permutation algorithm

This algorithm seeks to find all of the possible permutations of a word: ...
7
votes
1answer
60 views

Response to an answer: Solution to maxMirror problem

Garrett Openshaw had an okay answer to Improvements on solution to Codingbat maxMirror problem? But there were a couple of issues with the code he gave, so I decided to review it and make it a little ...
0
votes
1answer
32 views

Using Comprehensions to Handle a Large Dataset in Python 2.7

I have a Python 2.7.6 script parsing large files (~60MB to ~2GB) containing lines of the following format: componentA componentB < floating point value > My goal is to sum the floating point ...
1
vote
1answer
66 views

Get the levels of a binary tree

Given a binary tree, return a list of each level. I'm looking for a general review and a mention on best-practices, optimization, and verification of my complexities. Time complexity: \$O(n)\$ ...
5
votes
5answers
95 views

Improving efficiency for two sum

I am trying to solve this problem: Given an array of integers, find two numbers such that they add up to a specific target number. and this is my strategy: For each ...
1
vote
2answers
46 views

Binary Insert Method - handling edge cases

I've created a function that uses binary search to insert an element into an array (similar to this question). I haven't benchmarked it, but based on my knowledge it should get \$O(1)\$ in the best ...
1
vote
0answers
16 views

C code for hash-consing: how could its performance be increased?

I've isolated this function, cons, for hash-consing in C. It is where the bottleneck of my program is. How could its performance be improved? ...
3
votes
1answer
15 views

Bit compressing in JavaScript

I have an bit array var data = []; ... and I have the following function: jsPerf ...
0
votes
0answers
35 views

How to avoid StackOverflowError caused by the number of recursion calls in Java? [closed]

I want to solve this problem: https://www.hackerrank.com/challenges/find-median ,i.e. find the median element in unsorted array. To do this I perform quickselect algorithm. My program works correctly ...
11
votes
4answers
193 views

I'm not sure if I still love Fibonacci, but my code is getting better. How much better?

Technically, this is a follow up to these two questions, but I have taken a radically different approach. Everybody Loves Fibonacci Does everyone still love Fibonacci I finally allowed myself to ...
-1
votes
0answers
34 views

Followup to BigInteger Cube Root Optimization

Followup to Followup: How do I optimize this Java cube root function for BigInteger? Here's my current code for this cube root approximation for BigInteger, which returns the integer part of the cube ...
1
vote
0answers
16 views

NGON (many polygons) on SPOJ - Time Limit Exceeded

I am trying to solve NGON problem. I am using bottom up dynamic programming here. Recurrence function is: ...
3
votes
1answer
33 views

How to generate valid number of combinations of datacenter and its host id?

I have a list of datacenters, which are dc1, dc2 and dc3. And each datacenter has six ids as ...
4
votes
1answer
66 views

Followup: How do I optimize this Java cube root function for BigInteger?

Followup to How do I optimize this Java cube root function for BigInteger? So I've tried several implementations of this algorithm. The version using only BigInteger sometimes results in a ...
2
votes
2answers
43 views

Merge N sorted list

Given n sorted lists, merge them. This is a fairly common question, attributed to plenty of interview question websites. I'm looking for code-review, optimizations and best practices. I'm also ...
9
votes
4answers
279 views

Threshed and Malachi'd: Sieve of Eratosthenes

I took a little bit from all the answers to my previous question Threshing: Sieve of Eratosthenes. This may be bordering on code-golfing, but I think that I have a pretty good piece of code here. ...
3
votes
1answer
39 views

SPOJ is giving TLE for this solution of ROCK

I am solving ROCK problem on SPOJ. I have used bottom up dynamic programming approach for this problem. Time complexity for this is \$O(n^3)\$. ...
6
votes
3answers
158 views

Iterator of iterator

Given an iterator of iterators, return all of the elements from the iterators in order. Looking for code review, optimizations, and best practices. Verifying complexity to be \$O(n)\$, where \$n\$ is ...
2
votes
1answer
86 views

Why are these functions slower than BigInteger's included methods?

I tried writing alternative functions to Java's BigInteger.add(BigInteger.ONE), BigInteger.add(BigInteger), and ...
5
votes
4answers
362 views

Threshing: Sieve of Eratosthenes

I would like a complete threshing of this code so that I can see what I did wrong and what I am using incorrectly. I made this super simple, trying to learn a little bit about ...
6
votes
1answer
54 views

Longest Common Substring solution for a string and a list

Recently I have discovered one thing, which makes me feel frustrated. The Perl is lack of syntax sugar for strings, to do the same things, those are applicable to lists. The implementation I posted ...
5
votes
2answers
160 views

How to generate valid number of combinations basis on the rules?

I have a list of datacenters, which are dc1, dc2 and dc3. And each datacenter has six ids as ...
3
votes
1answer
41 views

Automatically gravitate popover towards page center

I've authored a couple of plugins which show popovers on a given element. One of the objectives for both was no specification of plugin direction. So instead of the implementing dev having to specify ...
1
vote
0answers
33 views

Find K distant nodes

Find all nodes, which are at a distance of k from the input node. Input: target = pointer to node with data 8. ...
4
votes
0answers
49 views

Fibonacci number function: convert to tail-recursion?

I've been implementing a function for calculating the nth Fibonacci number in F#. So far the best implementation I could come up with is this: ...
6
votes
3answers
69 views

Searching all diagonals of a 2D M x M array

I've started writing a piece of code to help me search for an object in all the objects found in the diagonals of an M x M 2D array. Though the code works, I'd like to know if there is a way I can ...
4
votes
1answer
50 views

Product of corresponding elements in 2 arrays

Given two arrays A and B, of length N and length k ...
5
votes
1answer
44 views

Canonicalizing a large set of addresses using many regex substitutions

I have a script that is standardizing a large amount of data in the database. The standardization involves applying over 500 regular expressions to the data. Here is some quick pseudocode: ...
8
votes
5answers
747 views

Splitting company name into 3 strings

I wrote an algorithm which should cut a companies name into 3 strings. Input: 1 String Output: 3 String. Conditions: String 1 2 and 3 shall not be longer then 35 signs. If the Input ...
-2
votes
1answer
86 views

Given a BST, transform it into greater sum tree

Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node. Diagram is here. Looking for code review, optimizations and best practices. ...
1
vote
1answer
55 views

Solving the coin change algorithm using 10 coins

I'm solving the coin change problem having target of n and upper bound as n. Also the maximum number of coins is 10. For example. if the target is 11. then the possible outcomes are - ...
6
votes
2answers
131 views

Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree

Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree. Given tree: ...
4
votes
1answer
82 views

Parallelizing scrypt key-derivation function

To review my use of multiprocessing, I don't think it is at all necessary to understand the algorithm, but it's the scrypt key-derivation function. This uses ...
6
votes
1answer
108 views

Finding all paths from a given graph

I need to find all paths from a given graph. I can do that for now, however my recursive code is not efficient and my graphs are very complicated, hence I need a better algorithm. ...
3
votes
1answer
113 views

Minimum number of letters removed, to make a string Peragram

Here is the link to the problem Peragrams. Peragrams: If a word is an anagram of at least one palindrome, we call it a Peragram. Problem: Given a string, find the minimum number of letters ...
7
votes
1answer
125 views

How do I optimize this Java cube root function for BigInteger?

I find that this function is one of the biggest causes of slow program execution. I can write a square root version with BigInteger only, but with the cube root, the algorithm sometimes gets caught in ...
0
votes
0answers
31 views

Detect if a tree is complete binary tree

Detect if tree is complete binary tree. This question is improvement over previously asked here. Looking for code-review, optimizations and best practices. Verifying both space and time complexity to ...
2
votes
1answer
28 views

Trie SPOJ Problem - PhoneList TLE

In this problem you are given input of phone numbers and you are supposed to determine whether any number is the prefix of another. I have an addchild method that ...
4
votes
0answers
56 views

Thinking in 'Haskell' - when to use (state) Monads

I really enjoy Haskell but feel I still have a total beginner's style, and would like to move beyond that. The code below - for Dijkstra's shortest path algorithm - is a case in point. I feel as ...
2
votes
4answers
99 views

Maximum of all subarrays of size k

Given an array and an integer k, find the maximum for each and every contiguous subarray of size k. Examples: Input: ...
5
votes
2answers
141 views

Find max-number div by three to be formed by array elements

Given an array of non-negative integers, find the largest multiple of 3 that can be formed from array elements. For example, if the input array is {8, 1, 9}, the output should be "9 8 1", and ...
5
votes
1answer
53 views

Generic Graph Library Part 2 : Algorithms

(This is a follow on to this question). Having given an implementation for some of the graph structures, here is a portion of the algorithms that are defined to work over them: graph_degree.hpp ...