Recaman's Sequence17 Mar 2025 | 4 min read Recaman's Sequence, named after the Colombian mathematician Bernardo Recaman Santos, is a fascinating integer sequence that has captured the imagination of mathematicians and computer scientists alike. It's defined by a simple yet intriguing rule, making it an excellent Java exploration topic. Understanding Recaman's Sequence Recamán's Sequence starts with the first term, which is always 0. Two rules determine the next term: If the next term in the Sequence would be a positive integer not already in the Sequence, take that integer as the next term. If the next term is negative or already in the Sequence, subtract that integer from the current term to get the next term. Let's illustrate this with a few initial terms: Continue this process, always choosing the positive integer if it's not already in the Sequence. The first few terms of Recaman's Sequence look like this: 0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, ... General understanding of the rule Java Implementation of the above approach depicts the computation of the next number using the recursive formula mentioned above Output: ![]() Time Complexity: O(n^2). This is because the code potentially checks all previous terms for each term in the Sequence to ensure the next term is unique. This results in a nested loop structure, where for each of the n terms, it may perform up to n iterations in the worst case. Space Complexity: The space complexity of the code is O(n), where n is the number of terms in the Recamán's Sequence to be generated. Optimized approach for the above principleIn this approach, we will make use of the hashing technique to store the previously computed values so that it reduces the time for computing values again and again Output: ![]() Explanation: In this approach, We define a method called generateRecamansSequence that takes an integer n as its input parameter. This n represents the number of terms we want in Recamán's Sequence. Before diving into the sequence generation, we check if n is less than or equal to 0. If n is not a valid input (negative or zero), we return immediately since there are no terms to generate. Next, we create a HashSet called seen. This HashSet will help us keep track of unique terms in the Sequence. We start with an empty set. We initialize a variable prev to keep track of the previous term and start it at 0 since the first term of Recamán's Sequence is always 0. Now, we enter a loop that iterates from the second term (i = 1) to the n-th term (i < n). We generate the terms one by one in this loop. Inside the loop, we calculate the next term, curr, based on the previous term (prev) and the rules of Recamán's Sequence. We subtract i from prev to calculate curr. However, we must ensure the contract is valid according to the rules. If curr is negative or already exists in our seen HashSet, we adjust it by adding i. This ensures that the term is unique and positive. We then add curr to our seen HashSet to keep track of it and print it as part of the Sequence. Finally, we update the previous variable to be equal to curr. This prepares us for the next iteration, where curr will become the previous term. Time Complexity: O(n) Space Complexity: O(n) |
K-way Merge Sort
Introduction K-way merge sort is a sophisticated sorting algorithm that extends the merge sort approach. The goal of the k-way merge problem is to combine k-sorted arrays into one sorted array that has the same elements. While the traditional merge sort algorithm merges two subarrays at a...
4 min read
Program to Validate an IPv4 Address
An IPv4 address is a numerical identification provided to each device connected to a computer network that communicates using the Internet Protocol version 4 (IPv4). It is a 32-bit binary number split into four octets (8-bit numbers), commonly expressed in human-readable form as a series...
4 min read
Primitive Data Type
Primitive is the most fundamental data type usable in the Programming language. There are eight primitive data types: Boolean, byte, character, short, int, long, float, and double. In a Programming language, these data types serve as the foundation for data manipulation. All basic data types are built-in...
5 min read
Circular Linked Lists Introduction and Application
In this article, we will provide an overview of linked lists. How they function, their attributes and examples of important applications that can benefit from using circular linked lists as the underlying data structure. We will also showcase some Python code samples to demonstrate how circular...
8 min read
Priority Queue using Doubly Linked List
Introduction: A priority queue is a data structure that stores elements with associated priorities and allows for efficient retrieval of the element with the highest (or lowest) priority. While there are various implementations of priority queues, one particularly interesting and flexible approach is to use a doubly...
7 min read
Stack Permutations (Check if an array is stack permutation of other)
In computer science and mathematics, stack permutations are an intriguing idea that are essential to many different algorithms and data structures. A basic data structure that adheres to the Last In, First Out (LIFO) concept is a stack. A stack permutation, as used in permutations, is...
4 min read
Pythagorean Triplet Hashing Problem
Pythagorean Triplet problem is used to find out if there exists a Pythagorean Triplet in a given array consisting of three integers (a, b, and c) which will satisfy a² + b² = c². One of the most common problems is figuring out whether any three...
9 min read
Array Implementation of Queue in c++
Introduction: Queues are fundamental data structures used in computer science for managing data in a FIFO (First In, First Out) manner. They are commonly used in scenarios where tasks need to be executed in the order they were received, such as job scheduling, breadth-first search algorithms, and...
6 min read
Search Query Auto Complete
Search Question Auto Complete, otherwise called auto-propose or look-through ideas, is an element usually found in web search tools and sites that help clients form their pursuit questions. At the point when a client starts composing in the hunt bar, the framework predicts and shows a...
7 min read
When building a Heap, is the structure of the Heap unique
? Introduction Heaps are fundamental data structures utilized in a variety of computer science applications, providing fast solutions to problems like priority queues, sorting, and graph algorithms. As we learn more about heap building, one fascinating issue arises: is a heap's structure unique? In this article, we will...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

