# Questions tagged [recursion]

Recursion in computer science is a method of problem solving where the solution to a problem depends on solutions to smaller instances of the same problem.

908 questions
Filter by
Sorted by
Tagged with
653 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 ...
43 views

...
2k views

### Recursive implementation of power set in Python

I was doing a recursion problem called power set, and would like to get code review. Here's the problem. ...
365 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 ...
341 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$ \\$...
46 views

### Creating a series of relationship hierarchies from a list of nodes and connections

I have put together some code that creates a tree structure from a large list of nodes (entities) and connections and am looking to speed it up. HierarchyWorker: Converts the list data into the tree ...
868 views

### Sudoku solver using NumPy

I come from the R language and I gradually switch to Python (Numpy and Pandas). I coded this python code for fun to solve a Sudoku grid. Could you tell me if my code is pythonic enough? For example, ...
622 views

### Codewars: N-dimensional Von Neumann Neighborhood in a matrix

Task: For creating this challenge on Codewars I need a very performant function that calculates Von Neumann Neighborhood in a N-dimensional array. This function will be called about 2000 times The ...
213 views

### Finding the closest pair of points divide-and-conquer speed improvement

This is a follow up to a previously asked Finding the closest pair of points divide-and-conquer question . The original code changed significantly based on answers from @AJNeufeld therefore I created ...
201 views

### Counting contiguous subarrays with a negative sum

I'm doing a coding challenge that asks to count the number of contiguous subarrays that have a negative sum: A subarray of an n-element array is an array composed from a contiguous block of the ...
4k views

### Finding the closest pair of points divide-and-conquer

