Open In App

Replace every node in Linked list with its closest catalan number

Improve
Improve
Like Article
Like
Save
Share
Report

Given a singly linked list, the task is to replace every node with its closest Catalan number.

Note: Catalan numbers are defined as mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations. The first few Catalan numbers are: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862.

Examples:

Input: 2 -> 7 -> 11 -> 5 -> 39 -> NULL
Output: 2 -> 5 -> 14 -> 5 -> 42 -> NULL
Explanation: The closest Catalan numbers for each node are:

  • Node 1 (value = 2): Closest Catalan number = 2.
  • Node 2 (value = 7): Closest Catalan number = 5.
  • Node 3 (value = 11): Closest Catalan number = 14.
  • Node 4 (value = 5): Closest Catalan number = 5.
  • Node 4 (value = 39): Closest Catalan number = 42.

Input: 23 -> 11 -> 97 -> 500 -> NULL
Output: 14 -> 14 -> 132 -> 429 -> NULL
Explanation: The closest Catalan numbers for each node are:

  • Node 1 (value = 23): Closest Catalan number = 14.
  • Node 2 (value = 11): Closest Catalan number = 14.
  • Node 3 (value = 97): Closest Catalan number = 132.
  • Node 4 (value = 500): Closest Catalan number = 429.

Approach: This can be solved with the following idea:

This approach first calculates a list of Catalan numbers and then iterates over the linked list. For each node in the linked list, it finds the closest Catalan number by comparing the absolute difference between the node’s value and each Catalan number. The closest Catalan number is the one with the smallest absolute difference. Finally, it replaces the node’s value with the closest Catalan number.

Steps of the above approach:

  • Define a function named calculate_catalan_numbers that takes a vector Catalan as input and fills it with Catalan numbers. 
  • First push 1 and 1 to the vector Catalan. Then, using a loop, it calculates the next Catalan number by summing the product of two corresponding elements from the beginning and end of the vector. The loop continues until the next Catalan number is less than or equal to 0.
  • Define a function named find_closest_catalan that takes a number num and the vector Catalan as inputs and returns the closest Catalan number to num in the vector Catalan.
  • In this function, we iterate through the vector Catalan and keep track of the minimum difference between num and the elements in the vector. It returns the Catalan number with the minimum difference.
  • Define a function named replace_with_closest_catalan that takes the head of the linked list and the vector Catalan as inputs. 
  • Inside this function, we iterate through the linked list and replace each node’s data with the closest Catalan number in the vector Catalan. The function calls the find_closest_catalan function to get the closest Catalan number for each node.

Below is the implementation of the above approach:

C++

// C++ code of the above approach:
#include <bits/stdc++.h>
using namespace std;

struct Node {
    int data;
    Node* next;
    Node(int d)
        : data(d), next(NULL)
    {
    }
};

// Function to calculate Catalan numbers
void calculate_catalan_numbers(vector<int>& catalan)
{

    catalan.push_back(1);
    catalan.push_back(1);
    int n = 2;
    while (true) {
        int next_catalan = 0;
        for (int i = 0; i < n; i++) {
            next_catalan += catalan[i] * catalan[n - i - 1];
        }
        catalan.push_back(next_catalan);
        if (next_catalan <= 0) {
            break;
        }
        n++;
    }
}

// Function to find the closest catalan
// number to a given number
int find_closest_catalan(int num,
                         const vector<int>& catalan)
{
    int diff = abs(num - catalan[0]);
    int closest_catalan = catalan[0];
    for (int i = 1; i < catalan.size(); i++) {
        int new_diff = abs(num - catalan[i]);
        if (new_diff < diff) {
            diff = new_diff;
            closest_catalan = catalan[i];
        }
    }
    return closest_catalan;
}

// Function to replace every node with
// the closest Catalan number
void replace_with_closest_catalan(
    Node* head, const vector<int>& catalan)
{
    Node* curr = head;
    while (curr) {
        int closest_catalan
            = find_closest_catalan(curr->data, catalan);
        curr->data = closest_catalan;
        curr = curr->next;
    }
}

// Function to print the linked list
void print_list(Node* head)
{
    Node* curr = head;
    while (curr) {
        cout << curr->data << " -> ";
        curr = curr->next;
    }
    cout << "NULL" << endl;
}

// Driver Code
int main()
{
    vector<int> catalan;
    calculate_catalan_numbers(catalan);

    Node* head = new Node(2);
    head->next = new Node(7);
    head->next->next = new Node(11);
    head->next->next->next = new Node(5);
    head->next->next->next->next = new Node(39);

    // Function call
    replace_with_closest_catalan(head, catalan);

    print_list(head);

    return 0;
}
Output

2 -> 5 -> 14 -> 5 -> 42 -> NULL

Time Complexity: O(n^2)
Auxiliary Space: O(n)


Last Updated : 09 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads