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.

learn more… | top users | synonyms

0
votes
1answer
21 views

Hackerrank “Queues: A Tale of Two Stacks” Javascript Solution

Here is the original problem, and below is my solution. It's asking to implement a queue with 2 stacks, which means I cannot simply use Array.shift. I wonder what can the solution be improved? Is ...
0
votes
1answer
30 views

An algorithm that finds a sequence of numbers to fill a square grid

Given numbers 1,2,3,4,5,6,7,8 I need them to replace the x's so that each side adds up to the number in the centre. ...
0
votes
1answer
32 views

Compute all possible attendance records with length n, which will be regarded as rewardable

Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after \$\mod 10^9 + 7\$. ...
4
votes
0answers
44 views

Minimum number of moves to solve “game of fifteen”

I read about this game and tried to make my own algorithm to solve this. I asked it on StackOverflow for the appropriate algorithm and logic to solve this problem. With the help of answers over ...
5
votes
2answers
74 views

Non-recursive implementation of MergeSort

I've been trying to internalize some of the basic sorting algorithms recently, and to do so I've been looking at their wikipedia pages to refresh myself with how they work, and then coding my own "...
3
votes
1answer
58 views

Find next bigger number with same combination of digits

Please review my code for a puzzle to find next bigger number with same combination of digits. so if the number is 156432 the next bigger number is 162345 I wrote a program which is as follows, <...
2
votes
0answers
27 views

Algorithm for point distribution of traversal between 2 points on a line in time

Not sure if this is best SO forum to post at, please suggest/move to more appropriate place otherwise. A synopsis of the problem 1st: Given a scenario where we have messages with data that define a ...
1
vote
1answer
30 views

Hackerrank CTCI “Stacks: Balanced Brackets” Javascript Solution

Here is the original problem, and below is my solution. I wonder what can be improved? Or is there a fundamentally better algorithm out there? Thank you! My intuition is telling me that it can be more ...
-1
votes
1answer
34 views

Efficient Hangman algorithm [on hold]

I'm trying to find the most efficient algorithm for solving Hangman. For those who are not familiar, it's a game where people need to guess the correct letters in a word. I've written the following ...
4
votes
1answer
33 views

Prim's Lazy Minimum Spanning Tree Algorithm in Haskell (Project Euler# 107)

I have implemented Lazy version of Prim's Minimum Spanning Tree Algorithm. I want to improve the code structure, follow prevalent conventions and reduce code size. I am solving Project Euler #107. ...
3
votes
2answers
79 views

Check if a string is palindrome or two strings are the opposite of each other

I made a function which checks if a word is the opposite of each other (ex: word and drow) and then realized that with minor changes it could also be used to detect if a word is palindrome. They both ...
4
votes
1answer
56 views

Find Eulerian Tour Algorithm - with multithreading and random permutations

After struggling for a long while on this through the algorithms course on Udacity, I finally understood enough of the concept to develop this implementation. However, it lacks efficiency and finesse. ...
4
votes
1answer
50 views

Repeated comparison of sorted subarrays

This question is from a recently concluded competition on Codechef. You are given an array A of size n, and there are ...
8
votes
2answers
349 views

method to decide if two strings are anagrams

I'm studying with the Cracking the Code Interview book. I'm just looking for some feedback on this method: C++ ...
3
votes
1answer
72 views

Hackerrank “Hash Tables: Ransom Note” Javascript Solution

Here is the original problem, and here's my solution: ...
12
votes
4answers
1k views

Codechef: Prime Number Generator

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t ...
7
votes
2answers
344 views

Computing sum on column + sum on row + sum on each diagonal

I have the following problem: I read from a file the dimension(n) of the matrix(which is n * n) and the matrix. For each element ...
4
votes
3answers
268 views

finding the conflicts in meetings given startTimes and end Times

Code to find conflicts in a list of meetings given as input. Meetings are defined as {startTime, endTime}. I am using a Greedy Algorithm to find the conflicts. Wanted to know if there is a more ...
4
votes
1answer
118 views

Floor the larger number

floor[1*e] + floor[2*e] + floor[3*e] + ... + floor[n*e], where floor[x] is the largest integer that is not greater than x, and e is Euler's number: 2....
3
votes
1answer
30 views

Sorting a singly linked list with natural merge sort in C

This C program implements a so-called natural merge sort that identifies existing runs in the list and exploits them in order to sort in sub-linearithmic time whenever possible. The running time of ...
12
votes
6answers
1k views

Check string has unique chars only

Following is my code to check if string has unique chars. I am assuming string would have only ascii chars. ...
3
votes
2answers
79 views

Sum of Subset of an Array equals a given number

The exact question that I am solving is given here: Codechef - MARCHA1 Problem Statement Basically, we have an array of n integers, say {1, 5, 6, 3, 12} where n = 5. Then we have a given number ...
8
votes
5answers
479 views

Brick Wall Leetcode challenge

I solved this problem on Leetcode There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to ...
6
votes
1answer
69 views

A* Search on 'Map of Romania' (Russel and Norvig ch3) in Python 3

I've implemented A* search using Python 3 in order to find the shortest path from 'Arad' to 'Bucharest'. The graph is the map of Romania as found in chapter 3 of the book: "Artificial Intelligence: A ...
1
vote
1answer
51 views

