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.

Filter by
Sorted by
Tagged with
3 votes
4 answers
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 ...
user avatar
  • 133
4 votes
1 answer
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. ...
user avatar
  • 283
7 votes
4 answers
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. ...
user avatar
3 votes
2 answers
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] ...
user avatar
  • 240
24 votes
5 answers
2k views

Sums of perfect powers

...
user avatar
  • 343
4 votes
2 answers
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 ...
user avatar
2 votes
2 answers
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 ...
user avatar
2 votes
3 answers
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.) ...
user avatar
  • 452
4 votes
2 answers
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 ...
user avatar
  • 183
2 votes
1 answer
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 ...
user avatar
  • 437
4 votes
1 answer
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 ...
user avatar
  • 561
7 votes
1 answer
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 ...
user avatar
6 votes
3 answers
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 ...
user avatar
  • 1,685
2 votes
3 answers
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 ...
user avatar
3 votes
2 answers
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 ...
user avatar
10 votes
5 answers
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: ...
user avatar
  • 205
3 votes
4 answers
12k views

Algorithm to find substring in a string

Can this be improved? ...
user avatar
  • 502
2 votes
2 answers
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 ...
user avatar
  • 167
1 vote
1 answer
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 ...
user avatar
  • 155
4 votes
2 answers
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. ...
user avatar
3 votes
2 answers
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 ...
user avatar
5 votes
1 answer
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 ...
user avatar
  • 53
2 votes
4 answers
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 ...
user avatar
  • 131
5 votes
1 answer
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 ...
user avatar
  • 311
10 votes
6 answers
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. <...
user avatar
  • 315
4 votes
2 answers
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: ...
user avatar
2 votes
1 answer
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. ...
user avatar
3 votes
1 answer
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! ...
user avatar
  • 259
6 votes
1 answer
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 ...
user avatar
2 votes
2 answers
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 ...
user avatar
3 votes
1 answer
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 ...
user avatar
1 vote
1 answer
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 ...
user avatar
7 votes
5 answers
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 ...
user avatar
  • 253
1 vote
2 answers
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 ...
user avatar
8 votes
2 answers
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. ...
user avatar
  • 253
2 votes
2 answers
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 ...
user avatar
8 votes
4 answers
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 ...
user avatar
  • 181
3 votes
2 answers
1k views

Shuttle Puzzle solver

How can I make this piece of code better? The problem to be solved is in the topmost comment. ...
user avatar
6 votes
5 answers
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 ...
user avatar
25 votes
9 answers
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: ...
user avatar
  • 359
5 votes
7 answers
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? ...
user avatar
1 vote
3 answers
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 ...
user avatar
1 vote
1 answer
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 ...
user avatar
5 votes
2 answers
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 ...
user avatar
8 votes
2 answers
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 ...
user avatar
  • 842
3 votes
2 answers
233 views

Product of all ints in array besides that an index i

I'm looking for a better implementation of the algorithm. ...
user avatar
8 votes
4 answers
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 ...
user avatar
  • 1,183
3 votes
3 answers
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 ...
user avatar
3 votes
1 answer
2k views

ByteLandian challenge from CodeChef

I am trying to go through the problems on CodeChef and came across this problem: In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on ...
user avatar
  • 1,183
2 votes
2 answers
2k views

Algorithm with byte shifts and BCD calculation

I'm trying to develop an algorithm that generates a 16-digit validation number to interact with a given system. This is how the algorithm should be implemented: I have implemented it, and have it ...
user avatar