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.

learn more… | top users | synonyms

4
votes
5answers
318 views

Rearranging numbers to get the largest number

Given a Integer, find the maximum number that can be formed from the digits. Input : 8754365 output : 8765543 Below is my solution: ...
4
votes
1answer
27 views

for_each_cons and for_each_slice, variants of std::for_each working on sliding windows

My goal was to replicate Ruby's each_cons and each_slice: ...
2
votes
0answers
55 views

Find largest rectangle

I solved this challenge and it passes all the test cases. Problem Statement There are N buildings in a certain one-dimensional landscape. If you join K adjacent buildings, they will form a ...
0
votes
0answers
20 views

Correlation and Dependence [on hold]

It is well known that 0 dependence implies 0 correlation, while 0 correlation does not imply 0 dependence. I have a paper here illustrating nonlinear correlation coefficients from relationship ...
-1
votes
0answers
25 views

Algorithm PHP - Optimize code for finding minimum array integer

Im trying to solve one simple task and on first look it is working. Every advice how I can optimize this code will be precious. So one fast description of the task : You have a file from which you ...
3
votes
0answers
15 views

Improving my build_max_heap algorithm [on hold]

I'm new to such complicated (for me) data structures as binary trees now. I think I've made many mistakes in my build_max_heap algorithm and it can be improved. The rule I'm using is that smaller ...
6
votes
5answers
875 views

Sort three numbers using only if-statements

Beginner here, trying to make a small program that sorts three numbers from smallest to largest only by using ifs. Any thoughts on how to improve this? ...
1
vote
0answers
13 views

Calculate total width of a range of numbers

