An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.
3
votes
1answer
30 views
Predict the best day to buy and sell the shares
I was trying out my hand at solving problems and came across this HackerRank problem. I have written a solution which passes for a few test cases but times out for most others (time limit - 3 seconds)....
1
vote
1answer
29 views
print tree level by level including Empty node
Here is my code to print tree in a level by level way, need to print empty node as well to keep tree structure like full binary tree for elegancy reason. Wondering if anything you think could improve, ...
1
vote
1answer
25 views
Find Intervals with Most Overlaps
Given a list of time intervals, I want to return the intervals with the most overlaps (i.e. most people free). My solution runs in \$O(n \log n)\$, but I made a bunch of "tracker" variables. How can I ...
1
vote
0answers
38 views
Choosing python data structures to speed up algorithm implementation
So I'm given a large collection (roughly 200k) of lists. Each contains a subset of the numbers 0 through 27. I want to return two of the lists where the product of their lengths is greater than the ...
0
votes
0answers
22 views
Rating calculator [on hold]
I want to rank content by rating, giving them a balance of "freshness" and "total votes up"
This is what I've come up with.
...
1
vote
0answers
28 views
Wikipath stack in Java - Part I/IV - The search algorithm
I am working on the following small software stack:
The objective of the entire stack is to search for shortest paths in the Wikipedia article graph as fast as possible. As of now, I rely on two ...
7
votes
7answers
959 views
Find out the 2 numbers which gives the highest product
Input given is an int array, which may or may not contain positive, negative or zero values. Write a
program to find out the two numbers which gives the highest product.
(Both numbers should be ...
1
vote
1answer
31 views
Check whether a given sequence can be rearranged using a stack
Working on a problem, for a given input sequence to a stack, check whether it could produce (using stack push/pop operations) the output sequence.
For example if input is ...
2
votes
0answers
53 views
betting strategy simulation
So lately I got interested into betting systems, dice strategies and algorithms. I took some time to think about it and eventually ended up with an algorithm of my own.
Considering that I don't have ...
0
votes
2answers
55 views
Calculating sum after ranged-based subtractions
The puzzle consists from something like this:
You will receive an array of integer with values, then you will receive X numbers. Each number (call it x) represents ...
7
votes
2answers
57 views
Sort items in a list by grouping rules
Problem Statement
Given a list of items each having a type associated with it, group them by the below mentioned rule
The list should not contain consecutive items that violate the ...
6
votes
3answers
1k views
Buy once and sell once for maximum profit
Suppose we have stock price history, and we are allowed to buy only once and sell only once. The problem is to find the maximum profit we can make. Here is my code that I tested for basic test cases. ...
-3
votes
0answers
60 views
Find out the maximum positive sum possible from array? [on hold]
I was attempting this question at hackerearth : https://www.hackerearth.com/problem/algorithm/once-upon-a-time-in-time-land/description/#c64471
In some test cases it is giving StackOverflow Error. ...
4
votes
1answer
56 views
Pairs and differences and correlations
This has a couple of twists which I personally found rather awkward... All code can be downloaded in a read-to-run format from here
The problem
Consider a classic interview question: "Find given ...
0
votes
1answer
27 views
Implementation of the binary search algorithm [on hold]
This is my first implementation the binary search algorithm. I have tested it and so far it is okay. But I need an experienced eye to review the code and recommend best practices.
...
5
votes
1answer
105 views
Spell checker using trie
This program implements spell checking by loading a given dictionary file into a trie structure and comparing against a given text file: ...
3
votes
2answers
66 views
HTML parsing algorithm for extracting <a> tags
My intention is to create a complete HTML parser, so far I made a basic algorithm that iterates trough text and extracts everything in an "a" tag.
It works on everything I tried, but I want a review ...
-2
votes
0answers
30 views
How can I speed up the following function in R?
I wrote the following function in R. I want to iterate it, say 50000 times. I used "sapply" in my function but it runs slowly in R.My PC is still working about 20h now and I have no idea about the run ...
2
votes
3answers
52 views
Generating variables to be named, filled in and operated on the fly in Python
I am very new to Python and trying to refactor my code which is ripe for error.
Currently, I manually name a bunch of arrays:
...
2
votes
1answer
49 views
Counting all substrings with exactly k distinct characters
Problem Statement : Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that has exactly k distinct characters. Example:
Input: abc, k = 2 Output: 2 ...
4
votes
2answers
36 views
Finding an equilibrium index in an int array
Here is a programming challenge from codility
A zero-indexed array A consisting of N integers is given. An
equilibrium index of this array is any integer P such that 0 ≤ P < N
and the sum ...
5
votes
1answer
100 views
The Palindromic Odometer Puzzler: A Programmatic Solution
That is a programmatic solution to a puzzler from Car Talk, its summary is:
"I noticed that the last 4 digits were palindromic. I drove a mile,
and the last 5 were palindromic. I drove another ...
3
votes
1answer
67 views
My Quick sort examination
I had a test on sorting using quick sort algorithm, which I have implemented. It's working fine.
But my examiner is not satisfied. They say that the solution which I provided is not a proper ...
-4
votes
0answers
20 views
Testing if the arc between two (lat,lon) seapoints, intersect landmass [closed]
I have the following problem: I have a bunch of points (latitude,longitude) that I know are located in the mediterranean sea. I have to check if the great circle path among each pair intersect the ...
2
votes
1answer
29 views
Dynamic Official Receipt increment algorithm
I made a function that would automatically generate official receipt for POS. it is able to accept format such as xxx, xxx-xxx, xx-xx.
But my code is really messy and horrible and it needs review. I ...
2
votes
1answer
46 views
Total number of stops for an elevator to serve certain people
I have seen this question asked around a bit, such as this Code Review question in Java:
Write an optimal program that would determine the total number of stops
a elevator has taken to serve X ...
0
votes
0answers
18 views
Real Coded Genetic algorithm performance issue [closed]
I am pretty new to genetic algorithms, and wrote a real valued genetic algorithm with an sbc function used for crossover. Currently I was trying to use the class below to train the weights for ...
2
votes
0answers
67 views
Solving the Mining algorithm from HackerRank
I was working on this problem for a few hours last night and finally came up with a brute-force solution. The task is to report the minimum work necessary (sum of weight × distance) to relocate gold ...
4
votes
1answer
59 views
“Similar Destinations” challenge
I am currently solving the Similar Destinations challenge on HackerRank and am in need of some assistance in the code optimization/performance department. The task is to take a list of up to 1000 ...
7
votes
1answer
90 views
Cipher text using a 2D array
I have a simple program which ciphers text using a 2D array. You specify what letters to replace with what and it does it.
I understand the code is very repetitive and that I should probably use ...
6
votes
2answers
120 views
Accepting user defined functions for custom map reduce functionality in C++
I am implementing map and reduce - style functions for processing geospatial raster datasets.
I would like the ...
10
votes
2answers
453 views
Project Euler #2: Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will
be:
\$1, 2, 3, 5, 8, 13, 21, 34, 55, 89...\$
By ...
8
votes
1answer
134 views
Heapsort implementation in C++14
Please review the following implementation of heapsort and pay attention to the following:
Have I correctly chosen the names InputIt and ...
4
votes
2answers
83 views
Merge sorting a singly-linked list in Java - follow-up
In this iteration, I incorporated all but one points made by forsvarir in the previous iteration. Now my code looks like this:
LinkedListNode.java:
...
2
votes
3answers
68 views
Java number formatting in accountant style
I have written a string number formatter. I would like to know if this can be improved and any suggestions with how it's done.
The purpose of this function is to convert a double that has been ...
3
votes
2answers
68 views
Merge sorting a singly-linked list in Java
I have this nifty merge sort implementation for sorting singly-linked lists. Its running time is \$\Theta(n \log n)\$, yet it uses only \$\Theta(\log n)\$ space (the stack). See what I have below.
...
1
vote
2answers
59 views
Selection sort algorithm examination
I had an algorithm test.
I was asked Selection sort algorithm to program.
I coded it in javascript and shown to examiner.
But he was not satisfied, he said its too complicated.
I think I have ...
5
votes
2answers
51 views
Finding longest common prefix
I have been trying to solve a modification of the Longest Common Prefix problem. It is defined below.
Defining substring
For a string P with characters P1, P2,…, Pq, let us denote by P[i, j]
...
2
votes
2answers
41 views
Implementation of insertion sort in JavaScript
I have written an insertion sort algorithm in JavaScript:
...
10
votes
2answers
189 views
Displaying all substrings of some given string
I solved exercise 8 from Core Java for Impatient, chapter 1. This exercise is to implement a displayAllCombinations method to display all substrings of the given ...
6
votes
3answers
385 views
Save the Prisoner
A jail has prisoners, and each prisoner has a unique id number, S, ranging from 1 to N. There are M sweets that must be distributed to the prisoners.
The jailer decides the fairest way to do this is ...
5
votes
3answers
85 views
A command-line calculator using infix to postfix in C++
I was in the mood for some C++ and decided to write a command line calculator, which understands addition, subtraction, multiplication and parentheses. See what I have:
main.cpp
...
3
votes
2answers
38 views
Getting the smallest snippet from content containing all keywords
This returns the smallest snippet from the content containing all of the given keywords (in any order). This provides the correct solution but I would like to know if it can be made more efficient.
<...
1
vote
1answer
34 views
LRU cache in Python
Here is my simple code for LRU cache in Python 2.7. Appreciate if anyone could review for logic correctness and also potential performance improvements.
A confusion want to ask for advice is, I am ...
5
votes
1answer
61 views
Stampcalculator - Given a set of stamps, what combinations are there to reach a certain amount?
Background
My mother has a hobby of buying and reselling books via online trading sites. After a price is agreed on, the books have to be put into an envelope for mailing. On this envelope, stamps ...
0
votes
0answers
30 views
HackerEarth Prateek and his Friends non-contiguous
How to solve this problem for non-contiguous range?
Prateek wants to give a party to his N friends on his birthday, where each friend is numbered from 1 to N. His friends are asking for a gift to ...
3
votes
2answers
104 views
Quick Sort in Java
I recently started learning about algorithms and data structure's. I decided that for every algorithm I learn about I'll implement it without looking at any pseudo code or actual code.
I have ...
2
votes
0answers
32 views
Unbounded knapsack solution with items of equal value
My solution correctly solves the problem, but I would like to optimize its performance. For a capacity of 7 and bars = [ 4, 1, 1, 2, 1], the output is 7.
...
1
vote
0answers
31 views
Comparing a recursive and iterative traveling salesman problem algorithms in Java
This snippet is about two (brute-force) algorithms for solving the traveling salesman problem. Since there is plenty of boiler-plate code, I arranged a GitHub repository for the entire program, in ...
2
votes
2answers
48 views
AVL tree insertion and deletion of nodes in C. 2.0
I asked a question yesterday, based on the answers to that question and some personal insights I was able to update the original code. which I am posting here to get reviewed. I also thought about ...