# algorithm's questions - English 1answer

3.429 algorithm questions.

### 2 Find the intersect area of two overlapping rectangles

This is my solution to find the coordinates of 2 overlapped rectangles implemented in JavaScript. Each rectangle is represented by 2 points, each with 2 (x,y) coordinates. Can this code be improved?...

### 7 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 ...

### 6 Using range minimal query for lowest common ancestor (LCA) — part 2

1 answers, 192 views python algorithm python-2.7 tree
Here is my code to use range minimal query to resolve the LCA problem. I applied comments from here. Any advice on code bugs, performance improvement in terms of algorithm time complexity, code style ...

### 3 Find smallest sum of squares to make a number

Leetcode problem 279 “Perfect Squares” is: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) I wrote the following solution: ...

### 1 Find max in-order difference in array

Given an array A of integers, find the maximum of j - i subjected to the constraint of A[i] <= A[j]. If there is no solution possible, return 0. Example : A : [3 5 4 2] Output : 2 for the pair ...

### 2 Crypt-arithmetic solver (Brute force) in Python

0 answers, 22 views python algorithm
I'm working on a crypt-arithmetic solver to add two or more numbers. I've used brute-force to find all the permutations. I'm looking forward to improve this algorithm. Kindly review for all the ...

### Quine McCluskey Algorithm

0 answers, 51 views java algorithm
This is a program that uses the Quine McCluskey algorithm to simplify user inputted minterms. This is the main ...

### 4 Sudoku-lite challenge

1 answers, 118 views performance algorithm vba excel sudoku
You can find the problem here. The example: Column A, B, and C are each 3 and together equal 9: ...

### 5 Knapsack algorithm in JavaScript - Integer weights and values

1 answers, 963 views javascript algorithm knapsack-problem
My version of Knapsack works only when the weights or values of items are whole numbers. Restrictions You are given an array of objects each of which contains a weight and value. You are also given ...

### Ensuring performance of sketching/streaming algorithm (countSketch)

1 answers, 27 views python algorithm numpy stream scipy
I have implemented what is know as a countSketch in python (page 17: https://arxiv.org/pdf/1411.4357.pdf) but my implementation is currently lacking in performance. The algorithm is to compute the ...

### Finding disjoint subsets with equal sums [on hold]

0 answers, 25 views c algorithm
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 ...

### 1 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 Getting all (forward) permutations of a list of strings

2 answers, 1.537 views python algorithm combinatorics
I have the following concise-ish (and working) approach to getting all the forward permutations of a list of string. So with the list: w = ["Vehicula", "Sem", "Risus", "Tortor"] the results ...

### 2 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 ...

### 6 C++2a comparison-based topological sort algorithm

2 answers, 106 views c++ algorithm sorting generics c++20
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 ...

### 2 Credit card validity check [on hold]

2 answers, 192 views python algorithm python-3.x
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? ...

### 1 HackerRank - Array Manipulation - Follow-up

2 answers, 216 views c++ algorithm programming-challenge
This is a follow-up question for HackerRank - Array Manipulation Problem You are given a list (1-indexed) of size \\$n\\$, initialized with zeroes. You have to perform \\$m\\$ operations on the ...

### 2 HackerRank - Array Manipulation

2 answers, 1.332 views c++ algorithm programming-challenge
This is my implementation for this hacker rank problem. (And a follow-up.) Problem You are given a list(1-indexed) of size n, initialized with zeroes. You have to perform m operations on the ...

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

0 answers, 39 views javascript algorithm
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 ...

### 3 Counting pairs that have a given difference in Java

2 answers, 57 views java algorithm time-limit-exceeded k-sum
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 ...

### 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 ...

### 3 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. ...

### 4 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 ...

### Finding the number of inversions in an Array

1 answers, 51 views algorithm scala
I am doing a college assignment where I have to count the number of inversions in an array/list. An inversion is defined as a tuple where i<j and a[i]>a[j]. ...

### 5 LinkedList class implementation in Python

I was solving this problem for implementing LinkedList and ListNode classes, and Insert and <...

### 1 Own implementation of Euclid sub algorithm in Lisp

0 answers, 19 views algorithm recursion lisp
...

### 12 Longest consecutive sequence of ascending, descending, or equal integers

7 answers, 1.603 views c++ algorithm
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 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 ...

### 8 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 ...

### 3 Travelling salesman problem using genetic algorithm in C++

0 answers, 77 views c++ algorithm c++17 genetic-algorithm
I have implemented travelling salesman problem using genetic algorithm. Since project is not so small I will give short introduction.GeneticAlgorithmParameters - ...

### 2 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 Counting distinct triangles that have integer-length sides

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

### 3 Find the longest ascending subarray from a given array

3 answers, 586 views java algorithm
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};...

