An algorithm is a sequence of well-defined steps that defines an abstract solution to a problem. Use this tag when your issue is related to algorithm design.

learn more… | top users | synonyms (2)

0
votes
1answer
31 views

Parallelization of Combination

I have got a piece of code that prints the combination of M number From N (nCm); As it is a recursion, it works very slow when N is large. #include <stdio.h> #include <stdlib.h> #define ...
0
votes
1answer
2k views

cloudsim Where should I put my algorithm in cloudsim? [on hold]

I just started to study cloudsim. I import the cloudsim to eclipse and I would like to run the my algorithm. Where should I put my algorithm to run the example test case ? thanks
1
vote
1answer
25 views

Mathematical Formula For Generating Incresing Sequences

I have two lookup tables that I would like to eliminate with simple math if possible. The first is a map from indices in an array to the sequence {0} => 1, {1, 2} => 2, {3, 4, 5} => 3, s.t. there is ...
2
votes
3answers
60 views

Algorithm for solving a linear equation in 3 variables?

I am working on a programming (using python) problem where I have to solve the following type of linear equation in 3 variables: x, y, z are all integers. Equation example: 2x + 5y + 8z = 14 ...
1
vote
1answer
36 views

Maximum cost from a set of labels 1-n each with a weight vi

I'm trying to solve a problem wherein I have a set of items with labels l1,l2,l3,....ln each of which is associated with a weight vi. n is "EVEN". There are 2 people each of which will take turns to ...
-3
votes
1answer
23 views

Using series to approximate log(2)

double k = 0; int l = 1; double digits = pow(0.1, 5); do { k += (pow(-1, l - 1)/l); l++; } while((log(2)-k)>=digits); I'm trying to write a little program based on an example I seen ...
0
votes
3answers
82 views

Are those operations equivalent?

I'm refactoring some code and I've stumbled upon a passage like this. if (a > 1.0) a = 1.0; else if (a < -1.0) a = -1.0; According to the guidelines we've got, I should refactor it into ...
0
votes
1answer
40 views

Designing an algorithm to sort n strings in O(dn)

How can I design an algorithm to sort n strings in O(dn) where d is the #of characters in a longest string?
8
votes
8answers
6k views

Max double slice sum

Recently, I tried to solve the Max Double Slice Sum problem in codility which is a variant of max slice problem. My Solution was to look for a slice that has maximum value when its minimum value is ...
1
vote
1answer
25 views

3D Line-of-Sight algorithm

I'm currently implementing a line-of-sight algorithm that will tell me the points I can and cannot see along a line. So, if I am standing atop some hilly terrain, I can know where I can and cannot see....
2
votes
4answers
41 views

Permutations of a boolean array

