Questions tagged [algorithm]

An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.

Filter by
Sorted by
Tagged with
0 votes
1 answer
99 views

subsets in array. Algorithm (No Linq)

Task: A subset is a continuous segment of an array. Two integer arrays are given with elements from 1 to 100: of N and n (N > n). For every subset of the first array of n length find out the next ...
0 votes
0 answers
22 views

Highlighting text with custom colors based on many regex

After quite a few attempts I've got a working solution with a Highlight react component. I'm not sure the algorithm is very clean or efficient mostly when it comes ...
  • 121
-2 votes
0 answers
27 views

Is this python code iterative or recursive? [migrated]

I have written the following program in python: ...
0 votes
0 answers
43 views

Searching for all paths between two terminal nodes in an undirected, unweighted graph using DFS in Java

I have this small program for building undirected, unweighted graphs and finding all the paths between two given terminal nodes: ...
  • 26.4k
6 votes
1 answer
87 views

ASCII art smoke wisp generator

I've written a Python function that generates a smoke wisp/genie-tail shape using all 26 (and only 26) letters of the alphabet. Here are some examples of the kind of ASCII art I'm trying to produce: ...
1 vote
2 answers
85 views

Convert camelCase function name to snake_case

I have the following code: ...
  • 11
1 vote
1 answer
86 views

C# Get All Diagonals Jagged Array

How can I improve this? The idea is to store every left to right diagonals in a list Not looking for a more efficient algorithm instead something readable, LINQ perhaps? input: 3, 1 2, 5, 7 1, 5, 8, 3,...
  • 103
2 votes
2 answers
90 views

Get objects from list based on object's field

I have a method which takes List<Document> documents as parameters. I created unit test for it: ...
  • 145
0 votes
0 answers
20 views

How to simplify Tarjan's algorithm implementation in JavaScript and make it non-recursive (i.e. iterative)?

I adapted this Tarjan algorithm in JavaScript to be a little more functional and not pollute the Vertex object with temporary metadata, and arrived here: ...
  • 331
3 votes
1 answer
289 views

How can I optimize double for loop in matrix

I am trying to optimize a function called place_block. It is meant to give the best row, col coordinate in the matrix to place a given block of row and column size. ...
2 votes
1 answer
111 views

Finding the 4 squares that sums up to a given number

I wrote this algorithm in JS with the main goal that it will give an answer for 10^750-10^1000 in 2-3 seconds but it solves 10^150 in 2-3 seconds so I am a little far from my goal, the problem is ...
  • 129
4 votes
1 answer
414 views

Find center of an undirected graph

Task: implement an algorithm to find graph center \$Z(G)\$ given undirected tree \$G\$. This is my first time programming in C++ so any (elementary) feedback is appreciated. The way I did it is: Run ...
2 votes
2 answers
73 views

Find differences between two arrays

I have to identify added and deleted items between two objects with the same structure. ...
0 votes
0 answers
134 views
+200

Integer hash function for concurrent map implementation golang

I am using concurrent map from this repo which only uses string as the key and it doesn't have any implementation for key as integer (int64) so I tried implementing it by just replacing all ...
  • 1,587
2 votes
1 answer
30 views

Optimising function to strip honorifics from names

Problem I have a list of around ~1000 honorifics, see below for a sample. Given an input string of a name, for example ...
  • 233
0 votes
0 answers
28 views

Java pathfinding in general trees - Part 2/4: algorithms

Java pathfinding in general trees - Part 1/4: model classes Java pathfinding in general trees - Part 2/4: algorithms (this post) Java pathfinding in general trees - Part 3/4: demonstration Java ...
  • 26.4k
2 votes
2 answers
70 views

Check if a date is in an interval defined by partial/periodic dates

I can define an interval with start and end in the format YYMMDD, but they can also be ...
3 votes
1 answer
36 views

Optimizing code for this function that filters a JSON file

I made a function that turns all child from a JSON file to parents (or brothers?). It adds the parent name to the key, so there are no duplicates. It works ok, but I would like to ask if there are ...
  • 31
1 vote
1 answer
111 views

Euler's totient function for large numbers

I made this algorithm to compute Euler's totient function for large numbers. A sieve is used. ...
0 votes
1 answer
67 views

Newton-raphson to find the square root of an arbitrarily sized number with arbitrary precision

I read here that "Furthermore, for a zero of multiplicity 1, the convergence is at least quadratic (see rate of convergence) in a neighbourhood of the zero, which intuitively means that the ...
  • 307
0 votes
0 answers
20 views

Maths check: calculator to estimate the time it takes to memorise a book

I built a tool to estimate the time it takes to memorise a particular book. This book is split into 30 chapters, and there are two editions of it, each with its own number of lines. The tool ...
6 votes
2 answers
632 views

Find all combinations of length 3 whose sum is divisible by a given number

I came up with a suitable solution to a HackerRank problem that failed because the execution took longer than 10 seconds on lists that were of very large size. The problem: Given a list ...
3 votes
2 answers
293 views

Calculate sum of largest sequence of decreasing odd ints

I wrote a method that finds the maximum sum of consecutive decreasing sequence of odd integers. For example: if sequence is 13 9 7 12 13 15 13, then sum is 29 (13 + 9 + 7). I don't think it's as good ...
5 votes
1 answer
86 views

Calculating the energy of the harmonic oscillator using a Monte Carlo method

The problem The partition function for the quantum harmonic oscillator can be written in the path integral formulation as $$Z\propto\int Dx(\tau)\exp\left(-\frac{S_E}{\hbar}\right)=\int Dx(\tau)\exp\...
0 votes
1 answer
49 views

Monte Carlo simulation for the harmonic oscillator

Is there any improvement that can be made to the following code, written to simulate the harmonic oscillator in the path integral formulation with Monte Carlo methods? ...
1 vote
2 answers
117 views

