# Questions tagged [algorithm]

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.

91,458 questions
18 views

### How to determine WHERE two lines intersect? Implementation?

I am wondering how to solve this problem. (Title) I've found this article, but am very confused by the mathematical notations and what not. Also, it does not contain an implementation to this problem....
48 views

### Find same position in two strings that are slightly different

I have two strings where the first is a master string and the second is a slave string. They both contain similar values except that the slave will have characters added or removed. I need to find ...
21 views

### Stackoverflow error on generic comparable merge sort algorithm

I keep getting a stackoverflow error on my mergesort call in my sort method. Its probably a quick fix, but I can't think of anything at the moment. Please look over my code. public class MergeSorter {...
16 views

### Algorithm to filter data structure AND/OR/NOT (similar to Prisma implementation)

I want to implement a data structure that allows for powerful filtering within my application. The closest implementation I've found is from Prisma https://www.prisma.io/docs/1.27/prisma-graphql-api/...
17 views

### Need an optimized way to handle combination of entities to improve performance

So, I am working on a feature in a web application. The problem is like this- I have four different entities. Let's say those are - Item1, Item2, Item3, Item4. There's two phase of the feature. Let's ...
30 views

### Check if all nodes in graph are in distance of <=k from each other

In a given graph, I need to check, if all nodes in graph, are in distance of <=k from each other. I Wrote a solution (simple C#) that runs with a loop on every node, and then checks that he has a ...
34 views

### Arranging the number 1 in a 2d matrix

Given the number of rows and columns of a 2d matrix Initially all elements of matrix are 0 Given the number of 1's that should be present in each row Given the number of 1's that should be present ...
11 views

### Customizing fuzzywuzzy string matching to edit distance <= 1

I am new in algorithms and my question may be silly, but how can I specify the edit distance in fuzzywuzzy library? I want edit distance <= 1 between two strings. from fuzzywuzzy import fuzz fuzz....
44 views

### How to calculate domino-chain for integer pairs?

The problem I'm facing is more like of algorithmic nature. Let's say that I have a list of pair objects containing integers. Is there a way to sort the list so that the second part of the pair is ...
71 views

### How to calculate running time of algorithm?

I am currently learning about Big O Notation running times. I try to calculate the time complexity of some code: int i = 1; int n = 3; //this variable is unknown int j; while (i<=n) { for (j ...
15 views

### Where is the Sentinel Node defined in this Implementation?

I'm trying to learn how to implement a skiplist using my textbook for extra practice, but the text seems to skip some portions of the implementation which are really stopping my progress, specifically,...
26 views

### Number of Islands LeetCode Q200

I am trying to do Q.200. Number of Islands question on Leetcode. The question reads as the following: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is ...
50 views

### Why isn't this code working to find the sum of multiples of 3 and 5 below the given number?

I've started with some problems on HackerRank, and am stuck with one of the Project Euler problems available there. The problem statement says: Find the sum of all the multiples of 3 or 5 below N I'...
37 views

### Ruby Enumerable#count performance issue in algorithm

I've solved this problem from LeetCode using Ruby but I'm having some performance issues that I don't understand. Here's my code: def word_subsets(a, b) occurrence_map = max_occurrences(b) a....
21 views

### How to simplify polyline while preserving corners?

I'm trying to write a simple "vectorizer" for my CG project. The goal is to vectorize characters / shapes in a binary-valued image. My approach is: 1.delete every black pixel with 4 surrounding ...
44 views

### Find the same numbers between [a,b] intervals

Suppose I have 3 array of consecutive numbers a = [1, 2, 3] b = [2, 3, 4] c = [3, 4] Then the same number that appears in all 3 arrays is 3. My algorithm is to use two for loops in each other to ...
34 views

### Is there a way to identify if a word has the same letters next to each other?

I'm trying to create a function in python does specific functions with a list of words in a text document. All the other functions work so it is not a problem with the word document. This function i'm ...
21 views

### What are the libraries available in python with help of which i can simulate waves? [on hold]

Libraries such as scipy,matplotlib,numpy are good as plotting tools but what i need is a video of the wave I am trying to plot is there any tool or library available in python for the same. Tried ...
26 views

### Maximum sum of given subset [on hold]

I want maximum of the given set such that no two elements has unique digit. Example: 121, 23, 3, 333, 4 output should be 458 explanation: sum of : 121 + 333 + 4 = 458 (we are not taking 23, 3 as 3 is ...
22 views

### Count unique users if user visit n times

I want to implement FreqCapping in an ad network. I want to serve a campaign to unique users only n time in day. If n=1, I could implement this with BloomFilter in redis, but usually n is greater than ...
39 views

### Optimal 2D bin packing

Given a set of rectangles of various sizes (items) and a set of rectangles of identical sizes (bins), place items into as few bins as possible. I'm aware of A Thousand Ways to Pack the Bin but I was ...
249 views

### Maximum sum of array with no repeating digits

I want to find the maximum sum which can be obtained from an array of numbers. But the constraint is that no two numbers have any digit in common. Example: Input: 121,23,3,333,4 Output: 458(...
66 views

### Find most common sum(s) in a list of integers

I'm trying to find the most common sum in a list of integers. For instance, given the list 2,4,6,8, the most common sums are 10, 12, and 14, as they can all be made in 2 ways: 2 + 8 = 4 + 6 = 10 2 ...
33 views

### group list into lists when sum of list under threshold

I have a list: [(14, 2), (14, 2), (16, 2), (14, 2), (15, 2), (15, 2), (21, 2), (15, 2), (18, 2), (15, 2), (19, 2), (25, 2), (22, 2), (17, 2), (31, 2), (26, 2), (21, 2), (25, 2), (29, 2), (33, 2), (25,...
18 views

### Using HLL_COUNT.MERGE outside of SQL

I can use the following query to general all the HLL sketches of the distinct counts: SELECT category, count(distinct city), HLL_COUNT.INIT(city) FROM `table` GROUP BY category And I get something ...
39 views

### Most Influencer City

My Problem goes like this: Suppose the cities of world, each city has a degree of economic collaboration with other cities. I need to detect communities such that the degrees among different cities ...
32 views

### Return Rowland Sequence without the 1's

I have this homework problem where I'm required to write a method that calculates the difference between each 2 numbers in the Rowland's prime generating sequence and return the results without the 1'...
43 views

### Function to count words in Python [migrated]

I am new to developing in Python and would like to see comments about my word count solution. Please indicate my mistakes or best solutions. def count_words(text): text = text.lower() symbols = ...
61 views

### Why does 2 raised to x give negative value as x increases in numpy?

Numpy is giving array containing list of 2 raised to natural numbers as negative values. How can 2 raised to positive numbers like 1000 be negative? I have an array 'x' that we use to plot x-axis ...
18 views

### How can I incorporate the camera's position into this algorithm to calculate a sprite's position on the screen?

I'm working on a 2D rendering system in 3D space, for practice. The sprites are 2D, but they're rendered in 3D space. The "camera" can move in 3D space and turn 360 degrees horizontally. I'm having ...
59 views

### Get day from day number of the year?

How to write a program that asks the user the day number in year in the range 2 to 365 and asks the first day of year - Sunday, Monday or Tuesday etc. Then program should display the day-number that ...
33 views

### lookup index value in a char table

I am trying to find the index number, by looking through an array, but I can't use a loop. Is it possible? I have attached the code to do this with a loop. #include <iostream> using namespace ...
153 views

### Is there an algorithm to figure out a 'nice' number formatting for a sequence of numbers of arbitrary order of magnitude?

I'm currently using an implementation of the Extended Wilkinson Algorithm to generate a sequence of axis tick values. For this, the algorithm is given a value range [min,max] and a number n of desired ...
43 views

### Reverse vowels in a given string, take the upper/lower case into account

Given a text string, create and return a new string constructed by finding all its vowels (for simplicity, in this problem vowels, are the letters in the string "aeiouAEIOU") and reversing their order,...
29 views

### My algorithm for Equal Sides Of An Array unexpected result

Given an array, find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1. I am ...
79 views

### Whats the expressive way of or'ing a range of bools? [on hold]

I recently came across a situation where i needed to 'or' a range of bools together. An example could be std::vector<bool> bools; I have come up with some different ways of or'ing all of them ...
41 views

### Algorithm for Baccalaureate exam on JS

I would like to write an algo depending on the mark you have. If your mark is stictly inferior to 10, you are "recalé". If it is between 10 and 12 (not included), you are "reçu". If it is between 12 ...
52 views

### fixing a recursive backtracking algorithm code

i need to find an algorithm that use a recursive backtracking algorithm which will print me all the possible compositions to x1+x2+x3 to be equals to the number. lets say the input number is 4 so the ...
22 views

### How to implement stop order in a trading platform? [on hold]

I'm working on a trading platform. Now I need to implement the stop loss order functionality Description: Suppose that market price for BTC/USD is 7900 at the moment and you have 4 BTC in your ...
40 views

### How to find the two largest disjoint subarrays of a given length?

I need to write a function that takes in as input an array A of positive integers, a positive integer B and a positive integer C. The function should then return the total sum of the elements of two ...
75 views

### Standard Algorithms to implement Transliteration and Transliteration Suggestion

I have constructed an algorithm to transliterate from English to multiple languages, Since we should show them appropriate suggestion for the words they have entered, I have made logic to search in ...
60 views

### Java backtracking program for N queen: a StackOverFlow error

I am currently taking an online course that teaches various programming puzzles. This week's puzzle was N-Queen problem, and the presented code was written in Python. It is the following: python def ...
15 views

### Algorithm for determing a ranking after choosing repeatedly between to items

I would like to write a little program to help me rank items based on the result of a lot of one on one comparisons. So if I have 100 items i would let users repeatedly choose between two randomly ...
96 views

### invariant of binary search for finding first occurrence of an element

I have a problem with defining the invariant of finding the first element of a binary search. (I have a sorted array a and I want to find the first element that is equal to some number q and if it ...
18 views

### Describe an algorithm to move the end of a robot arm (which has two elbows that can move 90 degrees up or down) from position to another [on hold]

[Question 1.2 from Data Structures and Algorithms by AHU: http://orion.lcg.ufrj.br/Dr.Dobbs/books/book9/mf1201.htm] Consider a robot arm that is fixed at one end. The arm contains two elbows at each ...
33 views

### Metasearch - Remove duplicated pictures with different resolution - improving on current approach

Assume that one picture with different resolutions from one host has more than one copies. At the metasearcher stage, I want to check if the 2 pictures have the same names, but not trivial names (...
45 views

### Perimeter around a 2D grid-shape

Given a 2D grid of square cells of the form grid[x,y], I would like an algorithm that produces an ordered set of points that form the perimeter of the shape. In other words, the algorithm would ...
71 views

### Distinct Count algorithm

I am wondering if it is possible to do an approximate distinct count in the following way: 1. I have an aggregation like this: country unique products sold helper_data -- limit 1MB size ...