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
1
vote
0answers
24 views

Document distance in Python

I wrote a document distance code. It seems working but I couldnt be sure. (I tried for the small strings and it seems working) Also I am not sure that its fast enough for large texts. ...
-2
votes
0answers
27 views

Numerical Euler method

I made an Euler method in which you can plot different parameters, and see how this affects the method. It works, but I have the sensation that it's horribly coded, and I want to learn a better way to ...
-2
votes
0answers
14 views

code to find first day of given year with respect to Gregorian Calendar [on hold]

I want to know is it safe or accurate if I use following method to find first day of given year, using GCC with Code block ...
2
votes
2answers
58 views

How to make my backtraching sudoku solving algorithm in Python faster?

I wrote a backtracking Sudoku solving algorithm in Python. It solves a 2D array like this (zero means "empty field"): ...
1
vote
0answers
64 views

Custom Knight dialer solution's time and space complexity

I recently solved the Knight Dialer problem but I'm not quite sure if I got the time complexity right; hence I'm requesting the following: 1) Time and space complexity review and explanation on why ...
2
votes
2answers
53 views

LeetCode #1120 Maximum Average Subtree (Python)

This code snippet shows 2 common patterns I see a lot in Python that I don't really like: Arbitrarily-named, narrowly-scoped recursive function Inappropriate data type (in this case, an array ...
-1
votes
0answers
47 views

Solving Kruskal algorithm using Python [on hold]

This is the problem from uva online judge. Visit https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=975 In an episode of the Dick Van Dyke ...
4
votes
1answer
164 views

Compute the square root of a positive integer using binary search

The requirement is to find the square root of a positive integer using binary search and the math property that square root of a number n is between ...
15
votes
3answers
1k views

Radix2 Fast Fourier Transform implemented in C++

Note: If you don't know much about Fourier transform algorithms, a simple review of whether I am doing anything inefficient with C++ in general would be appreciated. I've been working on implementing ...
3
votes
2answers
78 views

Reverse chars of each word in a string

An algorithm to reverse characters of each word in a sentence. What would be your concerns regarding the following solution? Example input: "I love New York" output: "I evol weN kroY" ...
2
votes
1answer
67 views

How can I make my solution to “Climbing the Leaderboard” not time out?

I tried to solve the following challenge and optimize the solution but my code still times out. My code is below. Problem statement Climbing the Leaderbord Alice is playing an arcade game and ...
1
vote
1answer
41 views

Dijkstra's implementation in c++ with STL priority_queue

This is dijkstras shortest path algorithm implementation in c++ using priority_queue STL. Looking for two things: a) Correctness of algorithm itself and b) Any improvement suggestions. ...
1
vote
1answer
52 views

Adding a number to an array of digits

I'm a student in my first semester and in my free time and holidays, I do some java training to keep my mind from rusting...so I downloaded this amazing Digital Algorithms practice app and came across ...
4
votes
0answers
41 views

Maximum profit earned on weighted un-directed tree

I came across this problem while giving a sample test. The problem was that we have given a tree which is undirected. We can start from any node of our choice. Initially we have power "P" and ...
2
votes
2answers
43 views

Hash table implementation in C for a simple table record

I have a requirement to implement this simple table from one of the DVB S2X standards. The table is relatively small that contains no more than 28 rows. I need to use the key for the table as the ...
3
votes
1answer
50 views

A Basic Hash Map in Python

A hash map is a data structure that implements an associative array abstract data type using keys and values and has a hash function to compute an index into an array, from which the desired value can ...
0
votes
0answers
36 views

Kotlin Goal Oriented Action Planning

I've implemented Goal Oriented Action Planning (GOAP) in Kotlin. Goal Oriented Action Planning is an algorithm originally devised by J Orkin for the game F.E.A.R. and performs a state space search ...
3
votes
1answer
66 views

CodeChef's Tree MEX (Minimum Excludant) challenge

This code is a solution for CodeChef's Tree MEX problem: Minimum excludant (or MEX for short) of a collection of integers is the smallest non-negative integer not present in the set. You ...
1
vote
1answer
52 views

Bubble Sort: Time and Space Complexities

Algorithm Bubble sort, also known as sinking sort, is a sorting algorithm that repeatedly steps through a list, compares adjacent pairs and swaps them if they are not in the right order. The pass ...
2
votes
0answers
50 views

Find Single Element in a Sorted Array, from LeetCode

The problem quoted: https://leetcode.com/problems/single-element-in-a-sorted-array/ Given a sorted array consisting of only integers where every element appears exactly twice except for one element ...
4
votes
3answers
174 views

Course Statistics Using Arrays and Files

I wanted to get some feedback here on my solution, because the course I am using is not allowing me to submit any answers. Objectives: Write a Java program that: Create a grades.txt ...
2
votes
0answers
45 views

Trapping Rain Water in Python

This is from Leetcode My solution is based on Python 3. It was faster than 90% of solutions on Leetcode, but used too much memory. Please what makes this memory hungry? How can I make this more ...
3
votes
1answer
60 views

Stereo image matching implementation in Halide

The authors of this paper compute support_points of a 900×700 image in 118 ms. I have implemented their algorithm below in Halide. In my algorithm, the nested for loops over length and width iterate ...
13
votes
3answers
4k views

Python π = 1 + (1/2) + (1/3) + (1/4) - (1/5) + (1/6) + (1/7) + (1/8) + (1/9) - (1/10) …1748 Euler

I wrote this code to show that my reddit post is correct. After the first two terms, the signs are determined as follows: If the denominator is a prime of the form 4m − 1, the sign is positive; if ...
0
votes
0answers
79 views

Digit Sum find number of ways and max people