Find the cheapest flight to reach from source to destination

As the input you get the flight schedule as an array, each element of which is the price of a direct flight between 2 cities ...
  • 133
1 vote
1 answer
80 views

Leetcode 55. Jump Game solution

I was working on Jump Game problem on leetcode Question You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your ...
  • 189
1 vote
2 answers
80 views

Object-Oriented Blackjack V2

[Edited] formatting and Flake8 linter corrections Four days ago I posted my first object oriented programming project. Everything I have learned to date is self-taught and posting project to receive ...
2 votes
1 answer
68 views

Sorting visualizer using Java Swing

I programmed a little sorting visualizer a while back and I have been meaning to get some feedback on the overall implementation and on what I could do better to have cleaner code or a "best ...
  • 119
2 votes
2 answers
66 views

Project sequence to multiple sequences using LINQ (or extension methods)

I'm creating a method that expands a sequence. This is a sample of how it works: Input: {1, 2, 3} Output: {1}, {1, 2}, {1,2,3} So far, I've been able to solve the ...
  • 233
0 votes
0 answers
28 views

Longest Subarray With Maximum Bitwise AND

How I can faster count the longest subarray of numbers. This is my solution: ...
0 votes
1 answer
41 views

Which Method is more Efficient when Saving Scraped Data to a File?

When scraping and saving data into a file, Which method is more efficient when saving scraped data to a file? (1) open the file first, scrap , and save the data all ...
  • 131
6 votes
4 answers
166 views

Object-Oriented Blackjack

[Edited: added V2 link] BlackJack V2 - Github This is my first post, and I was actually going to post this on Stackoverflow when I was rather suggested posting the code here, as it may be better ...
1 vote
0 answers
97 views

Entropy-based binning algorithm [closed]

Can somebody have a look at my code for an entropy-based binning algorithm? I follow the idea of this website. The code with some test data can be found here. I think there is definitely some room for ...
1 vote
1 answer
62 views

Selection algorithms (order statistic) in Java: linear time vs. sorting selector vs. Quickselect

This post is about computing order statistics: given an array \$A\$ (not necessarily sorted), find the \$k\$th smallest array component. The entire repository is here. ...
  • 26.4k
1 vote
1 answer
50 views

How to using custom colormap with pixel calculation, gray scale value by opencv and make it workable on camera [closed]

I tried to implement the mcolor colormap in the paper at page 8 to 10. I developed the code to process a image file and it works ok. Now, I want to adopt this image processing method to the camera ...
10 votes
7 answers
2k views

Unidirectional linked list in C

I'm learning algorithms and I wrote the single-directional linked list implementation in C. I need a code review of what can I improve in my code and I want to make sure there are no possible memory ...
  • 101
1 vote
1 answer
46 views

Re-order table columns in HTML dynamically with Javascript

I've a table in HTML looks like this: Subjects n1 n2 n3 subject1 10 0 0 subject2 0 5 20 ...
5 votes
1 answer
59 views

Traverse/transform (xml) tree

I am trying to write a utility for transforming xml trees using single pass and specifying node handlers. I used pugixml prior to this, and the library provides a ...
3 votes
3 answers
432 views

Sort array of associative arrays by the key of the subarrays

I am trying to sort an array of associative arrays in reverse order by the key of the subarrays. I solved it with a bubble sort algorithm which worked just fine in my test. I was wondering if there is ...
  • 243
4 votes
3 answers
242 views

Increase efficiency of stick cutting algorithm

I have this typical code interview type problem. Suppose you have a stick of length n. You make X cuts in the stick at places x1, x2, x3 and so on. For every cut in X cuts you need to print the length ...
0 votes
1 answer
123 views

Find factors of a Mersenne number

On RosettaCode I found this C++ version of modPow (compute 2^p mod n) to find the factors of a Mersenne number. ...
6 votes
4 answers
638 views

Counting the number of patterns that exist in a particular string

I am solving this problem where the function returns the number of times a pattern is found in a said string word. It could be easily solved via ...
  • 362
7 votes
1 answer
221 views

Get all partitions of an array into k non-empty sub-arrays

I am writing a function that takes an array and an integer number and returns an array of all the partitions into subarrays (so an array of arrays of subarrays). The number of subarrays is exact the ...
  • 305
0 votes
1 answer
34 views

Improving time complexity for codechef Que [closed]

Question Link. Binary Search and Two Pointers related question. For the above-mentioned question, In other people's solutions, people get AC with a binary search solution. However, I get TLE with ...
1 vote
0 answers
81 views

Find shortest knight's path between two chessboard squares

My function is a solution to the Knight's Travails problem. It receives start and end point coordinates on a chess board, and must return the shortest path from start to end. ...
3 votes
3 answers
315 views

Finding first unique character in string

Below is a solution I came up with (in C++) for an algorithm that is supposed to find the first character in a string that only appears once in the input string (the input string is guaranteed to be ...
3 votes
1 answer
50 views

Segmentation of list to minimize the largest sum

I have written the following code for diving a list 'seq' into 'k' segments such that the maximum sum of each segment is minimized. I have used dynamic programming to solve this problem. But my class &...
0 votes
0 answers
50 views

Three-dimensional DCT implementation for GPU computation in Matlab

This is a follow-up question for Three-dimensional DCT spatial frequency components illustration and Parallel 3D Discrete Cosine Transformation Implementation in Matlab. I am trying to perform three-...
  • 3,646
8 votes
5 answers
2k views

ReversibleList: a list that is "reversed" in O(1) time

UPDATE: Thanks everyone for the suggestions! I made a follow up version here which fixes the bugs, incorporates the feedback, and has more tests. A ReversibleList ...
  • 422

1
2 3 4 5
101