Here is my code for the famous "finding the closest pair of points" problem from CLRS book section 33.4 using a divide-and-conquer approach (this code returns the minimum distance between pair of ...
499 views

### Find the second largest number in an array with at most n + log2 n - 2 comparisons

This exercise is from Roughgarden's (excellent) course on algorithms on Coursera: You are given as input an unsorted array of n distinct numbers, where n is a power of 2. Give an algorithm that ...
100 views

### Possible letter combinations of a dial pad in linear time using a recursive approach

Time complexity: I walk the initial unzipped array of keys representing the number O(N) backwards. I then do an inner walk of the proceeding array of letters *O(L^2), mapping it to every value in the ...
627 views

### Memoized solution to Count number of ways to climb n steps

Problem statement : A Child is climbing up n steps with either 1 , 2, 3 hops ..how many ways can the child climb up the stairs? source: cracking the coding interview book. I have two solutions the ...
2k views

### All subsets of a String in java using recursion

I wrote this code for printing all sets of a String. It also prints the empty subset. Is this the right approach ...
455 views

### Find all N-Queens solution

This code is a multi-threading recursive backtracking DFS algorithm to find all solutions to the N-Queens problem. The board size is indicated by BOARD_SIZE. ...
81 views

### Recursive Generator Exceeds Call Stack

I have this recursive generator that fetches 100 messages from an API and gets the next 100 until there are no more messages to get: ...
255 views

### Hackerrank Day 9: Recursion

Write a factorial function that takes a positive integer, N as a parameter and prints the result of N! (N factorial). Note: If you fail to use recursion or fail to name your recursive function ...
526 views

### Determine all ways a string can be split into valid words, given a dictionary of all words

This function word_break(s, dictionary) receives a string s and a set of all valid words ...
589 views

### Find all root to leaf paths in binary tree

Description: Given a binary tree, return all root-to-leaf paths. Leetcode ...
310 views

### Find minimum depth in a binary tree

Description: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node ...
732 views

### Fast and flexible Sudoku Solver in C++

After seeing a lot of Sudoku solvers on Code Review I decided to implement my own. My implementation aims to be very fast and flexible, supporting some Sudoku variations (irregular boxes, extra ...
1k views

### Generate all combinations of length N through M

I've started learning programming and as matter of practice I've decided to make a program that will output all possible string combinations and put them in a file. I've manged to do that and it works ...
138 views

### Modeling tabular structure in MSSQL to store data like Excel sheet applying Column inheritance

I've created this database structure to store tabular information (it's a simplification of my real structure but enough to show the point). In it I have Sheets and ...
481 views

### Cycle in Directed graph to vector

I created a class of a directed graph and a method for finding if graph has an Euler cycle and updating a vector accordingly.Is there any optimization or a nicer way to do this. ...
1k views

### Bisection to find roots in C++

I have written a short C/C++ code finding root by bisection. (This is a simple iterative numerical method allowing to find the root of an equation i.e. x such that f(x) = 0). Bisection Method The ...
1k views

### Finding all possible paths between two points in a grid using recursion

I'm trying to find all possible paths made of 0 from bottom left to top right, only by moving either up or to the right. The input file contains a grid of characters, where ...
799 views

### Robot on a Grid: Find a path between two corners with forbidden cells on the road

Problem statement The problem is defined in the book as following: 8.2 Robot in a Grid Imagine a robot sitting on the upper left corner of grid with r rows and <...
1k views

### Recursive Quadtree implementation in C++

I've been working on a recursive quadtree in C++. How can I improve my code? My primary goals are improving the performance/speed, as well as practice. QuadTree.h ...
181 views

### Check whether array is sorted recursively

I'm trying to implement isSorted method checking whether given array is sorted recursively. I've written two types, one is similar to merge sort logic, the another ...
182 views

### Binary search for integers

I've written binary search with only my own efforts 💪💪 without glancing at anywhere. I stuck on finding base case. At final, I think that if last checking is less than first index or first checking ...
474 views

### Recursive merge-sort inversion count algorithm very slow

I am trying to implement a recursive count of inversions in a list of integers by using merge sort and counting the number of split inversions on each recursion. My code works but runs painfully ...
122 views

### PHP Iterate over Child's Child Efficiently

I have this code: ...
221 views

### Multiplying big numbers using Karatsuba's method

The Karatsuba algorithm, first published in 1962, aims to speed up the multiplication of big numbers by reducing the number of 'single-digit-multiplications' involved. Because of its complexity (...
476 views

### Number of Paths (BackTracking) in Java

Illustration You're testing a new driverless car that is located at the Southwest (bottom-left) corner of an n×n grid. The car is supposed to get to the opposite, Northeast (top-right), corner ...
1k views

### N Queens in Python

I used Python very occasionally, today I solved n queens in Python just for fun. I am sure there are better way of doing certain things in Python and can't wait for improvement suggestions! ...
87 views

### python magic square finder for arbitrary numbers

I wrote this code to get all possible magic squares that can be made with a list you put in. The only contraints are that the count of numbers is a square and that each number is unique. It even works ...
75 views

### Finding the length of the number of paths in a matrix

I found this problem from leetcode.com. We are asked to find the longest increasing path in a matrix. I finally came up with a recursion to achieve this but I found that when I increase the rows and ...
2k views

### Method to remove outliers from a set of values

I'm removing outliers from a set of data using the method described here. The method removes the outliers and returns the quartile values of the set after all outliers have been removed. (...
873 views

### Tic-Tac-Toe Recursive Solver

I am attempting to create a Tic-Tac-Toe game that uses a recursively checks every possible game board, and finds which boards result in a victory for X. (I ultimately plan to turn this into an AI ...
4k views

### Find the minimum number of coin change

Description: You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that ...
332 views

### Recursive solution for Longest Substring Without Repeating Characters written in Elixir

Here's my recursive implementation of a sliding window algorithm to find the longest substring of a given string without repeating characters. Is there a more idiomatic-Elixir way to write this? <...
142 views

### Merging bin-data via a bin count threshold

When performing a chi-squared test, one takes the square of the differences of the expected counts per bin and observed counts per bin, and divides these per-bin differences by the expected counts per ...
554 views

### Find subsets of size K in N set

The question is: There is a set An, and it consists of integers from 1 to n. ...
355 views

### Recursive Fibonacci in Java

I just started learning recursion and memoization, so I decided to give a simple implementation a shot. Works with some simple test cases I came up with. Is there anything I can add to make this more ...
1k views

### Sum numbers represented as linked lists

Problem Description: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add ...
977 views

### Eight Queens Recursive

I am working on a Java textbook chapter on recursion, and I am to implement an Eight Queens solution using recursion. Though I know that I am to post working code in Code Review, I do believe this ...
122 views

### Finding non-self-intersecting paths of certain moves that touch all points in a grid

The problem I'm trying to solve is the following: You have a 10x10 grid and starting from the upper-left point you have to touch all the points, without passing through the points that have already ...