### 2 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] ...

### 4 Find the maximum possible summation of differences of consecutive elements

Array A contains the elements, \\$A_1,A_2, \ldots, A_N\\$. And array B contains the elements, \\$B_1,B_2, \ldots, B_N\\$. There is a relationship between \\$A_i\\$ and \\$B_i\\$: any element \\$A_i\\$ ...

### 3 SPOJ GENERAL: sorting by swaps of distance k

I have been trying to solve this simple problem on SPOJ for quite some time now, but I keep on getting TLE (Time limit exceeded) for some reason. Since the problem is in Portuguese, a brief ...

### 2 Generating a string in a particular format by reading data from a map

2 answers, 155 views java performance algorithm guava
I have a processToTaskIdHolder Map which contains processId as the key and taskId as the ...

### 7 Hash-optimization and Wilson-maze generation algorithm

I've finished optimization of Wilson algorithm (maze generation) from "silly and slow" algorithm of choosing unvisited cells: ...

### 1 Intersection of the interval set

Recently I had an interview where it was asked: Given a list of people with their birth and end years find the year with the most number of people alive. I implemented in Java and was trying to find ...

### 1 Two arrays lookup

3 answers, 90 views c# performance algorithm
The task is to return array with values which satisfy the condition: for each max the less or equal numbers count. Example Input: numbers = [1, 2, 1, 4] maxes = [2, 4]. Output: [3, 4] Explanation =...

### 2 Binary Tree max sum conditionally

2 answers, 78 views java performance algorithm tree
Problem : A binary tree is given as an input, each node of binary tree contains one integer value. Find the maximum sum of collection of nodes such that following two conditions are met. if node's ...

### This code transforms raw data in a table into nested objects

2 answers, 125 views c# algorithm
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 ...

### Map value to color rgba

1 answers, 43 views performance php algorithm
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. ...

### Binary Tree Longest Consecutive Sequence

1 answers, 40 views python algorithm tree
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 Dynamic Priority Heap - Kotlin

0 answers, 17 views algorithm kotlin
...

### 3 Greedy preference-matching algorithm

2 answers, 121 views c# algorithm graph
The problem that this is based on is something like the following: A matchmaker is working with six clients: John, Joe, Charlie, Jill, Leslie, and Katie. John is compatible with Jill and Leslie, ...

### Process N lists simultaneously by doing permutations

I'm trying to see if someone can come up with a better solution for this algorithm. I have an input of three lists/queues and I have to group/sort the elements of ...

### 1 Find all pairs in an array that sum to a given number without using HashMap

Find all pairs in an array that sum to a given number without using HashMap. Duplicate pairs are not allowed. Input array cannot be modified. input: ...

### 3 Attempt at checking whether two words are permutations

2 answers, 117 views c++ beginner algorithm strings
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 ...

### 14 Return the largest palindrome from the string

3 answers, 10.538 views c# algorithm palindrome
Here is the question: find the largest palindrome from a string. Ex:ABCBAHELLOHOWRACECARAREYOUILOVEUEVOLIIAMAIDOINGGOODResult: ...