Category "algorithm"

How to call swap() algorithm on std::list elements?

I'm trying to swap the 2nd and 4th elements in a list, but I can't figure out how to do this. I tried to use vector notation, but it didn't work. list<int>

I am confused between shortest path finding algorithm and graph traversing algorithm

My understanding is that BFS and DFS are graph traversing algorithm while other algorithms like A* and dijkstra are for finding shortest path between two nodes

Group up date ranges by interesecting ranges

This is a variation of the gaps and islands problem. I tried finding solutions but they are all SQL based. Problem: Given a list of date ranges, return a combin

The most efficient solution for finding the minimum value

I have a list of Sugar. The Sugar class has two field: price and weight: data class Sugar(var price: Double, var weight: Double) And I need to find max/min val

Find Indices of the pair of elements in the Array such that they add up to the Target value

I want to find a target sum in an array by adding integers until it's reached, then return the indexes which add up to the target by using streams. For example,

Postfix Algorithm Evaluation in JS

this is what we start with: '7.7+7' The input of the function is an array of strings (original input converted to postfix): 7.7,7,+ then would get fed back into

Worst-case time complexity for adding two binary numbers using bit manipulation

I'm looking over the solution to adding 2 binary numbers, using the bit manipulation approach. The input are 2 binary-number strings, and the expected output is

How to make this code faster in python (algorithm question)

Today I was browsing for some questions that I saw this: Algorithm - Air Battle C ++ time limit: 1 second Java time limit: 2 seconds Python time limit: 10 secon

Postponed Sieve algorithm with start logic

Based on the answer by Will Ness, I've been using a JavaScript adaptation for the postponed sieve algorithm: function * primes() { yield 2; yield 3;

Cartesian product but remove duplicates up to cyclic permutations

Given two integers n and r, I want to generate all possible combinations with the following rules: There are n distinct numbers to choose from, 1, 2, ..., n; Ea

Simple algorithm to compute coins that form minimum number of coins (or any valid number of coins) from a limited number of coins

I need to find the coins needed to make up a specified amount, not the number of ways or the minimum number of coins, but if the coins end up a minimal number t

Count nodes within k distance of marked nodes in grid

I am attempting to solve a coding challenge however my solution is not very performant, I'm looking for advice or suggestions on how I can improve my algorithm.

given a number k and a graph is there a DFS run that will give forest larger then k

I was given a question that I can't seem to solve. given a directed graph G=(V,E) and a natural number k, k>0. Find an algorithm that will return "YES" if th

How can I manage duplicate letters in Wordle Game (JavaFx)?

In my algorithm to compare the secret word with the guessed word, I need to manage the duplicated letters. For example, if the secret word is newly and the gue

Distirbute Candy - Finding minimum reproducible example of the problem

The question is to distribute candies to N children.Each child has a rating. Distribution should be such that each child have at least one candy and children wi

How to calculate multi-operator of list in dart?

I have a list of num and string of operator above: final List<dynamic> items = [5.0, "-", 2, "*", 3]; I want to calculate the value inside of it. I can d

Coursera DSA Algorithmic toolbox week 4 2nd question- Partitioning Souvenirs

Problem Statement- You and two of your friends have just returned back home after visiting various countries. Now you would like to evenly split all the souveni

Trouble simplifying conditional

I'm having trouble simplifying a conditional statement. It looks like this: There is an array of arrays like this, dice = [ [a, b], [c, d] ]. a, b, c, and d rep

k-diff sequences in a float array

Looking for an algorithm to find longest sequences (pairs, triplets, up to quadruplets) that are separated by a constant, non-integer difference k in a sorted a

Time complexity of nested loop?

I'm not very familiar with Big O notation. I'm wondering does the time complexity of the nested loop always be n^2? For example the following function, the inne