Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

4
votes
0answers
23 views

Warped Audio Sample

I was asked this question and got no feedback, so I'd like to ask you for any feedback on my solution, Thanks! Think of an audio sample as a rubber-band that you want to pin to a musical time ruler. ...
2
votes
1answer
22 views

Python merge sort

In Algorithms Fourth Edition by Robert Sedgewick and Kevin Wayne, the exercise 2.2.10 states that: Implement a version of merge() that copies the second half of a[] to aux[] in decreasing order ...
2
votes
0answers
28 views

Find out whether a given text contains any of the prohibited words

We have been given a list of strings which are blacklisted. The goal is to identify if a given text contains any of these blacklisted strings. The restriction here is that the blacklisted string needs ...
2
votes
2answers
68 views

A recursive algorithm to find the number of distinct quantities of money from a pile of coins

Problem: find the number of distinct quantities of money that a pile of coins can make. The vector coins contains the values of the coins, and the corresponding ...
1
vote
1answer
43 views

Implementing `components(separatedBy:)` method in Swift

On looking at the components(separatedBy separator: String) -> [String] method from the Swift Standard Library, I tried to come up with an implementation just ...
-4
votes
0answers
9 views

SPOJ is giving WA for this solution of ROCK [on hold]

I am solving ROCK problem on SPOJ. I dont know why I am getting WA here. Tried many testcases. ...
-1
votes
0answers
34 views

LeetCode 234. Palindrome Linked List, a TLE weird case [on hold]

Here is my AC submission. And I found a wierd circumstances when I remove this line dumyHead->next = NULL; my submission will yield a TLE. I know if I don't set ...
1
vote
2answers
47 views

Python: count number of pairs in array(list)

A solution for a small algorithmic problem. I believe tests are pretty descriptive: ...
3
votes
1answer
68 views

Hackerrank “Almost Equal” solution

I've spent the best part of a day on this question. It is marked as Expert level. There are about fifteen submission test cases and my solution manages to satisfy the first four. However, from there ...
3
votes
0answers
30 views

Distance transform on image using NumPy

I would like to find the find the distance transform of a binary image in the fastest way possible without using the scipy function distance_transform_edt(). The ...
-4
votes
0answers
12 views

It's a leave calendar that takes new employee start employment date and calculates the remaining annual leave that employee is qualified for [on hold]

I've spent hours going through this and I feel I am missing simething. Each employee has random annual leave totals somewhere it's broken
3
votes
2answers
120 views

Stack implementation in C++ including min(), max() functions in O(1)

This implementation seems to work. I would love to get any suggestions / comments on how this code could be improved. Are there any potential memory management issues? Any additional methods that ...
1
vote
0answers
17 views

Sorting an array of numbers in Java with an algorithm

This is pretty much one of my first programs that I have created in Java and I just wanted to ask if anyone sees some obvious errors or mistakes I made. The purpose of this program is to sort numbers ...
0
votes
0answers
19 views

Volatility indexes calculation

I created a function to calculate the volatility indexes (abbreviated to VIX) using the old method. However, since I have no sample data, I have no idea whether I implemented my code correctly. Can ...
0
votes
1answer
51 views

Merge sort approach in Python

I'm working my way through a book on algorithms. One of the first challenges is the merge sort. I've seen this implemented before, but as the book suggested I coded one from just the method without ...
2
votes
2answers
127 views

My own algorithm to sort integers

I'm studying the C language with a Harvey Deitel book. Trying to solve an exercise that asked to write a good sorting algorithm, I did this: ...
2
votes
1answer
70 views

Speed up Magic square program

I have written a Java program to calculate magic squares with recursion and backtracking. A 3*3 Magic square is calculated in about 1 sec, but a 4*4 needs about 50 minutes on my laptop with Intel i5. ...
0
votes
0answers
44 views

Huffman Coding API in Swift

This question is a follow up question to: this post I'd love some advice on my implementation and the API of the Huffman class. I'm also not sure how to test if ...
2
votes
1answer
76 views

Robot Block Command

Problem We have a robot that can pickup blocks from a stash, move them horizontally and lower them in place. There are 10 positions available to lower blocks number 0 to 9. Each position can ...
1
vote
0answers
35 views

Maximize product of sum of subsequences [closed]

Code Chef Problem description You are given a sequence of \$N\$ powers of an integer \$k\$; let's denote the \$i\$-th of these powers by \$k^{A_i}\$. You should partition this sequence into two ...
1
vote
0answers
35 views

Sorting cards by suit and value using Shell Sort

The question description: put a deck of cards in order by suit (in the order spades, heart, clubs, diamonds) and by rank within each suit, with the restriction that the card must be laid out face down ...
2
votes
1answer
82 views

Huffman Coding in Swift

I’m learning Swift and decided the Huffman Coding algorithm would be a good exercise while learning this new language. Below is a working version that encodes a ...
2
votes
2answers
87 views

Substrings search (Aho-Corasick algorithm)

