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
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

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.

0
votes
0answers
20 views

Finding disjoint subsets with equal sums

The problem statement: Take a set of numbers as input, find the power set. Find the subsets with equal sum. These subsets should be disjoint. I am supposed to use the brute force technique for ...
2
votes
2answers
189 views

Credit card validity check [on hold]

I want to go more in depth, but this is the 'boilerplate'. Is there any way I can optimize this or use more idiomatic Python code? ...
2
votes
3answers
58 views

Climbing the Leaderboard: HackerranK, Terminated due to timeout

I have implemented my solution and I thought it was pretty efficient and passes most of the cases but seems like it fails some of the test cases when the input size is very large. I basically push ...
0
votes
0answers
39 views

How to check if quotes and parentheses are balanced [on hold]

I have been unsuccessfully trying to adapt this solution "Balanced parentheses" at Code Review SE to be able to check if the quotes and parentheses are balanced. For example this should be unbalanced ...
6
votes
2answers
100 views

C++2a comparison-based topological sort algorithm

Here's my attempt at a C++2a Standard Library–friendly "topological sort" algorithm. There are several areas of interest here: The algorithm is comparison-based and in-place, just like ...
1
vote
1answer
50 views

Count number of islands 2d grid

This is a recursive approach using DFS to counting the number of islands. However, I would like to improve performance, while keeping the code clean concise and readable. Better yet can this be solved ...
3
votes
2answers
55 views

Counting pairs that have a given difference in Java

You will be given an integer k and a list of integers. Count the number of distinct valid pair of integers (a,b) in the list for which a+k=b. For example, the array [1,1,1,2] has two different ...
4
votes
1answer
36 views

Minimum swaps algorithm terminated due to timeout

I have been trying to solve this question. Given an unordered array consisting of consecutive integers [1, 2, 3, …, n], find the minimum number of two-element swaps to sort the array. I was able ...
3
votes
2answers
48 views

A Stack Implementation In C

I've written a very simple linked list based stack implementation and was wondering if this is the standard way to do it in C. ...
1
vote
0answers
19 views
1
vote
0answers
59 views

Code Vita 7: Form the maximum possible date time in the MM/DD HH:MM format using any eight of twelve given single digits

The question is similar to my previous question but is quite different, hence I won't call it a follow up. I have made changes like fixing bugs and replacing bubble sort from review of previous ...
5
votes
2answers
54 views

LinkedList class implementation in Python

I was solving this problem for implementing LinkedList and ListNode classes, and Insert and <...
8
votes
1answer
277 views

Solve 8-tile sliding puzzle

I created a BFS algorithm in order to solve an 8-puzzle, and while it works, it is awfully slow compared to my DFS implementation. My main point of concern is the puzzleExists() function, which ...
12
votes
7answers
2k views

Longest consecutive sequence of ascending, descending, or equal integers

Given an array of integers, find the longest consecutive sequence, where a sequence is defined as being either (strictly) ascending, (strictly) descending, or all-equal. 836926 then has the longest ...
5
votes
1answer
76 views

Shunting-yard algorithm

I implemented the Shunting-yard algorithm in Python, I have experience in C# but I'm looking for comments making this more pythonic. I'm also be interested if these methods would be better as ...
3
votes
0answers
77 views

Travelling salesman problem using genetic algorithm in C++

I have implemented travelling salesman problem using genetic algorithm. Since project is not so small I will give short introduction. GeneticAlgorithmParameters - ...
3
votes
0answers
72 views

Sudoku-lite challenge

You can find the problem here. The example: Column A, B, and C are each 3 and together equal 9: ...
2
votes
2answers
65 views

Forming all multiples of 3 using the given digits

I'm working on this kata from Codewars. The task is: Given a certain number, how many multiples of three could you obtain with its digits? Supose that you have the number 362. The numbers that ...
5
votes
3answers
505 views

Counting distinct triangles that have integer-length sides

Problem: We are interested in triangles that have integer length sides, all of which are between minLength and maxLength, ...
3
votes
3answers
585 views

Find the longest ascending subarray from a given array

I have following task, form a given array, I want the longest ascending array. I will give you an example int[] a = {19,12,13,1,2,3,4,5,14,23,24,25,26,31,32}; ...
0
votes
1answer
40 views

Binary Tree Longest Consecutive Sequence

I written code to solve this problem which I found on leetcode. My solution worked for the vast majority of the test cases run against it but failed on 2 for some reason. The first test case it failed ...
1
vote
0answers
17 views
0
votes
1answer
43 views

Map value to color rgba

I wrote this function to map value to a color I am looking for some general feedback on how I can improve the efficiency of the function. and if there is better way to do it. ...
2
votes
1answer
77 views

Buy and Sell a Stock for maximum profit (given a day and a price)

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Example one: Input: [7,1,5,3,6,4] ...
3
votes
1answer
44 views

Dynamically convert to a type and perform an operation comparing the converted values [closed]

I have a list of values which are parsed in other place and are populated in the List<string>compareValues list. There is also a ...
2
votes
1answer
46 views

Code Vita : Chakravyuha

Problem Statement A Chakravyuha is a wheel-like formation. Pictorially it is depicted as below A Chakravyuha has a very well-defined co-ordinate system. Each point on the co-...
5
votes
2answers
333 views

Finding the best terms to use

I have a real-world problem involving finding the best known term from a string containing sets of terms. We have Term objects that are comparable in that they have ...
3
votes
2answers
117 views

