Tagged Questions

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

0
votes
0answers
13 views

3 Pitcher with water problem in Ruby

I don't know what is problem named, so i can't google about the solution about it, here's the image there's 3 pitcher with capacity of 10, 7 and 3 quartz all we need to do is to move the water ...
0
votes
1answer
22 views

Recursive bubble sort in Java

I want to know if someone can suggest how I can remove the initialValue parameter of my method which sorts an array: public static void bubble(int[] array, int initialValue) { int aux; for ...
3
votes
2answers
111 views

Given an array find any three numbers which sum to zero

Looking for optimizations and cleaner, more pythonic ways of implementing the following code. #Given an array find any three numbers which sum to zero. import unittest def sum_to_zero(a): ...
1
vote
0answers
14 views

Density-based clustering of image keypoints

I have implemented the DBSCAN algorithm for clustering image keypoints, I'm using C++ and OpenCV, I have been following the pseudocode on the wiki page pretty strictly and its working but I get the ...
-2
votes
0answers
32 views

sorting the list [closed]

i want to sort the fallowing in the ascending order the code is as fallows .pdf');"> .pdf');"> <%= form_list[14] %> will return the string value and the link with it say 1 aaaaaa 2 dddddd ...
4
votes
2answers
111 views

Better way to write the following code?

Is there a better, more elegant solution to the following method? Boolean[] spades = new Boolean[10]; // 40 cards deck foreach (Card card in m_Cards.Where(card => (card.Suit == Suit.Spades))) ...
1
vote
1answer
54 views

Leap year algorithm in Ruby

I wrote two functions of determining leap year. (Kabisat means leap year in indonesia.) def kabisat? y return false if y%4!=0 return true if y%100!=0 y%400==0 end def ...
1
vote
0answers
63 views

How to optimize this algorithm? (solving systems of linear equations)

I am working on a programming challenge described here: http://www.enigmagroup.org/missions/programming/9/ Basically, you have a 6x6 table where the first 5 columns and rows are comprised of ...
3
votes
2answers
78 views

remove duplicates in a sorted array if duplicates are allowed at most twice

Given a sorted array, remove the duplicates in place such that each element appear at most twice and return the new length. Do not allocate extra space for another array, you must do this in place ...
0
votes
0answers
14 views

remove duplicates in a sorted array [duplicate]

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with ...
2
votes
1answer
95 views

Is this a good encryption algorithm?

I've never had a serious attempt at writing an encryption algorithm before, and haven't read much on the subject. I have tried to write an encryption algorithm (of sorts), I would just like to see if ...
3
votes
4answers
113 views

Finding unconnected sets

I have a list of boxes, and wish to group them into unconnected sets of overlapping boxes. (Note that two boxes A and B may not overlap each other, but if they are both overlapped by a box C, they ...
0
votes
1answer
39 views

Creating an Array of Linked Lists from a BST

Question Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists) Here is my ...
3
votes
1answer
63 views

Challenge: solution for this deep-iteration JavaScript function dilemma?

Trying to implement a deep-iteration function with some constraints, I've noticed the faster solution is iterating it twice! Any attempt to avoid this resulted in slower code. So, the challenge: can ...
4
votes
2answers
64 views

Using standard library to simplify pairwise iteration of container values

I came up with this code whilst answering this question. Is there a simpler way of doing this using standard library? I want to iterate over every object and do something with every other object. ...
4
votes
4answers
181 views

Find the 2 distinct highest values in a array

Good Night I want to know if there is a better way to improve my code to find two highest in a array, and these numbers need to be distinct. I don´t want to sort the values. Only run in the array ...
1
vote
2answers
71 views

Calculate difference of two ranges given

Write a method to compute the difference between two ranges. A range is defined by an integer low and an integer high. A - B (A “minus” B) is “everything in A that is not in B”. This is an interview ...
4
votes
2answers
75 views

Move object by one up or down algorithm in a custom order