If I have an array of boolean values of n length, how can I iterate over all possible permutations of the array? For example, for an array of size 3, there are eight possible permutations: [0,0,0] [...
-2
votes
1answer
17 views

Find the kth smallest element in two sorted arrays with complexity of (logn)^2

S and T are two sorted arrays with n elements (integer) each ,describe an algorithm to find the kth smallest number in the union of two arrays(assuming no duplicates) with time complexity of (logn)^2....
1
vote
1answer
16 views

Are the suffix links in a suffix tree same as the failure edges in an aho-corasick automaton?

If so, can someone explain the purposes of suffix links in a suffix tree for exact string matching ?
0
votes
0answers
20 views

what is the significance of Slack time in scheduling the jobs with a particular deadline?

Job Scheduling : We have a single resource and n jobs request to use this resource Request i requires time t(i) to complete and has a deadline d(i) All requests will be scheduled Request j starts at ...
0
votes
1answer
43 views

Size of a vector of pairs

I am filling up an adjacency list of vector with pairs given by : vector<pair<int, int>> adj[1000]; I am doing a depth first search on the list but experiencing some weird behaviour. ...
-7
votes
3answers
69 views

Shift zeros to beginning of array [on hold]

Given an array of integers, take all of the 0's inside of the array and put them on the left in-place. How to do this in O(N) time? (non zero elements do not have to be in same order) Eg: `[0, 1, 4, ...
0
votes
1answer
36 views

Code for calculating how many leaves are left unharmed

Recently I was practicing this problem , where I need to calculate the total number of leaves which are left unharmed by the caterpillars. And fortunately I did the code pretty fast only to find that ...
0
votes
0answers
15 views

Is N<sup>2</sup>-1 solution using A* search that much memory expensive if N is about 10?

I read somewhere on internet that A* search to solve N2-1 consumes too much memory if N is more than 4 or 5. Is it really? Why should we store every state as a matrix rather than the transformation ...
-2
votes
0answers
16 views

This book 'Introduction to Algorithms' is enough for data structures? [on hold]

I'm a graduate considering to buy this book 'Introduction to Algorithms' for data structures and algorithms. I wonder if this book is enough for an interviewee to study data structures so I don't need ...
1
vote
0answers
27 views

optimization in brute force vertex cover algorithm

I'm writing a brute-force algorithm to solve vertex cover like this: BruteForceVertexCover( Graph G = (V,E) ){ for size= 1 ... |V| vector<int> v = {0...size-1} do{ ...
10
votes
17answers
13k views

Fastest algorithm for circle shift N sized array for M position

What is the fastest algorithm for circle shifting array for m positions? For example [3 4 5 2 3 1 4] shift m = 2 positions should be [1 4 3 4 5 2 3] Thanks a lot
3
votes
1answer
30 views

Why is this (partial) MergeSort Implementation blowing the stack?

I'm going through the steps of making my own MergeSort implementation. It's recursive and has a base case. The only thing that I haven't done is the imperfect halving of arrays where length % 2 != 0. ...
0
votes
0answers
23 views

JAVA: Red-Black Tree Remove Method

I'm preparing for an internship interview but I have not taken the algorithms course yet so I am learning about Red-Black Trees and other common algorithms. My question: how do you implement the ...
6
votes
4answers
98 views

Find the subset of a set of integers that has the maximum product

Let A be a non-empty set of integers. Write a function find that outputs a non-empty subset of A that has the maximum product. For example, find([-1, -2, -3, 0, 2]) = 12 = (-2)*(-3)*2 Here's what I ...
1
vote
2answers
56 views

From a sentence group all words that start with same alphabet and and sort it according to first character of the word

suppose I take a string input from the user , and if will then group all the words in that sentence with respect to first character of the word and later we have to display the o/p as dictionary , No ...
1
vote
0answers
27 views

Single Machine Sequencing - Optimize an oven schedule with expiring products

Let's say you own a burrito restaurant and you need to cook beef, chicken, and shrimp on the grill for the lunch rush. You know how much of each item you will need to have prepared and ready to serve ...
0
votes
1answer
32 views

Math function with three variables (correlation)

I want to analyse some data in order to program a pricing algorithm. Following dates are available: I need a function/correlationfactor of the three variables/dimension which show the change of ...
-1
votes
0answers
17 views

Expectiminimax of Dungeons and Dragons [on hold]

I'm taking an Artificial Intelligence class, and using the knowledge I'm gaining, I'm trying to design a Dungeons and Dragons Combat AI for fun. When trying to decide who to attack, it's clear to me ...
-1
votes
3answers
97 views

What is the Big-O of code that uses random number generators?

I want to fill the array 'a' with random values from 1 to N (no repeated values). Lets suppose Big-O of randInt(i, j) is O(1) and this function generates random values from i to j. Examples of the ...
-3
votes
0answers
77 views

Converting python code to delphi [on hold]

I want an algorithm to convert Delphi code but could not figure out where the calculation. I solved the crc16 calculation, info, but I figured it could not figure out how I'm going to address the cost ...
1
vote
3answers
58 views

Find the nth comparison

I have to compare M items where a single item should not be compared to itself. In this case, I would like to design an algorithm to find the nth comparison. If, for example, I am comparing 2 items ...
-6
votes
0answers
43 views

where is my bug(c++)? [on hold]

i'm trying to solve this problem : giving a number N , permutations of N are all the arrangements of the numbers from 1-N for example for N = 7 , one possible permutation is 3572146 . a sub ...
0
votes
1answer
43 views

Recursively modify a dictionary

I've created a class that should transform a nested list into a dictionary. The following is my input: ['function:and', ['variable:X', 'function:>=', 'value:13'], ['variable:Y', 'function:=...
12
votes
7answers
16k views

Explanation of Merge Sort for Dummies

I found this code online: def merge(left, right): result = [] i ,j = 0, 0 while i < len(left) and j < len(right): if left[i] <= right[j]: result.append(left[i]...
2
votes
2answers
40 views

Trouble finding shortest path across a 2D mesh surface

I asked this question three days ago and I got burned by contributors because I didn't include enough information. I am sorry about that. I have a 2D matrix and each array position relates to the ...
1
vote
0answers
21 views

Longest Common Subsequence for k strings

We all know that the LCS(Longest Common Subsequence) algorithm for 2 strings can be computed using Dynamic Programming for 2 strings with lengths m and n in O(mn) time and space. Now if we consider k ...
-1
votes
1answer
27 views

Is this fact true about triangles?

I'm wondering if this fact is true since it relates to a programming problem I'm solving. Given a positive integer P, there exists at most one set of positive integers {A,B,C} such that A+B+C=P and ...
-1
votes
0answers
17 views

mergeSort Algorithm in Python [duplicate]

So recently in class we've been exposed to different sorting algorithms, to keep it simple, let's assume we're sorting numbers numerically. A sorting algorithm classed as mergeSort was discussed, the ...
-4
votes
1answer
41 views

Trouble starting an algorithm [on hold]

I'm having trouble on thinking of away to attack this problem. X is defined below. For n=1,x=0.5,n=2,x=0.833.As you add more terms, X increases. Calculate n for which X becomes larger than 4. First ...
581
votes
44answers
360k views

How to count the number of set bits in a 32-bit integer?

8 bits representing the number 7 look like this: 00000111 Three bits are set. What are algorithms to determine the number of set bits in a 32-bit integer?
0
votes
2answers
25 views

Heap Sort Complexity

Below is the Pseudo Code of HEAPSORT on an array HEAPSORT(A) BUILD-MAX-HEAP(A) for i = A.length downto 2 exchange A[1] with A[i] A.heapsize = A.heapsize - 1 MAX-HEAPIFY(A,1) It ...
-1
votes
0answers
31 views

c++ Map: Possible to add non-ASCII char to map<char,int>?

So I am using a map to hold character frequencies for this string matching algorithm I'm working on. So, the key is the character and the element is the frequency it occurs. Mind you, Im reading in ...
3
votes
1answer
51 views

Finding maximum area k-gon given a set of points

I was trying to solve a practice problem in the topcoder arena: http://topcoder.bgcoder.com/print.php?id=417 According to my understanding the aim of the problem is to find a k-gon of maximum area, ...
46
votes
7answers
11k views

Find the number of occurrences of a subsequence in a string

For example, let the string be the first 10 digits of pi, 3141592653, and the subsequence be 123. Note that the sequence occurs twice: 3141592653 1 2 3 1 2 3 This was an interview ...
2
votes
1answer
26 views

imrpove execution time in a simple java program

I am playing around codefight, but I am really stuck to the following efficient issue. Problem: Given integers n, l and r, find the number of ways to represent n as a sum of two integers A and B ...
0
votes
3answers
54 views

Traversing Hash Map linear using generics (Java)

I have a hash map which uses linear probing to deal with collisions. I would like to traverse it. Conceptually, this is quite easy, however, the use of generics is spinning me off. The entries in ...
8
votes
2answers
5k views

Algorithm to auto-arrange entity relationship diagram

I am currently writing a control (in C#) for displaying a set of tables and the relationships that exist between them. I got the basic control done, but would like to implement something similar to ...
-4
votes
1answer
22 views

How to control the number of layer in nested for-loop in python? [duplicate]

Given a list like alist = [1, 3, 2, 20, 10, 13], I wanna write a function find_group_f, which can generate all possible combination of elements in alist and meanwhile I can dynamically control the ...
0
votes
1answer
9 views

simulation platform in linux to implement algorithms of distributed OS

I'm working on distributed os algorithms. I have written one algorithm on mutual exclusion. I have submitted the paper, the reviewer said that I have to run my algorithm on a simulation platform but ...
-3
votes
1answer
20 views

Random No.s algorithm

Level: Newbie q1)Do true random number exists? q2)If I were to reverse engineer a series of no.s would be random any longer? q3)Is AP, GP (Arithmetic Prog, Geometric prog) a random series? q4)Is there ...