# Questions tagged [algorithm]

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.

4,919 questions
Filter by
Sorted by
Tagged with
8k views

### Finding the minimum difference in a list of numbers

I have written following code to find the minimum difference from a list of numbers. Because I am using a loop once and LINQ again to find the minimum, the algorithm is O(N2). Can you please tell me ...
• 133
3k views

### PHP code to scale images and generate thumbnails

I have written a piece of code by taking help from this link. For generating thumbnail, I create a temporary scaled image and then copy from that image. I am assuming that it is resource intensive. ...
• 283
4k views

### Randomly Permute Elements in a List

I want to randomly permute a finite list in the most effective and efficient way in C#. My attempt is as follows. ...
908 views

### Prettify this code - combine items in a list

Can anyone help me prettify this code? The idea is to combine adjacent time spans so [0:00-1:00, 1:00-2:00, 3:00-4:00] becomes [0:00-2:00, 3:00-4:00] ...
• 240
2k views

...
• 343
306 views

### Devise an algorithm of complexity O(N) finding No of 1's from a 2 dimensional matrix[N][N] containing 1's and 0's

Assume a 2D [n][n] matrix of 1's and 0's. All the 1's in any row should come before 0's. The number of 1's in any row I should be at least the number of 1's row (i+1). Find a method and write a C ...
714 views

### determining if a range is within array of ranges

I have a List of a custom structure (myStruct) which has two ranges stored in it. I'm trying to write a function where I can figure out if a range is completely enclosed in any of the ranges. Is this ...
368 views

### "The broken pedometer" optimizations

