Category "algorithm"

Integer Programming for NNC

I'm trying to implement Integer Programming for Nearest Neighbor Classifier in python using cvxpy. Short intro Given a dataset of n points with a color (red or

Integer Programming for NNC

I'm trying to implement Integer Programming for Nearest Neighbor Classifier in python using cvxpy. Short intro Given a dataset of n points with a color (red or

How solve Codility equiLeader challenge

After finding the "leader" I tried finding the possible equiLeaders in the given array but couldn't. Tried to find a solution of someone else but can't wrap my

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. and here is my code function adjacentEle

Sort a List in Dart based on how another List was sorted

I am trying to sort a list with reference to another list. Consider this example, List<String> a = ["a", "b", "c"]; List<int> b = [2, 3, 1]; Now,

Custom Hashing Algorithm

Out of sheer boredom, I decided to write a hashing algorithm in python. It all works, however I've got a couple problems: Results are similar (e.g. hash(1234)

Does the .includes method in JavaScript loop through the array and case O(n)?

Does the .includes method cause O(n^2) when run with another loop such as in this simple example? function myFn(){ let age = people.map(person => {

In SPFA Shortest Path Faster Algorithm why does it have to check if current vertex is in queue before adding it to queue?

procedure Shortest-Path-Faster-Algorithm(G, s) 1 for each vertex v ≠ s in V(G) 2 d(v) := ∞ 3 d(s) := 0 4 push s into Q 5 w

Throw an error if multiple shortest paths are found

Given an undirected weighted graph, a start, and an end point. You need to find the shortest path to that end point, but throw an error if multiple shortest pat

Initializing a variable with INT_MIN in Python 3.8 [duplicate]

About the code The maximum sum subarray problem is the task of finding a contiguous subarray with the largest sum, within a given one-dimensio

Optimal Selection for minimum total sum

This is a problem from competitive programmer's handbook: We are given the prices of k products over n days, and we want to buy each product exactly once. Howev

Working with small probabilities, via logs

Source: Google Code Jam. https://code.google.com/codejam/contest/10224486/dashboard#s=a&a=1 We're asked to calculate Prob(K successes from N trials) where

Largest Triple Products without using sort?

I implemented the Largest Triple Products algorithm, but I use sort which makes my time complexity O(nlogn). Is there a way to implement it without a temporary

Reasons for using a Bag in Java

I am currently studying about Algorithms & Data Structures and while I was reading over the Book of Algorithms 4th edition, I discovered the Bag data-struct

Extract the century of a date from a string

My task is to output the current century of a given date. The date can be represented as a string; for example, 19.03.2022. How can I retrieve the year and cent

Understanding the Big O for squaring elements in an array

I was working on a problem where you have to square the numbers in a sorted array on leetcode. Here is the original problem Given an array of integers A sor

How to approach this Dynamic Programming Meal Plan Problem?

I came across this problem in a practice interview. I don't want a direct answer, just some help with the intuition of the problem, both your brute force thinki

Is there a known algorithm for finding which K elements out of N elements have a sum that is closest to a whole number?

As a small example, say I have N=6 elements { 0.03, 0.25000039, 1.391, 500.1, 0.5000001, 1.75001 } and K=3 then the combination { 0.25000039, 0.5000001, 0.

count words in a string without using split

I have a problem on which I am working where I need to count the number of words in a string without using the split() function in Python. I thought of an appro

how to calculate XOR (dyadic) convolution with time complexity O(n log n)

“⊕” is the bitwise XOR operation. I think Karatsuba’s algorithm may be used to solve the problem, but when I try to use XOR instead of