﻿ Newest 'algorithm' Questions - Stack Overflow

# 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.

96,180 questions
Filter by
Sorted by
Tagged with
24 views

### The most important question for me: is there a better way off doing it and if there is, can you guys point me to the right direction?

I had a task like this. The section in this chapter called Alice in Wonderland, again! started with the observation that the merge algorithm uses a pattern that can be reused in other situations. ...
21 views

### Java - How can I make this “Edit Distance” algorithm/code more optimized?

I have a piece of code that calculates the edit distance between words and works, but it's apparently not fast enough. Code: ClosestWords.java: import java.util.LinkedList; import java.util.List; ...
6 views

### Cost of BFS in directed cyclic graph

I have a direct cyclic graph and i need to find a path that cost at least K. I've implemented an algorithm that resolve the problem using BFS and now i'm studying it's cost. What's the cost in the ...
9 views

### Scala Spark execution of RDD contiguous subsets

It's almost 4 days I'm struggling with this problem and I cannot find an efficient solution. I have an RDD in Spark in the form RDD[(Int, (Date, Double)] (the first value is just an index). What ...
55 views

### Optimising code to find offset of the last occurrence of a string in a text

I need to find the offset (from the beginning) last occurrence of a string using the brute force approach. I have to test my code against multiple test cases. text is the string that has to be ...
16 views

### For each node v: find the cheaper colorful path from s to v

Let G = (V,E) where each node has one of three colors {R, G, B}. A path (not necessarily simple) will be called colorful if it contains all the colors. Input: directed Graph G as above (including ...
66 views

### Java - Find the difference between two arrays with duplicates

I've implemented a method for finding the difference between two unsorted arrays. Currently I have achieved getting the diff without duplicates. But how to make it take duplicates into consideration ...
28 views

### How to reduce complexity of an algorithm to find min/max average-closest element an in array? [closed]

I try to solve a basic algorithm challenge that needs time-effective code implementation. In this challenge, I'm going to be given an array of integers which includes n elements. Then, my task is to ...
59 views

### Why recursion behave differently in the two examples?

I have two examples of recursion, one calculate the sum of all elements of the array and the second return the count, both examples behave differently and i can't relate! First Example: const ...
81 views

### Whats the most efficient way to sort elements of an array based on frequency of elements

I have an array and i've used the following method to sort the array based on the frequency of the elements. I store the elements and their respective frequency as key value pair. I store the values(...
27 views

### Image segmentation using deep learning approach

I am working on a segmentation task using Deep learning approach.my dataset contain images for the whole heart and I want to segment the left atruim. So if I want to get sub-image containing the left ...
28 views

### String search algorithms and MD5 Hash [closed]

My current scenario is the following: I have 4 different files: T1 (1000 HS) T2 (10000 HS) T3 (1000000 HS) T4 (10000000 HS). Let's say on: T1 – Find 141ac11891fc6c57e0920f509f879a1d in 1000 HS T2 -...
53 views

### 3SUM, but duplicates allowed

I am trying to solve the 3SUM Problem, but duplicate triplets are allowed. For example, suppose we have the array [2 0 -1 1 -2 3 3]. Here, the solutions are (2, 0, -2), (0, -1, 1), (-1, -2, 3), and (-...
55 views

### How to find the substring if the substring has random characters replaced?

Let's say we have a string in Python: original_string = "TwasTheNightBeforeChristmasWhenAllThroughTheHouse" And we are interested in finding the beginning coordinates of the substring substring ="...
43 views

### Minimum Step Algorithm(Java) [closed]

I have a number line 0 to infinity. I am incrementing towards a certain number. At any step, I can change my Increment by -1, 0, or 1. How many steps does it take me to get to a certain number x, if ...
24 views

### Asymptotic Bounding | If f = 2^n and g = (2^n+1), how does f = Θ(g)?

We are asked to indicate whether f = O(g), or f = Ω(g), or both (in which case f = Θ(g)). To solve the big O, I found it easy by simply providing constant C = 1 in which case 2^n <= 1(2^n+1). I ...
63 views

### Find the maximum possible value [closed]

Given the first and the last integer of an array, a and b, and the size of the array -- n, where n-1 >= |a-b|. The difference between the adjacent integers in the array does not exceed 1. Among ...
55 views

### Does this algorithm have time complexity of O(n) or O(n^2)?

public static void reverseWords(char[] message) { reverseCharacters(message, 0, message.length - 1); int currentWordStartIndex = 0; for (int i = 0; i <= message.length; i++) { ...
67 views

### Why did removing “else” without changing logic cut my runtime in half?

After solving this problem on LeetCode: https://leetcode.com/problems/count-and-say/ my first solution took 16ms. By removing an else statement, without (as far as I can tell) changing the logic in ...
17 views

### How to generate an optimal array for quicksort with first element as pivot?

I'm practicing measuring timing for algorithms and I wanted to test different timings for QuickSort. I'm trying to write a function that takes a sorted array of length n as input and generates an ...
30 views

### Creating RPN calculator using c#.NET with expression trees

I am new to expression trees and haven't done c# in a while now. I need to create reverse polish notation calculator using expression trees. So far I have created 2 stacks one is for operations and ...
27 views

### Trying to implement a stack and the bracket matching problem but cannot figure out some semantic errors

I am required to write my own stack using an array which then I have to implement the bracket matching problem. This is my algorithm: if we're given the string: "(())" it is returning "not balanced" ...
33 views

### Merge sort with insertion function

I have two sorted linked list l1 and l2. I wanted to merge l2 into l1 while keeping l1 sorted. My return statement is l1 sorted and l2 is an empty list. How do I approach this problem by using ...
38 views

### Creating recursive folders with a linux bash script

I've asked this once before, and it was marked as it needs more details. Well here it is in great detail. Goal is to have a script that creates folders as follows: main After creating a single ...
31 views

### Is there an explanation for a change in behavior in applying MD5 hash to a set of files in parallel in Java?

All! I'm trying to find a faster method of calculation the MD5 sum of a large set of files, in order to identify duplicates for personal purposes. I'm using Timothy Macintha fast hash implementation ...
25 views

### how to fill out space like the snake from snake game? [closed]

Im currently struggling to come up with an algorithm for this problem. Given an image (Example:) A snake or turtle (like from the Official Snake Game) should fill out the white space. Its only ...
61 views

### Pick random element subset from map

I have a map of elements: std::map<char,int> values; values['a']=10; values['b']=30; values['c']=50; values['d']=70; values['e']=90; values['f']=100; values['g']=120; ...
95 views

### Merge numbers at same level

I have a number of integers in a list, now I am working on a task where I can sum two numbers ifthey are same and add that back to my list in next step. Now I want to find out how many numbers will ...
74 views

### C++17 Iterate over a subset of parameter pack

I have a struct receiving a parameter pack. Let's assume that the parameter pack will never have a size smaller than 3. Also, the std::array found in the struct should be evaluated at compile-time. I ...
55 views

### Number of characters compared during naive string matching

I've been asked to find the number of characters that are compared during naive string matching. This was the function we were asked to implement: // Count the number of characters compared while ...
40 views

### Generalization of strings and state machine optimization

I am looking into learning state machines that accept given samplings of a regular language, you can allow or disallow noise in the sampling, lets assume no noise for simplicity. Example: given the ...
13 views

### Burrows-Wheeler-Transformation permutation

In the BWT the last column is used to recreate the original string. During this process each element gets its own number. Iam searching a method of distributing the numbers so that i get a ...
17 views

### How to solve for different base multiplication of an array in Java [closed]

I have a matrix of 2x3 and 2x4 ; how can i proceed to solve the multiplication of the matrix by either trimming/adding a row or a column or by using some other method . Please help me out here ,, it ...
87 views

### Count characters ocurrence in string in a perfomant way?‽?

I am doing the following programming exercise: Numericals of a String. The statement is: You are given an input string. For each symbol in the string if it's the first character occurence, ...
78 views

### Count the number of intervals that fall in the given range

Suppose you have a list of intervals, such as [(0 4), (1 3), (2 5), (2 6)]. This list is not sorted. Then you are given a range, such as [1 5]. You have to return the number of intervals that fit ...
68 views

### Find super-string in a set of strings

I have a list of strings, like: cargo cargo pants cargo pants men buy cargo pants men cargo pants men melbourne buy In this, the string that contains all remaining strings is cargo pants men ...
63 views

### Print a new <tr> each time & “N” number of elements inside it, for a total quantity of “Q” [closed]

i have a variable Q, i want to print "cat" (or whatever) Q times in HTML table rows, only each row of the HTML table can contain maximum of 5 cats. like if Q <= 5 then i want to print a single <...
36 views

### Why does this code return an empty array?

Question: Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Desired output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Aren't arrays passed ...
39 views

### List all sums of k objects in set of n integers IN DESC ORDER

I have a large set of n integers. I need to choose k elements in that list such that they sum from largest to smallest. Every subset chosen will have to be valid for some rule (it doesn't matter the ...
35 views

### Implementing Merge Sort with Java

I was trying to implement merge sort using java, but it's saying: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at HelloWorld.merge(HelloWorld.java:42) at HelloWorld....
24 views

### Least cost increasing subsequence

Say we have an array A that contains N integers. The problem is that we want to minimize the cost of some increasing subsequence(not necessarily strictly increasing) starting at position 1 and ending ...
68 views

### Algorithm to move balls from one set of buckets to another set in least amount of moves given a machine that can move unlimited balls in one move

One of my friends proposed a problem where you have n number of buckets (a, b, c, ..., n), each with a certain percentage of your total balls. You are then given a breakdown of how many balls each ...
25 views

### Edit Distance: why dp[i-1][j-1] is always smaller than or equal to dp[i][j-1] + 1?

Edit distance is defined as follows: Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a ...
31 views

### How to reverse a linked list in-place and in one-pass from position m to n? The linked list is single pointer linked list [closed]

Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list
46 views

### Algorithm for Finding Graph Connectivity

I'm tackling an interesting question in programming. It is this: we keep adding undirected edges to a graph, until the graph (or subgraph) is connected (i.e. we can use some path to get from each ...
136 views

### Minimum cost path from (0,0) to (N,N) on 2D grid

I have a problem with a 2D grid, where you are trying to find the shortest path from (0, 0) to (N, N), where 1 < N < 10^9. There are also P (1 < P < 10^5) shortcuts, where you can jump ...
49 views

### What is the best approach to solve this sorting problem?

To participate in a prize draw each one gives his/her firstname. Each letter of a firstname has a value which is its rank in the English alphabet. A and a have rank 1, B and b rank 2 and so on. The ...
31 views

### minimum RGB path

You are given a directed graph G = (V, E) weighted edges. Nodes are colored red, green and blue. A path from s to v is called colorful if it contains red green and a blue node. find the shortest ...