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)

6
votes
4answers
319 views

Find permutations by repeatedly cycling 3 elements

Is there an algorithm to find all possible permutations of a series of unique elements, that follows this rule? From a given permutation the next permutation must be found by cycling exactly 3 ...
0
votes
3answers
47 views

Find the numbers larger than them in the collection

Let's say we have a collection of numbers like { 16,17,4,3,5,2 }. Now the objective is to find those numbers which are greater than the rest in the collection while compare with their right elements. ...
3
votes
3answers
71 views

Is it possible to query number of distinct integers in a range in O(lg N)?

I have read through some tutorials about two common data structure which can achieve range update and query in O(lg N): Segment tree and Binary Indexed Tree (BIT / Fenwick Tree). Most of the examples ...
0
votes
2answers
38 views

Creating an interval with a loop and determining minimum and maximum values without arrays in c++ [on hold]

I have been given this assignment for homework: Write a program that prompts the user to enter two positive integer numbers: top and bottom of an interval and create two functions to display the ...
-1
votes
0answers
31 views

Optimal Selection in Ruby

Given an array of values, arr = [8,10,4,5,3,7,6,0,1,9,13,2] X is an array of values can be chosen at a time where X.length != 0 and X.length < arr.length The chosen values are then fed into a ...
0
votes
1answer
31 views

Hash function f(a, b) = f(b,a) [on hold]

I need simple hash function that takes two parameters, with the following parameters: a,b,c,d are different strings (approximately 30 characters) f(a,b) = f(b,a) f(a,c) ≠ f(a,d) f(c,b) ≠ f(d,b)
0
votes
0answers
13 views

Graph Traversal using DFS