Count the number of ways an integer can be represented as a sum of consecutive positive integers

I am trying to count the number of ways an integer can be represented as sum of 2 or more consecutive positive integers. My Code is working in under 1 second for small inputs (\$\le 10^7\$) but after ...
4
votes
0answers
26 views

Recur-ratively resolve dependencies and summarize all results

I've been tasked with resolving all dependencies to a project. Because it couldn't be simple (it never is, right?), the requirements include a few different dependency management tools. For a first ...
1
vote
2answers
62 views

Java Left Rotate Array upto n times

Java problem to left rotate an array upto n times. It would be nice if anyone can review this code. Task: A left rotation operation on an array of size shifts each of the array's elements unit to ...
1
vote
1answer
76 views

Hackerrank “Strings: Making Anagrams” Javascript Solution

This is the original problem : Input Format The first line contains a single string, a. The second line contains a single string, b. Constraints 1<= |a|,|b| <= 10^4 It ...
3
votes
1answer
48 views

Karatsuba Algorithm in Python [closed]

I implemented a Karatsuba algorithm in Python but it doesn't give me the exact answer. ...
3
votes
3answers
58 views

java 2d Array HourGlass Problem from hackerrank

This is a java solution to one of hackerrank problem given in below link. https://www.hackerrank.com/challenges/2d-array I know it's not optimized, can anyone help me to refactor and optimize? ...
-5
votes
1answer
45 views

What are the time and space complexity of this algorithm? [closed]

What is the time and space complexity of this algorithm and can it be improved (how)? It generates random 4 digit even number and the adjacent 2 digits must be different. ...
3
votes
0answers
45 views

Finding shortest paths Python 3 [closed]

Given two words, beginWord and endWord, and a wordList of approved words, find the length of the shortest transformation sequence from beginWord to endWord such that: Only one letter can be ...
5
votes
1answer
62 views

Python 3 - Get n greatest elements in dictionary

Given an dictionary with lists as values this piece of code is dedicated to find the n keys with the longest lists as values. It works, but there has to be a more elegant solution. ...
5
votes
1answer
37 views

Shortest path around a set of points

Problem I'm trying to get into Python and get a bit familiar with it, so I found a problem online to train. The problem was to find the shortest path around some points, given a set of nodes which ...
0
votes
0answers
34 views

Combination of decimal and binary numbers using Dynamic Programming

I was solving this problem on HackerRank and managed to make a working implementation. The question in brief can be described as follows - A binary ...
3
votes
1answer
73 views

Fibonacci series - a different way

I am a C++ student (1.5 months into it). Please give feedback to this different way I have thought of for Fibonacci series. If there are any improvements needed, please suggest them also. ...
3
votes
1answer
49 views

Read CSV files and find the same values on the specific columns

I created a simple command line application to read CSV files in a specific folder and find the same values on the specify column which exists on all files. (Here's the complete source code.) Here's ...
4
votes
2answers
90 views

Finding the second-largest value in an array

The problem is stated as: You are given as input an unsorted array of n distinct numbers, where n is a power of 2. Give an algorithm that identifies the second-largest number in the array and ...
5
votes
3answers
102 views

Finding consecutive numbers in a vector

I need to write a program to print the line numbers, in ascending order, of a particular word in a file. Example output of the program would be: ...
3
votes
3answers
46 views

Get closest Entity from the Player on a 2D battlefield

I am building a little game in using Unity, and recently made an algorithm to get the closest Entity to the player on the battlefield. I made a working sample in ...
9
votes
2answers
89 views

Iterative n choose k combination generator

I wrote this as an attempt to create a non-recursive solution to project euler 60 which is basically finding a specific n choose k combination. The difference is that partial combinations can be ...
4
votes
1answer
70 views

Make each element equal to its neighbouring element which is maximum in a matrix

I have a matrix and I want to make each of its element equal to the maximum of its neighbouring element and each time I increase number of counts till all the elements in the matrix becomes equal to ...
6
votes
2answers
152 views

Faster way to loop through array of points and find if within polygons

I have a Node app which allows users to plot 'events' on dot plot graphs. An event is represented by an array of floats, e.g 1 event may look like this. ...
-1
votes
0answers
48 views

Operator Precedence in AST

I need an algorithm that will turn an AST (abstract syntax tree) that was parsed without any respect to operator precedence into a tree that does respect operator precedence. I came up with one and ...
1
vote
2answers
95 views

Generic numerical systems conversion

This program is a start point to a more comprehensive program that deals with different numerical systems from binary until 35 base. That includes the traditional decimal, octal and hex. It is until ...
7
votes
3answers
248 views

Faster solution to compute magic numbers

There are N doors with a number on each door. You have a golden key(K) and you can open with it only doors on which the numbers ...
1
vote
1answer
40 views

Asynchronous task on every element of an array

Here is some synchronous code: ...
2
votes
1answer
63 views

Format capital letters at the start of a sentence

I have a set of predefined templates for messages, where you can insert different arguments to make it more specific. In order for the messages to be looking good I needed a way to capitalize the ...
0
votes
1answer
73 views

HashCompactor() keyword-value pair manager will become .hash()

As far as I know there is no standard method yet of maintaining keyword-value pairs. I'm certain most implementations would come to a screeching halt given my number crunching requirements. The ...