Basically, I did an object (using hibernate) with a field called sorting_order. This field needs to be unique and I wish to swap two object by one. So one element has to be after or before the current ...
-1
votes
1answer
50 views

Project RGB with Switches [closed]

I am working on project in which I need to display different colors on RGB led. I am using pwm to drive different colors on LED. My Pic is PIC24FJ64GA004 with which I am working on now. Basic concept ...
4
votes
2answers
66 views

Merge sort in scala

I've implemented merge sort in scala: object Lunch { def doMergeSortExample() = { val values:Array[Int] = List(5,11,8,4,2).toArray sort(values) printArray(values) } def ...
2
votes
2answers
64 views

Need a suggestion for invert index structure in C++

I am trying to create an invert index structure in C++. An invert index, in this case, refers to image retrival system, where each invert index refers to a numbers of "Patch ID". (simply called "ID" ...
1
vote
3answers
88 views

What is wrong with my Genetic Algorithm?

I am trying to understand how genetic algorithms work. As with everything, I learn by attempting to write something on my own;however, my knowledge is very limited and I am not sure if I am doing this ...
4
votes
1answer
60 views

Improving a priority queue sketch in Racket/Scheme

I just put together the skeletons for a priority queue using a binary heap in racket/scheme. Using racket/scheme is all about the educational experience and I was wondering if anyone wants to ...
4
votes
1answer
81 views

Need help optimizing orthogonal range search for a static kd tree

I'm trying to optimise my implemention of a static kd tree to perform orthogonal range searches in c++. My problem: The code performs slowly even for a small number of queries when the number of ...
5
votes
3answers
147 views

Maximum Sub-array Problem

I started taking a look at a programming challenge I had read about earlier today on 8thlight. Unfortunately, it seems to have been taken down and all I could remember about it was the problem posed: ...
1
vote
2answers
68 views

RGB Color Name Matching using Euclidean Distance (Improved)

The code below stems from work on a Euclidean Distance algorithm. The color table was simply a vehicle to test the algorithm. It is perhaps reinventing the wheel, however it is useful in itself. ...
1
vote
2answers
73 views

Consolidate list of ranges that overlap in Python

I wanted to implemented an algorithm in Python 2.4 (so the odd construction for the conditional assignment) where given a list of ranges, the function returns another list with all ranges that overlap ...
0
votes
1answer
36 views

Skiena's Programming Challenge [UVa ID 10137]- Getting WA

Question from Skiena's Programming Challenges. Getting WA (wrong answer) in Online Judge even though it's working for sample test cases. Can somebody find a case where it fails? I tried the tricky ...
2
votes
1answer
81 views

Kadane's Algorithm, finding maximum contiguous array

Description of Kadane's algorithm. Please comment on the efficiency and approach. class Kadane { int sum = 0; int max = 0; public static void main(String[] args) { int[] full = { 1, 2, 3, 4, 5, ...
2
votes
1answer
44 views

VB.Net interfacing with F# Euclidean Distance Algorithm

I testing F# code which calculates "nearness" of two N-dimensional points using a least square euclidean distance algorithm. The class library is written in F# and the calling will be from VB.NET. ...
3
votes
1answer
44 views

Code to find the proper index comparing 3 values for max one

I have an algorithm and the idea is to move over an array choosing as index the index of the neighboring cell that has the max value. I.e. if array[i + 1][j + 1] has the largest value among the 3 ...
12
votes
5answers
359 views

Naive C++ Matrix Multiplication 100 times slower than BLAS?

I am taking a look at large matrix multiplication and ran the following experiment to form a baseline test: Randomly generate two 4096x4096 matrixes X, Y from std normal (0 mean, 1 stddev). Z = X*Y ...
1
vote
0answers
66 views

Sudoku Valid Arangements Permutations Enumerator

I wondered if anyone has any ideas to make this cleaner or more efficient. Mostly this was an exercise to generate the data once (So speed really isn't too important, but I'd love to see what ...
1
vote
3answers
83 views

Check Whether Binary Tree is a Binary Search Tree [closed]

Could someone please verify the validity of my algorithm? I am assuming that a non-null value is initially passed into the function: boolean isBinarySearchTree(Node *root){ if(!root) ...
1
vote
3answers
65 views

Evaluating result for mastermind comparison

I'm implementing a Java version of the game Mastermind. My version uses numbers to represent colors, for example 2315. This function compares a string of numbers to the secret code the object ...
2
votes
1answer
73 views

Implementing logic in a different way

I need to write a function which will do the following functionalities I would get a string and a array as input parameters The string would be like one of the following for example catalog ...
1
vote
2answers
106 views

How can this code become cleaner and less error prone? Especially the binary search part

How could this code become cleaner? I think that the way I handle the interfaces and binary search could be improved. I am trying to understand how to structure such a code 9and usage of APIs) in a ...
0
votes
1answer
118 views

Review algorithm of this code

In this piece of code i have to display some set of data in serpentine order.In an experiment there are Replication,range,plot Replications contain range,plot.A range contains plot. if there is 2 ...
1
vote
1answer
236 views

Dynamic programming solution to knapsack problem

I wrote a solution to the Knapsack problem in Python, using a bottom-up dynamic programming algorithm. It correctly computes the optimal value, given a list of items with values and weights, and a ...
1
vote
1answer
51 views

Any mistakes in the following Java program in solving this task from ACM Timus [closed]

The problem description is from ACM Timus Online Judge. (1930. Ivan's Car) My first idea is using BFS. But it keeps getting Wrong Answer at Test 6. Any hints to solve this? The following is my Java ...
1
vote
2answers
392 views

InterviewStreet Triplet optimization

I came up with a solution for the InterviewStreet problem "Triplet" link to the problem In short, there is an integer array d which does not contain more than two elements of the same value. How ...
1
vote
1answer
112 views

Finding the longest common subsequence algorithm using hash table Slow

I've designed an algorithm to find the longest common subsequence. these are steps: Starts with i = 0 Picks the first letter from the first string start from ith letter. Go to the second string ...
1
vote
2answers
59 views

Partition a linked list arround an element

This is my code to partition a list into two parts according to a value. I.e. nodes smaller than value x should precede nodes larger than the value x. It seems correct. Any corner cases I am ...
0
votes
0answers
49 views

custom cmdline implementation

The below program takes the inputted string from commandline and parses it and calls the corresponding function. commands are either single word or multiple words. say for e.g. -#version ...
1
vote
1answer
76 views

AStar C++ implementation is too slow

My AStar Algorithm in CPP is pretty slow. I'm not sure if it because of bad implementation or just because I have way too many Nodes (I have a Field of 256x256 Nodes). It takes the Algorithm about 5 ...
1
vote
3answers
175 views

Shortest path in a grid between two points. Code optimization

I have this problem where I have to find the shortest path in an NxM grid from point A (always top left) to point B (always bottom right) by only moving right or down. Sounds easy, eh? Well here's the ...
2
votes
0answers
236 views

Breadth- and Depth-first search code

Here is my breadth-first search (bfs) and depth-first search (dfs) code for graph traversal. Please give me some constructive reviews on it. Thanks for any help. #include<stdio.h> ...
-1
votes
1answer
85 views

Power set recursive algorithm [closed]

What is wrong with this code? It should return the power set of a given set. static public <T> ArrayList< ArrayList<T> > powerSet(ArrayList<T> inputSet){ ArrayList< ...
0
votes
1answer
178 views

Boyer Moore Implementation

I am trying to implement Boyer Moore Algorithm for text searching and have come up with this implementation: public class BoyerMooreStringSearching { readonly Dictionary<char, ...
1
vote
1answer
104 views

Code review for recursion to map phone number to strings

I am trying to solve the following via recursion: In a phone we have each digit mapped to a number. Example: 1 2(ABC) 3(DEF) 4(GHI) 5(JKL) 6(MNO) 7(PRS) 8(TUV) 9(XYZ) * 0 ...

1 2 3 4 5 6