I have a code where the goal is to give score to strings based on how much score certain substrings are worth (including repeating substrings). The way my code (which is based on the Aho-Corasick ...
2
votes
2answers
84 views

Find kth smallest element in binary search tree

I was faced with below interview question today and I came up with below solution but somehow interviewer was not happy. I am not sure why.. Given a binary search tree, find the kth smallest ...
2
votes
2answers
66 views

Given some integers, find triplets where the product of two numbers equals the third number

I am trying to solve a coding challenge: Given N integers A1, A2, …, AN, count the number of triplets (x, y, z) (with 1 ≤ x < y < z ≤ N) such that at least one of the following is true: ...
0
votes
0answers
50 views

Verify binary search tree with duplicate keys

Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, ...
3
votes
1answer
93 views

“Counter Game” Powers of 2 Game

I have been working on this hackerrank problem: Louise and Richard have developed a numbers game. They pick a number (1 ≤ n < 264) and check to see if it is a power of 2. If it is, they divide ...
1
vote
0answers
26 views

Dijkstra Implementation: Comparing my program with widely available Internet Java program

I wanted to discuss Dijkstra's implementation that I generally find on Internet, and then compare it with my own implementation. On Baeldung and on Vogella (Thanks to both and many others, that I ...
5
votes
0answers
81 views

2-opt algorithm for the Traveling Salesman and/or SRO

In this question I present a method to solve the Traveling Salesman Problem and/or the Single Route Optimization problem. I am extracting 100 lat/long points from Google Maps and placing these into a ...
3
votes
1answer
57 views

Checking whether given array is sorted by divide-and-conquer

I've written a code that I try to use divide and conquer approach to determine if the given array is sorted. I wonder whether I apply the approach accurately. ...
0
votes
1answer
29 views

Merging K Sorted Linked Lists

Specifically, how can I improve the time complexity of my algorithm (currently it is O(listLength * numberOfLists))? It only beats 5% of accepted LeetCode solutions,...
0
votes
0answers
50 views

Even and odd permutations in prolog

This is the exercise 3.3.1(iv) from The Art Of Prolog where it's asked to find even and odd permutations. I used Heap's algorithm which finds all permutations which always have one step from each ...
5
votes
3answers
119 views

Singly-linked list data structure implementation

This code seems to work correctly. I would appreciate any comments on how to improve the code, e.g. readability, algorithms, const-correctness, memory, anything else I am forgetting? Also, for the ...
1
vote
0answers
31 views

Get stacked game state in NHWC format

After reading this, I decided to transition my DQN code from the keras library to tf.keras library (code is located in this repo) and my original code used NCHW format, as it was faster with GPUs. As ...
2
votes
1answer
55 views

Find all shortest paths between 2 nodes in a directed, unweighted, SQL graph

I want to find all shortest paths between a pair of vertices in a unweighted graph i.e all paths that have the same length as the shortest. The edges of the graph are stored in a SQL database. The ...
2
votes
1answer
42 views

Find palindromes in circular rotated string

I'm trying to tackle a programming challenge to determine how many palindromes exist in a given string that is being rotated character by character. My solution works, but it's too slow, and I'm ...
2
votes
1answer
46 views

Map Character to Characters Most Frequently Found With it (in list of strings)

For an interview, I was tasked with writing a program that consumes a list of strings, and produces a mapping between every character in the list, and the characters found most frequently with it (let'...
8
votes
4answers
1k views

Find every possible pair of numbers that sum up to n number

I have this following code which find every possible pair of numbers that sum up to n number ...
6
votes
1answer
84 views

Rotating an Image using own algorithm in python

For an uni assignment I have been giving the task of making my own rotating algorithm in python. This is my code so far. ...
1
vote
2answers
73 views

String Compression

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string "aabcccccaaa" would become "a2b1c5a3". If the "compressed" string ...
1
vote
0answers
84 views

Find the shortest path between two points in a 2D matrix with obstacles

I need to find shortest path between two points in a grid given an obstacles.. Given a 2 dimensional matrix where some of the elements are filled with 1 and rest of the elements are filled. Here ...
3
votes
5answers
670 views

Unique character lookup

I'm solving 387. First Unique Character in a String LeetCode problem defined as: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. ...
1
vote
2answers
36 views

Stack Array implementation of Java

This is my array implementation on stack in Java. It involves push, pop, get and growth when limit of array size is reached. I am a self-taught programmer starting to learn data structures and ...
2
votes
3answers
62 views

“Pivoting” data with VBA

I've attempted to erite some VBA for this this question. The output is OK but the code is not very elegant. I'm happy-ish with creating unique list of names and certificates, but the rest seems ...
1
vote
0answers
28 views

Qsort in C++ using vector

I tried to make a simple qsort() function in a functional way, it takes a vector and a bool pred() Suggestions as to make it more functional, optimize it, and/or ...
-3
votes
1answer
39 views

How to reduce run time of my code to solve Cycle race practice problem from geeksforgeeks

I am referring to this problem https://practice.geeksforgeeks.org/problems/cycle-race/0. The Problem description: Jack and Jelly are two friends. They want to go to a place by a cycle ( Assume that ...
4
votes
1answer
89 views

Performing unit conversions using intermediate conversions

Challenge: Two input files are given The first contains |N| ratios in the form of two labels and a ratio: USD, GBP, 0.69 Meter, Yard, 1.09 YEN, EUR, 0.0077 GBP, YEN, 167.75 ...
1
vote
0answers
30 views

Document distance in JavaScript

I am looking for the fastest way to find document distance. How can I improve this? Split each document into words. replaceAll() is faster than replace() ...
1
vote
0answers
44 views

Bidirectional iterative deepening search algorithm in Java

Introduction I have this iterative deepening search algorithm. The main "research" attempt was to find out a bidirectional version of that search, and it turned out to be superior compared to two ...
2
votes
2answers
48 views

Simple array operation using a Queue

Question We are given an array (indexed from 1) of N elements on which we make M queries: add(Left, Right, X) - all the elements between the position Left and Right (1<=Left<=Right<=N) are ...