## Good way to print all time intervals from event object

I am working with an event object that includes a start and end time. I want to print out all of the intervals for some arraylist of event objects, but can only think of a trival brute-force solution. Assume our arraylist has the following events: ...
more »

## Getting Infinite Loop Babylonian Algorithm R

I am trying to do the Babylonian Algorithm in R and can't quite figure out where I'm wrong. Basically, I'm trying to calculate the square root of x using the Babylonian algorithm. It consists of guessing the root of x, - (let's call this r). Let's sa...
more »

## Selective timeout based handling: immediate first, debounce next

Let's say there are random sequences of external actions (e.g. scroll events). I need to handle the first action immediately, then dismiss all actions occurred with intervals less than some given delta, and then handle the next one which should be de...
more »

## {Java - PriorityQueue} time complexity of this code

Given an array containing N points find the K closest points to the origin (0, 0) in the 2D plane. You can assume K is much smaller than N and N is very large. E.g: given array: (1,0), (3,0), (2,0), K = 2 Result = (1,0), (...
more »

## In Python, how can I prevent a double for-loop from returning the same indexes of a list?

I am currently working on problem 1 in Leetcode, named "Two Sum." Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and...
more »

## Remove last matching element in an array that meets a condition

I'm working on a Kata. Given a list lst and a number N, I am creating a new list that contains each element of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], I take [1,2,3,1,2], drop the next [1,2] s...
more »

## "Mortgage" - code not passing the last system test [TOPCODER]

I am trying to solve the Mortgage problem on TopCoder. My code works almost good - it passes every system test except the last one, where instead of "1976284585" it returns "1976284584". I tried to solve this problem, I can't do it though. Can you he...
more »

## Challenge: Find the unique value in an array of even or odds numbers

You have an array of numbers in which most of the elements except one will be different, example: All the elements can be even and you have to find the position of the odd element. All the elements can be odd and you have to find the position of th...
more »

## Dynamic Programming Optimal "Non-Decreasing Sequence"

The question says, Given an array A with N integer numbers, output the minimum number of operations needed to make the sequence non decreasing. An operation represents choosing a number in the array A[i], summing it to A[i + 1] or A[i - 1], and de...
more »

## How to do not select duplicated values from db?

I have the following comment table(comment and corresponding info including photo_id) comment_id | photo_id | ... com_1 | pho_1 | ... com_2 | pho_2 | ... com_3 | pho_1 | ... com_4 | pho_2 | ... Frontend sends ...
more »

## Best algorithm to filter data with multiple criteria in Python

I have a multi-dimensional and numerical values only dataset that conceptually look like this: Id Field1 Field2 Field3 1 123 .05 10.33 2 234 .17 5.78 3 345 .01 0.23 4 456 .99 1233 ... ... ... And I...
more »

## algorithm to merge two arrays into an array of all possible combinations

Example given in JavaScript: Suppose we have two arrays [0,0,0] and [1,1,1]. What's the algorithm to produce all possible ways these two arrays can be combine. Example: mergeEveryWayPossible([0,0,0],[1,1,1]) // [ [0,0,0],[1,0,0], [0,1,0], [0,0,1], ...
more »

## Find words and put spaces after them

Once I was asked a question in an interview and I am still not clear how to solve it. Or couldn't even get close to it. Q. Given is a of series of characters eg."thisisachallengingquestion", Write a program to put space after every word except the ...
more »

## What is the most efficient way to tune the settings of a sensor, such that it gives optimal readings?

I have a fairly simple sensor which outputs a measured distance, with a number of adjustable parameters which determine how the sensor takes said measurement. My question is, what is the most programmatically efficient method for zeroing the setting...
more »

## Triple Nested For Loop with specific output (java)

I need to write some java using 3 "for" loops that outputs 122333444455555 22333444455555 333444455555 444455555 55555 The code I have so far: public static void problemFour() { for(int i = 5; i >= 1; i--) { for(int a = 1; a ...
more »

## memoization not working as expected

I am new to algorithms and so I was experimenting with several possibilities of algorithms, specially memoisation I have a simple Fibonacci series recursive function using memoisation class Memoize: def __init__(self, f): self.f = f ...
more »

## Ruby: Averaging values of Array of Arrays while Grouping according to Date

How to average values of the second element of an array of arrays; instead of adding them : [ [ '01-01-2001', 4 ], [ '01-01-2001', 5 ], [ '01-01-2001', 6 ], [ '01-02-2001', 7] ] to [ ['01-01-2001', 5], [ '01-02-2001', 7] ] ? ...
more »

## Algorithms: Hybrid MergeSort and InsertionSort Execution Time

Good day SO community, I am a CS student currently performing an experiment combining MergeSort and InsertionSort. It is understood that for a certain threshold, S, InsertionSort will have a quicker execution time than MergeSort. Hence, by merging b...
more »

## Define an algorithm which gets a number and a list and returns a scalar based on number's distance to average of the list

Let's suppose that we have got a list which appends an integer in each iteration which is between 15, 32(let's call the integer rand). I want to design an algorithm which assigns a reward around 1 (between 1.25 and 0.75) to each rand. the rule for as...
more »

## Graph library for virtual (dynamic) graph search

As a part of a larger algorithm, I need to run graph search algorithm (A* family) on a graph that is created dynamically during the search. I cannot instantiate the whole graph because it is exponentially large though not infinite, and most nodes wo...
more »

## Implementation for Range search algorithm

I would like to ask a question about the algorithm. Suppose there are several segments.(segments number not fixed,maybe so longd) example: segment data range (data range not fixed) **A 0~10** **B ...
more »

## Algorithms - Count all pairs of equal numbers in a sorted array in O(n)?

A question that has me speculating is the following: Let's say we have a sorted array with the numbers {1,1,1,1,2,2,4,4,4}. Now, given that we can clearly see that we have six pairs on 1's, one pair of 2's and three pairs of 4's (10 pairs). How wou...
more »

## search a line in preprocessed big text file

I have a data file which contains 100,000+ lines, each line just contains two fields, key and value split by comma, and all the keys are unique. I want to query value by key from this file. Loading it to a map is out of question as that consumes too ...
more »

## N-length sequences of pairs following conditions

I have the following 2d-array of fruits: fruits = [["apple", "lemon"],["apple", "cucumber"],["carrot", "lemon"],["carrot", "cucumber"],["peach", "cucumber"],["lemon", "melon"],["grape", "cucumber"],["lime", "lemon"],["lime", "cucumber"],["apricot", ...
more »

## Algorithm for many images and their color palettes

For a project I'm looking for an algorithm to convert a lot of images to paletted images, which can share same palettes. The short story Given: A list of images (RGB), which already have the final colors which should be used. Result: A list...
more »

## How to search a very large array in C++ for a particular value?

I was asked this question by an interviewer. How can I search a large array (thousands maybe millions of values) for a particular value. I suggested binary search for cases in which the items are sorted and the array size is smaller. I also sugges...
more »

## Number of times all the numbers in an array are divisible by 2

I am trying to get the count of number of times all the integers in an array is divisible by 2 considering only one integer in each step. For example, initially if I have the array : [2,4,2] and count = 0 Step 1 [1,4,2] , count=1 Step 2 ...
more »

## How to generate grid system accept column with gutter and full width column in the same row

all I'm trying to build my own CSS Grid system, so basically I've applied the main algorithm to generate the columns which is var func = (function (numberOfColumn){ var str = "",result = 0; for (var i = 1 ; i < (numberOfColumn + 1); i++...
more »

## How to match and highlight all terms in any order from an array of strings?

I'm aware that there are similar questions asked and answered. Please take the time to understand the specifics before voting to close this as a duplicate though. The requirements are: Find strings from an array (from here on called options) t...
more »

## Year to Century Function

Problem: Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc. My Code: def centuryFromYear(year): century = ye...
more »

## Precalculate Result of A*

Currently learning about the A* search algorithm and using it to find the quickest solution to the N-Puzzle. For some random seed of the initial starting state, the puzzle may be unsolvable which would result in extremely long wait times until the al...
more »

## Optimization of profit in Ruby

I'm using Ruby, but for the purposes of this problem it doesn't really matter. Let's say I have two different kinds of resources, the quantities of which are denoted by a and b. I can allocate d new resources, and since a and b are of equal cost an...
more »

## Find value in javascript array of objects deeply nested with ES6

In an array of objects I need to find a value -- where key is activity : However the activity key can be deeply nested in the array like so: const activityItems = [ { name: 'Sunday', items: [ { name: '...
more »

## Considering Start point and end point on edge, not Node of graph in Path finding algorithm for Road Network Graph

I am working on Path finding algorithm for Road Network. I have details of my road network as undirected graph in db with Node(id,cordinate) and Edge(id,start-node-id, end-node-id,line-points) table. I am using A* path finding algorithm to compute pa...
more »