This is a solution to the problem: "The Broken Pedometer" on the UVA online judge. (Note: I can not use anything beyond what is in g++ 4.6 which is what is used for the competition I'm training for.) ...
• 452
1k views

### Binary tree max sum level - better design?

I have written some code for finding a level in a binary tree, having a maximum number of elements. I have a few questions: Is it a good design? I have used 2 queues but the total sum of elements ...
• 183
237 views

### First steps in Scala: does this look idiomatic?

I an a newcomer in the Scala world, and I would like some advice from more experienced people to find out whether what I am writing goes in the direction of idiomatic code. In particular, I have to ...
• 437
126 views

### How to refactor a verbose datatype?

This morning I decided to write this question: https://stackoverflow.com/questions/11727571/are-there-problems-if-i-have-a-verbose-datatype/11727723#11727723 I don't really explain my scenario about ...
• 561
2k views

### Pairwise Combinations Generator

Below is an algorithm to generate all pairwise combinations for a set of random variables. See here for an explanation of what constitutes a minimal all-pairs set. The algorithm below works but I feel ...
• 408
14k views

### Base-36 encoding of a byte array

After posting a question about Alphanumeric Hash generation on StackOverflow, the most helpful answer was to change the conversion method from pulling 5-bit chunks of a binary hash value, to instead ...
• 1,685
177 views

### Is there a better way of defining custom calendars?

I need to define custom calendars and, in particular, test a DateTime for being a holiday. My current code is shown below. Is there a more concise/better way of ...
20k views

### Definite integral calculation in C#

How do I better calculate the definite integral? I am using a function to integrate and another to find the factorial recursively. I'l like to better the algorithm or the efficiency or even the ...
2k views

### Simple word counter

I've decided to do this by writing a simple word counter. The app gets all the params and outputs all the unique words, each one with a counter: "Hello world Hello" would return "Hello: 2", "world: ...
• 205
12k views

### Algorithm to find substring in a string

Can this be improved? ...
• 502
958 views

### Dynamic two-column list, "Vertical Wrap"

I have created a very simple script that will create two columns which are populated by an array. It works, but I am certain that the way that I have gone about it is not the best way. I have been ...
• 167
1 vote
4k views

### Tackling the TSP with a genetic algorithm

This is my first go at genetic algorithms, using PyGame for the visual representation. Is there a way to improve the speed of the code? Click to place a point, press ↑ to start looking for the ...
• 155
65k views

### Deleting a node from a binary search tree

I have read about it at a few places and tried to write my own version. I would like to get it reviewed. ...
• 201
364 views

### PHP function to compare two Roman numerals

This was for an assignment. I had to write a function to compare two roman numerals. This had to return true only if the second number was larger than the first and ...
6k views

### Tree-traversal without recursion

I wrote the following code for printing all the paths to the leaf nodes in a tree (NOT a binary tree ) without using recursion. I have used a stack and performed dfs on the tree. Whenever I reach a ...
• 53
677 views

### How can I remove unwanted combinations from my algorithm in C#?

I have written an algorithm in C#. It is recursive, but it is not optimal. Maybe you have suggestions for improvement? (I already posted this question on stackoverflow but the question was closed ...
• 131
1k views

### Path sum - Dijkstra's algorithm in F#

I'm learning F# and I've decided to solve Project Euler's Problem #81 with Dijkstra's algorithm. In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only ...
• 311
14k views

### Printing stars using nested loops

I just got back from my interview where I had to solve the following problem: Write a C++ program that prints the following pattern using nested for loops. <...
• 315
320 views

### Storing three integer values

Consider an 8-bit complier, in which we have an integer of size, say 2-words i.e. 16-bits or 2 bytes. You need to store three integer values which are supposed to be as: ...
• 173
172 views

### Timed series of events. Logic for organizing events based on time? [closed]

I would like to execute a series of events at the time at which they occur. The events are stored in a list and always sorted by time of execution. ...
• 173
755 views

### Printing all subsets

I have tried to incorporate comments from my previous code review into this. So, I am hoping for less mistakes. If you spot any, please let me know mercilessly! ...
• 259
5k views

### Find all single elements in an array

I am interested in printing all numbers that do not have a match in an array. Example: 1,3,4,5,3,4,5,5,6 result 1,5,6 Please ...
• 627
5k views

### Splay tree implementation

I am looking into splay trees and I implemented a version. It seems correct. Could someone please let me know if this indeed correct and how to improve it? I will just show the ...
• 627
100 views

### List creation of BST's nodes

I want to create lists of the nodes of a binary tree at each depth. So if the depth is X I would have X lists. Please see my approach. I think it is correct. Am I right to believe it is correct? Any ...
• 627
1 vote
497 views

### 2vs2 Poker Hand One Pair Winner Algorithm

I wrote this algorithm to determine the winning hand of two players going head to head with a hand of One Pair. Note that the hand is sorted once it is given out, and the algorithm has to compare the ...
20k views

### Calculating nth power of x

I have written this code which precisely calculates the nth power of x by a recursive method. I compared my program with Java's pow(double, double) function, and ...
• 253
1 vote
4k views

### Find two equal substrings in a string

I have various strings that contain codes, e.g. like this: "file = new JarFile(new File()" + "\nSystem.out.println(file" but also ...
• 1,163
32k views

### Permutations of a list of numbers

I have written a program to find all the possible permutations of a given list of items. This precisely means that my program prints all possible P(n,r) values for r=0 to n. ...
• 253
407 views

### {Sum+=i++} to Reach N

I am developing code for a problem posted on StackOverflow. I have written my solution in Java but it is taking too much time to execute for numbers such as ...
16k views

### Mapping the differences between JavaScript objects

The reason I want to do this is that the JavaScript objects are created from XML elements and if the element has changed I want to be able to detect that change in the JavaScript objects. So I wrote a ...
• 181
1k views

### Shuttle Puzzle solver

How can I make this piece of code better? The problem to be solved is in the topmost comment. ...
• 1,871
14k views

### Find most occurring word in a txt file

Assume that we have a .txt file that has one word per line. Find out the word that occurs the most. Here's what I was able to write (I used array of strings ...
• 225
40k views

### Most efficient way in C++ to strip strings

If I want to strip a string completely of its whitespaces, punctuation and numbers (i.e. anything that is not A-Z, a-z), what is the most efficient way of doing it in C++? I tried this: ...
• 359
14k views

### Counting the number of bits of a positive integer

How can I improve this code for counting the number of bits of a positive integer n in Python? ...
1 vote
8k views

### Returning all possible English dictionary words that can be formed out of a string

I am using an external library named enchant here. My problem is that I guess my program is not returning all possible English words and is slow as well for large ...
• 3,075
1 vote
868 views

### Snippet of custom array.indexOf that supports nested arrays

I recently wrote a snippet to make a custom array.indexOf but with nested array support in addition to allow using a nested array as needle. For example, it correctly returns the index ...
• 264
8k views

### Min Heap implementation with Dijkstra's algorithm

I am implementing Dijkstra's Algorithm using Min Heap to speed up the code. For a small number of nodes, the code is really running very fast. But for a large number of nodes, my code is throwing ...
• 265
12k views

### Implementation of Kosaraju's algorithm for strongly connected components

To learn and practice coding in C++, I wrote an implementation of Kosaraju's two-pass algorithm for computing the strongly connected components in a directed graph, using depth-first search. This was ...
• 842
233 views

### Product of all ints in array besides that an index i

I'm looking for a better implementation of the algorithm. ...
• 1,871
12k views

### Yet another prime number generator

The question asked to find all the prime numbers within a particular range. The way I approached this was to loop through each of the numbers within the range and for each number check whether it's a ...
• 1,183
2k views

### Possible combinations of bills that sum to the given amount

Question: You are given an array that represents bills in certain currency (for example 1, 2, 5, 10) and an amount, for example ...