I applied for a job, and the prospective employer sent me the following HackerRank problem, which cannot be found in the public area. There is a lottery with n ...
5
votes
0answers
87 views

General algorithm to calculate sums of all subsets of a given sequence of numbers

Background A recent question Print sums of all subsets made its way to the Hot Network Questions list. The problem is easy: Print sums of all subsets of a given set Given an array of ...
-3
votes
1answer
75 views

Binary tree algorithm for 2D bin packing

I have re-implemented in C++ an algorithm already in JavaScript available here and there. C++ code works fine, but I have memory management concerns: I allocate memory with such statement as ...
1
vote
0answers
44 views

Algorithm X implementation for Minimum Dominating Set

As as final project for a MOOC I wanted to write a program that finds the exact cover set/minimum dominating set from the given social network data. So I did end up with writing this. I kinda followed ...
11
votes
3answers
366 views

Print sums of all subsets

Print sums of all subsets of a given set Problem summary : Print all subset sums of a given set of integers My approach is to store previous results and use them to calculate new (similar idea as ...
7
votes
3answers
1k views

Determine if a triangle is equilateral, isosceles, or scalene

I've been fiddling around with some easy code challenges and there's one about determining if a triangle is equilateral, isosceles, or scalene. I've come up with a working solution, but I feel this ...
6
votes
1answer
82 views

Shunting-yard Algorithm, implemented based on reference pseudocode

I have a lot of problems making this code look more elegant or to reduce its size. The algorithm is mostly copied from the pseudocode of wikipedia page. The difference is I prepare my string with the <...
1
vote
2answers
50 views

Constant Time implementation of String Uniqueness Algorithm

I've written what I think is a constant time implementation of a uniqueness check. I've seen many algorithms online, which run in linear time, but was wondering whether my conclusion that this runs in ...
2
votes
1answer
48 views

Coding Challenge: Kattis

Short problem description: A sequence of numbers are antiarithemtic if there exists no three numbers a_i, a_j, ...
1
vote
1answer
66 views

HarckerRank 2D Arrays Hourglass challenge

About this 2D Arrays HackerRank problem. The task is to find the maximum sum of a region in a 2D array. Specifically, it is to find the maximum sum of an "hourglass" region, defined as a 3x3 square ...
1
vote
3answers
33 views

`set_includes` (string subsequence-containment) in Python 2

I've implemented C++'s std::includes algorithm in Python, so that I can use it to efficiently implement a Scrabble "can I make this word" function: ...
7
votes
5answers
1k views

Finding all possible pairs of square numbers in an array

I am writing a program that allows me to find all possible pairs of square numbers including duplicates. We can also assume the array elements to be of positive integers only. e.g an array of {5,25,3,...
3
votes
1answer
105 views

Collecting numbers in ascending order

What kind of algorithm wold solve the following problem from https://www.ohjelmointiputka.net/postit/tehtava.php?tunnus=junar ? I have been given a positive integer n. Take the n first positive ...
2
votes
1answer
46 views

Trapezoidal rule for set of data

Here is the question from the book of Mark Newman-Computational Physics Exc 5.1 a) Read in the data and, using the trapezoidal rule, calculate from them the approximate distance traveled by the ...
2
votes
1answer
89 views

Create Node tree from list of paths

I have this list of paths: ...
2
votes
1answer
29 views

Find articulation points/ cut vertices in graph

I would like to get some feedback on my completed code that finds and displays the articulation vertices in an undirected graph. I included a test case within the code, it should (and does) print <...
2
votes
1answer
56 views

Java Perlin noise implementation

I've decided to code Perlin noise generator in Java. It works pretty well, I just want to know If I am doing everything right and if it is a valid implementation! PS : The ...
7
votes
3answers
576 views

Cracking the Coding Interview — 1.5 One Away

I would like to know any suggestions about to improve my code solution and your rating of my approach to the problem, I based my code on the fact that if the strings have different length removing one ...
2
votes
1answer
53 views

Find the number of all specific substrings in a string

I would like to ask a code review regarding a concrete exercise. Let's suppose I have to get the number of all specific substrings in a string. We call something specific if any of these conditions ...
8
votes
1answer
95 views

Geektrust: Solve War problem in a OO way in Python

I solved this programming challenge whose objective is to come up with an OO solution to the problem. The problem statement is quite verbose and the tl;dr version would be: Given an enemy army(...
3
votes
1answer
103 views

Find A Series of Numbers Who when Squared and Summed are equal to a given Square

I am working on a CodeWars titled 'Square into Squares. Protect trees!' it can be found here: https://www.codewars.com/kata/54eb33e5bc1a25440d000891/train/javascript I have a working solution, the ...
0
votes
0answers
105 views

Minimum sum of elements, taken 2 at a time

Update: I had not recalled the question correctly and ended up with the wrong question. I have now updated the question and the solution: The question is: Given an array of integers say [8,4,6,12,10] ...
4
votes
1answer
34 views

Calculate implicit dates

I need help to simplify this code about implicit date calculation. The context: I have sentences with implicit and explicit dates. For instance: "From the 5th to the 7th of September 2017, the ...
2
votes
0answers
35 views

Program to draw ASCII art based on commands

I want to know how to improve the design of Canvas drawing implementation. Canvas Drawing This solution is to implement a console-based canvas drawing application. Problem Statement Description ...
4
votes
2answers
111 views

Determining the maximum value that can be obtained from a loot

A thief finds much more loot than his bag can fit. We have to find the most valuable combination of items assuming that any fraction of a loot item can be put into his bag. For example has a bag ...
1
vote
1answer
169 views

Python: Heapsort

I am beginner in python and I am learing about syntax and other functions in python by writing Algorithm and Data Structure programs. How can this code be improved? ...