Category "algorithm"

Find the maximum number of distinct elements that can be achieved in array a after at most k operations

My assignment: Consider two arrays a and b where each consists of n integers. In one operation: Select two indices i and j (0 <= i,j < n) Swap integers a

Given an array find all the pairs with product between a given range

Given an array having n elements, find all the possible pairs of elements whose product is between x and y provided by the user, i.e. all i and j less than n, s

Reasonable neighbours for a bees algorithm on a not-complete digraph

I am trying to solve an optimization problem for a graph, where basically there is a cost for transporting "package" over a single edge, and I have multiple sou

Island perimeter problem - cannot count all neighbor cells

I'm working on the Island Perimeter Problem from LeetCode. I tried to use the DFS algorithm, based on LeetCode#200, to solve this problem. For example 1, the ou

How to calculate median of an unsorted frequency table in O(n)?

I have a dataset comprised of n unsorted tuples representing numbers (let's say specific color codes) and their frequency (number of times of appearance). I wan

Is it right to implement queue with single linked list?

My Code ↓ Some macros defined by #include <stdio.h> #include <malloc.h> #define ElementType int #define ERROR 0 // 队列ஷ

Extracting data from a .txt file without using modules

I am taking a course in python and one of the problem sets is as follows: Read in the contents of the file SP500.txt which has monthly data for 2016 and 2017 a

How to solve TSP problem using pyGAD package?

Using PyGAD Package, how to generate the item of populations with element not duplicate between 1 and 12? It is always has duplicate value in random population.

Show that Dijkstra on the potential-modified graph is same as A*

Section 4 of this paper states that it is easy to see that A* is equivalent to dijkstra used on a modified graph where edge weights add the potential from the o

Can we use C++ standard algorithm like std::mismatch instead of for loop for iterating over two equal length sequences?

Consider two vectors: vector<int> A = {1, 4, 3}; vector<int> B = {5, 7, 1}; It's given both vectors are of equal length. If I need to use element b

Cannibals and missionaries problem python -artificial intelligence algorithm

I'm trying to solve the cannibals and missionaries problem in python (with some additional criteria, but the main idea is the classic one). So, in the class Gra

How to solve a partition problem using recursion only

I got a partition problem for which I need advice. I'm given a 1D array whose length is even. I need to write a boolean method to determine whether the array ca

What would be the time complexity of this two nested loops

given: public void trash(int n){ int i =n; while (i > 0){ for (int j = 0; j < n; j++) { System.out.println("*"); i

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