Attempt at checking whether two words are permutations

This is my first algorithm in C++ ever to check if two strings are permutations of each other. Please provide guidance on correctness of algorithm + ways to improve + thoughts on my use of the ...
0
votes
1answer
36 views

Playing cards with ReactJS

I came across problem 54 on projecteuler.net and decided to give it a go using ReactJS. I have had Angular experience prior to this. I wrote a couple of fully tested components. I think that they only ...
1
vote
0answers
31 views

Merge K sorted Arrays (JavaScript using generators)

Question: Merge k sorted Arrays My main concerns about my code are: Performance: is it optimized and is my description of the time and complexity correct. How should I annotate the space and time ...
4
votes
1answer
120 views

Generate Christmas Tree

Problem Statement: The Christmas tree is comprised of the following Parts Stand Each Part is further comprised of Branches. Branches are comprised of Leaves. How the tree appears as a ...
7
votes
2answers
195 views

Better container for tracking number of string elements?

I'm looking for the most simple and efficient way to track the number of string elements in a container. I'm currently using std::vector but I wonder if there's a ...
6
votes
1answer
96 views

Finding intersting path in a graph

There is a question in a contest but it doesn't have any answer. I solve it but I get time limit for most of test case. Is it possible to improve my code or give a better approach? Question There is ...
4
votes
2answers
55 views

Checking if an array can be rotated to equal another in JavaScript

I needed to write a function in JavaScript which checks if any rotation of an array is equal to another array. By rotation, I mean: ...
6
votes
2answers
197 views

Code Vita : Form the maximum possible time in the HH:MM:SS format using any six of nine given single digits

Problem Description The objective is to form the maximum possible time in the HH:MM:SS format using any six of nine given single digits (not necessarily distinct) Given a set of nine ...
3
votes
1answer
72 views

Recursively generating the look-and-say sequence

I have a pattern where every number is counted by the number of repetitions. A new resulting number is formed by adding that repetition to the front of that value. The resulting pattern would look ...
3
votes
3answers
165 views

Find k-distinct partitions which contain at least two perfect squares

Problem Statement: A k-distinct-partition of a number \$n\$ is a set of \$k\$ distinct positive integers that add up to \$n\$. For example, the 3-distinct partitions of 10 are \$1+2+7\$ \$...
4
votes
2answers
81 views

Demonstration of Scale Balancing

Have the function ScaleBalancing(int vector, int vector) read vectors which will contain two elements, the first being the two positive integer weights on a balance scale (left and right sides) ...
-1
votes
0answers
49 views

minimum number and number of moves to make all elements (have circular order) of an array equal [closed]

Given an array and a number k (each element is as a disc has a number from 0 to k-1 written around its circumference,Consecutive numbers are adjacent to each other, i.e. 1 is between 0 and 2, and ...
0
votes
2answers
50 views

Filter array of objects by comparing nested object properties

I recently had a problem whereby I wanted to filter objects in an array by comparing the values of nested properties of each object to see if there were duplicates. I have tried to explain as ...
6
votes
1answer
75 views

Multithreaded testing for counting rooms from a floor plan solution

This is version 3 of Efficiently counting rooms from a floorplan. Version 2 is here Efficiently counting rooms from a floorplan (version 2) It also has a comprehensive test facility that is the ...
5
votes
2answers
179 views

Given a list of coordinates check if 4 points make a line

I was doing some preparation for coding interviews and I came across the following question: Given a list of coordinates "x y" return True if there exists a line ...
0
votes
1answer
72 views

Create a summary report by summing the amount's of accounts with the same name and within the date range specified

I have heard from my friend that it is bad practice to normally loop though the whole database to meet certain criteria. He mentioned something about the proper way being that you index the objects of ...
-1
votes
2answers
64 views

Generating parentheses using DFS

I am solving the "Generating Parentheses Given a Number" challenge. The total number of parentheses combination turns out to be Catalan Series. The following code works but I am looking for comment on ...
7
votes
1answer
101 views

Swiftly counting rooms in a floor plan

Inspired by recent questions about counting the rooms in a floor plan (1, 2, 3), here is my attempt to solve the problem with a Swift program. The problem (from “Counting Rooms” on CSES) is: You ...
10
votes
5answers
2k views

Efficiently counting rooms from a floorplan (version 2)

This is version 2 of Efficiently counting rooms from a floorplan. I had accidentally pasted in the wrong version of the code. Update Final version (version 3) of the code with updated test harness ...
6
votes
1answer
271 views

Efficiently counting rooms from a floorplan

Note this was the wrong version of the code. The updated version is here: Efficiently counting rooms from a floorplan (version 2) My apologies! Update Final version 3 with test harness here: ...
6
votes
1answer
93 views

Moore-Neighbor Tracing Algorithm

I wrote code to extract the contour of a hole, based mostly on the Moore-Neighbor Tracing algorithm. Only a. instead of finding a shape, I'm finding a "hole"; and, b. I'm looking for the (outer) ...
3
votes
0answers
36 views

Optimizing NICK binary thresholding

NICK binary thresholding essentially boils down to a convolution where the kernel at each pixel calculates the sum and squared sum (for the standard deviation). These values are then used in the ...
0
votes
2answers
125 views

This code transforms raw data in a table into nested objects

I have a huge array that I want to group by by category and subcategory. A Book is contained in a shelve. A Shelve is contained in a Library. The output is a list of Library. I cannot change my Book ...