I'm mostly curious if there's a faster or more efficient algorithm I could use. I created this for a unique side project, as well as for an issue at work, so what the function does (e.g., return a ...
3
votes
2answers
44 views

Search key in a rotated sorted array without repetition of elements

Is there any improvement possible in this implementation? //Search in sorted rotated array ...
3
votes
1answer
74 views

Find the kth largest element of an array

Find the kth largest element in an array of integers. You may assume that 1 <= k <=size(array) Any comments? Is there a more efficient solution? ...
5
votes
1answer
44 views

Implementation of the change making algorithm

I wrote a set of python classes for solving the change making problem, in various forms. Given the coin denominations and values: Find all possible ways of making change. Find a best way ...
8
votes
3answers
692 views

Project Euler #4 - Largest Palindrome Product in Java

I am seeing that this is actually not in accord with the difficulty level said there. Firstly I cannot list all possible improvements it could have. ...
1
vote
1answer
37 views

Track the highest high in a time series

I want to find the highest number in a time series, in the most efficient way. To avoid iterating through all the look back periods, I'm only iterating in case the current high number has just been ...
3
votes
3answers
55 views

Number of right cyclic shifts to give the biggest number

I am preparing to coding interview and I met this task today: You are given 30-bit unsigned integer N. A right cyclic shift of N by K bits is the result of performing a right cyclic shift of N by one ...
3
votes
3answers
67 views

Letter combinations of phone dial pad number

In preparation for interviews, I have written a solution to this question: Given a sequence of numbers (34128) and an input map such as a dial pad on a phone (2->[a,b,c], 3->[d,e,f], ...
4
votes
1answer
51 views

Merge sort implementation in JS

Just for learning and practicing I've created a completely recursive version of the merge sort algorithm. I've tested it a little bit and it seems working, but I'm quite sure that it can be largely ...
3
votes
1answer
38 views

Safe cracker string with all combinations

Imagine a safe with a 4-digit code, and accepting a continuous stream of code entries, such that when the 4 digits are seen in the right sequence, the safe opens. Generate a short string that ...
1
vote
0answers
24 views

Find minimum number of coins (count and list of coins too)

For a given set of denominations, you are asked to find the minimum number of coins with which a given amount of money can be paid. Assume that you can use as many coins of a particular ...
1
vote
0answers
33 views

A* search algorithm in Clojure

Cost of nodes are represented by a matrix (called world), heuristic cost estimate/g score/f score are all represented in matrices. The world and its size are ...
9
votes
1answer
72 views

Mathematical expressions evaluator with callbacks, the logic

I hesitated quite a bit of time before posting this question since the code to review is more or less a monster (with regards to its size). It is a basic mathematical expressions evaluator, but with ...
-1
votes
0answers
15 views

Kth selection routine - Floyd Algorithm 489 [migrated]

I have recently come across a promising Kth selection routine that reportedly outperforms quickselect the Floyd, Rivest Select routine. This Wikipedia article provides a pseudocode version which I ...
4
votes
1answer
91 views

Password Attacker (Google apac test problem)

Original Problem Short Summary: Given a \$M\$ possible characters all of which must be used at least once, find the number of possible combinations of length (exactly) \$N\$. The ...
5
votes
2answers
62 views

Sum of successive powers of digits

Some numbers have funny properties. For example: $$\eqalign{89 &→ 8^1 + 9^2 &= 89 &= 89 × 1 \\ 695 &→ 6^2 + 9^3 + 5^4 &= 1390 &= 695 × 2 \\ 46288 &→ 4^3 + 6^4 + 2^5 + 8^6 ...
1
vote
3answers
154 views

Isomorphic Strings

Two strings are isomorphic if the characters in s can be replaced to get t. Can efficiency be increased further for this code? ...
-4
votes
0answers
12 views

How to feed data Excel file from C# language that can be retrieved? [closed]

Guys I need to get the code to to insert data from C# to excel file that already connected;for the application sending sms from desktop to cell phone by invoking info from excel file which already ...
-3
votes
0answers
21 views

Diagonal Sudoku [closed]

I implemented a diagonal sudoku. Diagonals must not have repetitions My algorithm to check the diagonal was. copy diagonals to arrays left_x and ...
5
votes
2answers
238 views

Longest substring with unique characters

Find the length of the longest substring without repeating characters Any comments on my code? ...
2
votes
2answers
109 views

The longest subsequence with specified difference between min and max

As an input you have non-empty zero-indexed array of integers. The goal is to find the longest subsequence (any sequence obtained by removing some elements from A), where difference between min value ...
0
votes
1answer
33 views

Sum of factorial digits [closed]

I have seen one algorithmic task and I am not sure if my solution is fully efficient. As input to the method you are given positive integer N. And you should return the sum of values of the digits in ...
0
votes
0answers
44 views

A hint on improving this algorithm's complexity [closed]

I'm currently working on improving the following algorithm (Yes, it is a school assignment). It's not going as well as I had expected though. Here's the pseudocode for the algorithm (dumbest way): ...
32
votes
3answers
3k views

Sorting millions of integers

Last Friday I was hit with a sorting interview question that I never really had to deal with. Develop a your own sorting algorithm. It cannot use any other Classes for help. It needs ...
3
votes
1answer
69 views

Texas Hold em Poker Hand recognition algorithm and implementation

I am designing an in-depth poker game. I am first focusing on recognizing the strength of a hand given the set of cards. Is the following algorithm suitable for the stated purpose? Am I using ...
9
votes
5answers
1k views

Finding the smallest number whose digits sum to N

I tried my hand at a grade XII Board exam program: Given two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100, find the smallest integer that is greater than M ...
5
votes
1answer
77 views

Super2048 (Google apac Test Problem)

This is my second try at writing Ruby. Stylistic feedback is very much appreciated. The problem: 2048 is played on a simple 4 x 4 grid with tiles that slide smoothly when a player moves them. ...
5
votes
2answers
74 views

Project Euler #9 in C: Special Pythagorean triplet

I have finished Project Euler #9 on HackerRank: Given \$N\$, Check if there exists any Pythagorean triplet for which \$a+b+c=N\$, \$a^2+b^2=c^2\$. Find maximum possible value of \$abc\$ among ...
10
votes
5answers
192 views

Word frequency in a large text file

I've am trying to read a large text file and output the distinct words in it along with it's count. I've tried a couple of attempts so far, and this is by far the fastest solution I have come up with. ...
0
votes
0answers
29 views

Algorithm for correcting missing values

I have problem with setting missing values in data frame. In the first 3 columns there are ID of product, ID of store, and number of week. There are also 28 columns from 4 to 31 corresponding to last ...
5
votes
1answer
63 views

Sort a scrambled itinerary (Google apac test problem)

This is my first time writing ruby - and I was mainly hoping to get some feedback and stylistic pointers. The algorithm is pretty straight-forward, since Google guarantees airports aren't repeated I ...
3
votes
2answers
85 views

Sudoku solver using forward checking

I was compelled to look into a Sudoku Solver in Java using the principles I have learned in a course, namely I wanted to make something that included backtracking and forward checking. Has anybody ...
2
votes
1answer
33 views

Solving Diophantine equations - reloaded once

Inspired by this question, I was curios, if I could solve it for large n. For n=6209, its fifth power overflows ...
2
votes
1answer
82 views

Count balanced substring

Chandan got bored playing with the arrays all the time. Therefore he has decided to buy a string \$S\$ consists of \$N\$ lower case letters. Once he purchased the string, He starts formulating his own ...
3
votes
1answer
75 views

Project Euler #5 - Smallest Multiple

JavaScript is my first programming language, and I'm still pretty new. I'm just looking for feedback. What can I do to make this more efficient and handle larger numbers? ...
2
votes
2answers
79 views

Merge Sort algorithm

I just read and experimented with C++ implementation of the merge sort algorithm from the Wikipedia page. During the experiments I have slightly modified the source code and would like to know how ...
3
votes
3answers
146 views

Speed up large calculation of intersections

I have the following data: trains is a dictionary with 1700 elements. The keys are the IDs of trains and the value for each train is an array with every station ...
2
votes
1answer
53 views

Doing tree search in Java

I have this tiny library for building unbalanced, binary trees and doing tree search on them. I tried hard to define a generic API that would allow custom implementations of trees. ...
2
votes
1answer
51 views

Recursive Sudoku Solver

I just finished up a program which takes a byte[9][9] as input and recursively does a DFS to find any solutions (including multiple solutions). I'm pretty happy ...
6
votes
1answer
42 views

Method for Access to a Resource with 'Retry' Logic

I am trying to implement so me 'retry' logic in a Data Access layer where the DB occassionaly times out (and the method is called several thousand times a day). This is the best I can come up with, ...
3
votes
2answers
93 views

Multiple longest common subsequence (another algorithm)

Here is the algorithm: Input: a set of \$M\$ strings \$S=\{s_i\}\$ of length \$N\$ with alphabet \$\Sigma\$ Make an index of each string by chars. Maintain a position \$p=(p_i)\$ where \$p_i\$ is an ...
6
votes
2answers
43 views

Recursively evaluate neighbors in a two dimensional grid

I'm creating a puzzle mini game where there is a board of gems, varying in color, and chosen at random. This is what my code generates: When you click on a gem the game should traverse the board ...
0
votes
1answer
45 views

Sorting algorithms - Relocation Counter

I want to count the total relocations for each of these sorting algorithms. From the results it seems my code is correct, but I need someone to confirm it. Is the counter variable placed in the right ...