I am learning the graph traversal from The Algorithm Design Manual by Steven S. Skiena. In his book, he has provided the code for traversing the graph using dfs. Below is the code. dfs(graph *g, int ...
0
votes
0answers
21 views

Algorithm to find best combination of offers depending on constraints

I have items with ID and respective quantity in an array of map with id as first element and its quantity as second element . [100 : 1, 300 : 2, 400:2, 600 : 2, 700 : 1]. I have input data : 1 ...
-4
votes
1answer
29 views

What kind of algorithm is well suited for optimizing seat allocation in opera house?

So i have an opera houses with 10 rows and 10 columns of seats (Total :100). Each seat is allocated a preference value Aij. The preference value is halved if the group do not get seats in same row. Eg:...
-2
votes
1answer
39 views

Find all possible consecutive subset in a unsorted array using JAVA

Say I have exactly 5 cards, and it is obviously unsorted, how could I find the length of all possible straight(s)? (Straight has at least length of 3) Ace would be only considered as 1. For example ...
4
votes
4answers
63 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 ...
5
votes
5answers
197 views

How to extract all possible matching arrays of objects from one array of objects?

I have an array of objects, e.g. var arr = [ {"a": "x"}, {"b": "0"}, {"c": "k"}, {"a": "nm"}, {"b": "765"}, {"ab": "i"}, {"bc": "x"}, {"ab": "4"}, {"abc": "L"} ];...
1
vote
0answers
28 views

How can we switch between two sorting algorithms based on their run time performance?

I am trying to write a program which will take an input array as input and will sort it. Sorting will be like this : Program will start sorting the first 20% of the array using any of the below ...
0
votes
1answer
22 views

Recurrence Relation to a specific algorithm

Good evening. The past week for my course I have been learning how to calculate run-times as well as determining recurrence relations for given algorithms. I am comfortable with iterative algs, but ...
27
votes
6answers
52k views

Finding the median of an unsorted array

To find the median of an unsorted array, we can make a min-heap in O(nlogn) time for n elements, and then we can extract one by one n/2 elements to get the median. But this approach would take O(nlogn)...
0
votes
2answers
37 views

Generate next combination of size k from integer vector

Lets say I have a vector of integers v = {0, 1,..., N-1} of size N. for example: k = 2, N = 10 {0,1}, {0,2}, ..., {0,9}, {1,2}, ..., {8,9} Given a size k, I want to generate all k-sized ...
0
votes
0answers
9 views

Algorithms to use when all predictors are categorical and response is numeric

I have a dataset that has categorical values as predictors; and the response variable is Cost. In other words, my data has the Clinical Risk_levels of a person (say, Hypertension_Risk_Level - Stage-1/...
2
votes
5answers
473 views

how to check whether 2 strings are rotations to each other ?

Given 2 strings, design a function that can check whether they are rotations to each other without making any changes on them ? The return value is boolean. e.g ABCD, ABDC, they are not rotations. ...
4
votes
3answers
100 views

What would you call the time complexity of an algorithm of this sort?

I am using C# syntax but this question is not specific to C# only. Example 1 public static long Do(long n) { var sqrt = (long)Math.Sqrt(n); for(long i = 0; i < sqrt; i++) // do ...
0
votes
2answers
33 views

Selection Sort Algorithm Python

Working on implementing this algorithm using Python. I thought my logic was okay but apparently not as Python is complaining. The while loop is causing issues. If I remove that it works as expected ...
2
votes
0answers
21 views

SWAB segmentation algorithm on time series data

I'm trying to understand how to do segmentation on a set of time series data (daily stock prices, temperatures etc.) and came across a book that explains how to do the SWAB (sliding-window and bottom-...
-3
votes
0answers
35 views

Pythagorean Triples Given Upper Bound

I am to write a function that will take a upper limit, (higher than 10) and return all Pythagorean Triples between [1,n] >>> findtriples() Enter an upper bound > 10: 11 ( 3 4 ...
0
votes
2answers
25 views

Need explanation of time complexity solution of algorithm

My intro to software engineering class has just hit time complexity and learning how to analyze certain algorithms. I'm having difficulty seeing how they got to their solution and was hoping someone ...
2
votes
2answers
3k views

Simple Big O with lg(n) proof

I'm attempting to guess and prove the Big O for: f(n) = n^3 - 7n^2 + nlg(n) + 10 I guess that big O is n^3 as it is the term with the largest order of growth However, I'm having trouble proving it. ...
98
votes
43answers
141k views

How do I check if a number is a palindrome?

How do I check if a number is a palindrome? Any language. Any algorithm. (except the algorithm of making the number a string and then reversing the string).
0
votes
0answers
17 views

Minimum Variance Quantization and MatLab

I've implemented Minimum Variance Quantization algorithm described here on page 126 (or 154 if you use pdf viewer search). This method is used in matlab function rgb2ind described here if you specify ...
0
votes
0answers
8 views

Insertion in left-leaning red-black tree

It is supposed to be a simple question, but somehow I stuck... Find an example of an insertion in a left-leaning red-black tree that requires at least 4 rotations and makes the new element the root ...
0
votes
1answer
49 views

Finding the closest pair of points - How to implement split pairs inside recursive side pairs function calls

I'm trying to implement finding the closest pair of points algorithm using divide and conquer approach. First some explanation, split pair (p1, p2) are te closest pair such that p1 is in the left ...
0
votes
1answer
55 views

C# Algorithm for a Packed Bubble chart? [closed]

I try to find or figure out the algorithm for the Packed Bubble chart. As those charts from Tableau (image below) or D3.js. Or a "bubble cloud", similar to the Wordle's Word Cloud. Unlike other ...
1
vote
1answer
61 views

Is my explanation about big o correct in this case?

I'm trying to explain to my friend why 7n - 2 = O(N). I want to explain based on the definition of big O. Based on definition of big O, f(n) = O(g(n)) if we can find real value c and integer value n0 >...
-1
votes
1answer
49 views

Why don't we count linear search cost as a prerequisite bottleneck for the insertion operation of a linked list, compared to ArrayList?

I have had this question for a while but I have been unsatisfied with the answers because the distinctions appear to be arbitrary and more like conventional wisdom that is sort of blindly accepted ...
-1
votes
1answer
21 views

Implementing Betting for a Poker Game

I am currently writing a Texas Hold'Em style Poker game that I would like both humans and computers alike to be able to play. However, I am hung up on how to implement the betting rounds. Currently I ...
-2
votes
0answers
31 views

I need an algorithm that generate string that matches a regular expresion

I have search and search on the internet but I can't find a solution to my problem. As the title says , I need an algorithm that generate N string that matches a regular expresion , where N is an int....
-1
votes
0answers
29 views

Is VBA RegExp suitable for checking patterns in large amount of data?

I'm writing a VBA code to find specific patterns in excel sheet string cells; count of cells may be 500K or more. I've read that RegExp has low efficiency when it comes to large amount of data. What ...
2
votes
1answer
55 views

ruby - possible inefficient algorithm

This was my solution to a function that should return the first pair of two prime numbers spaced with a gap of g between the limits m, n if these numbers exist otherwise nil. This is a kata from ...
0
votes
1answer
30 views

depth first search in an undirected, weighted graph using adjacency matrix?

I don't want the answer, but I'm having trouble keeping track of the nodes. Meaning, say I have nodes 0, 1, 2, 3,4, 5, 6, 7, where 0 is start, and 7 is goal, I made an adjacency matrix like so: [ ...
1
vote
2answers
50 views

String Finding Alg w/ Lowest Freq Char

I have 3 text files. One with a set of text to be searched through (ex. ABCDEAABBCCDDAABC) One contains a number of patterns to search for in the text (ex. AB, EA, CC) And the last containing the ...
-1
votes
3answers
94 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
3answers
52 views

Rank of a Permutation

so there was a question I wasn't able to solve mainly because of computing power or lack thereof. Was wondering how to code this so that I can actually run it on my computer. The gist of the questions ...
0
votes
2answers
59 views

Using Genetic Algorithm to converge to global minimum of a 2 variable function

I am using Visual Studio C++ as the platform to try to converge to the global minimum. Let's assume the function is a blackbox function where in if I input (x,y), I get z. And also that the ...
-1
votes
1answer
22 views

How do I hide a word in a random number matrix?

I am a Python beginner and am stuck here. I have written the following program that generates a matrix of random numbers. def randsq(size): for i in range(size): for j in range(size): ...
1
vote
2answers
31 views

How to solve T(n) = T(n-2) + T(2) + n with recursion tree

I want to find the complexity of an algorithm that involves the recurrence: T(n) = T(n-2) + T(2) + n T(n) is the time it takes to solve a problem of size n. I want to use recursion tree but my ...
0
votes
4answers
49 views

Test If String Contains All Characters That Make Up Another String

I am trying to use Javascript to see if a certain string contains all characters that make up another string. For instance, the word "hello" contains all characters that make up the word "hell." Also,...
0
votes
1answer
42 views

Find a Bigger Number than a number given in array

There is an UNSORTED sequence of numbers: a1,a2,...,an And, We Have a number, i So We have ai We want to find number, j Which j<i and aj>ai Like this: 3,4,9,1,8,10,2,2,6,1 if i=8 then j=6 if ...
-1
votes
2answers
160 views
+200

How to split text into chunks minimizing the solution?

OVERVIEW I got a set of possible valid chunks I can use to split a text (if possible). How can i split a given text using these chunks such as the result will be optimized (minimized) in terms of ...
13
votes
7answers
9k views

Find the number in an array that is closest to a given number

I have an Array of integers in javascript, [5,10,15,20,25,30,35] when given a number x, how can I find the element in the array that is closest to that number? If the number is over a value, but ...
2
votes
1answer
58 views

Best way to handle file operation for Logging last 1000 Events

I came across a situation where I have to log last 1000 events present in the queue. What will be the best solution to handle this by reducing costly file operation? At present we are completely ...
0
votes
5answers
35 views

Time complexity and recurrence of an algorithm

It is a problem from an algorithm textbook, i think the time complexity is log(n!), but my classmate says it is nlog(n). Algorithm XYZ(r,q,n) if n=1 then return r else if n=odd then return XYZ(...
0
votes
3answers
72 views

Why do we say linked list inserts are constant time?

I understand that linked list insertions are constant time due to simple rearrangement of pointers but doesn't this require knowing the element from which you're doing the insert? And getting access ...
125
votes
28answers
75k views

Given an array of numbers, return array of products of all other numbers (no division)

I was asked this question in a job interview, and I'd like to know how others would solve it. I'm most comfortable with Java, but solutions in other languages are welcome. Given an array of ...