Open In App

Make all even values inside loop equal in given Linked list

Last Updated : 23 Dec, 2023
Comments
Improve
Suggest changes
2 Likes
Like
Report

Given a linked list that contains integers, your task is to find the minimum cost for making all even numbers that are inside the cycle equal. You can perform the below-mentioned operations to achieve this :

  • Increase a node's value by 1, this will cost 1 operation.
  • Decrease a node's value by 1, this will cost 1 operation.

Examples :

Input: 1->2->4->6->2
Output: 6
Explanation: Even numbers inside a loop are 2, 4, 6, and 2. To make them equal, they need to be converted into 3.

  • So, converting 2 -> 3 will cost 1 operation.
  • converting 4->3 will cost 1 operation.
  • converting 6->3 will cost 3 operations.
  • converting 2->3 will cost 1 operation.

Minimum Cost = 1 + 1 + 3 + 1 = 6 operations.

Input: 1->2->3->4->8->12->NULL
Output: 0
Explanation: Since there is no loop in the given linked list, therefore, the cost will be 0.

Approach: To solve this problem, follow the below-mentioned instructions step by step:

  • Start by detecting the loop in the linked list, if yes then proceed further otherwise directly return 0.
  • If there exists a loop then iterate inside the loop track even numbers present inside the loop and store them in a vector.
  • If the size of this vector is more than 1 (this shows that there are more than 1 even numbers so we need to make them equal) then pass this vector to findMinCost function otherwise return 0.
  • In findMinCost, find average of all the elements in array, this average number is the number which every array element should be converted to.
  • Then iterate over the array again and calculate the absolute difference between current element and average value, add this sum of all elements and then return it.

Below Code is the implementation of above approach in C++.

C++
// C++ program for the above mentioned approach
#include <bits/stdc++.h>
using namespace std;
// Link list node
class Node {
public:
    int data;
    Node* next;
};

// Function that checks for loop in linked list,
// returns address of node inside loop if
// present otherwise returns null
Node* detectLoop(Node* head)
{
    Node *slow = head, *fast = head;
    while (slow && fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;

        // if loop detected then
        // returning the node address
        if (slow == fast) {
            return slow;
        }
    }

    // if no loop found then return NULL
    return NULL;
}

// Function for pushing node in linked list
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();

    /* put in the data */
    new_node->data = new_data;

    /* link the old list of the new node */
    new_node->next = (*head_ref);

    /* move the head to point to the new node */
    (*head_ref) = new_node;
}

// Function for calculating minimum cost which
// takes integer vector
int findMinCost(vector<int> arr)
{
    int avg = arr[0];

    // Calculating average
    for (int i = 1; i < arr.size(); i++) {
        avg = (avg + arr[i]) / 2;
    }
    int ans = 0;

    // For calculating sum of absolute
    // difference
    for (int i = 0; i < arr.size(); i++) {
        ans += abs(avg - arr[i]);
    }
    return ans;
}
// Main function which takes head node address
// and returns minimum cost
int minimumCost(Node* head)
{

    // For detecting loop
    Node* temp = detectLoop(head);
    if (!temp)

        // Return 0 if no loop found
        return 0;
    Node* temp2 = temp->next;
    vector<int> arr;
    if (temp->data % 2 == 0)
        arr.push_back(temp->data);
    while (temp2 != temp) {

        // if current data is even
        // then push it into the vector
        if (temp2->data % 2 == 0)
            arr.push_back(temp2->data);
        temp2 = temp2->next;
    }

    // when even numbers are more than 1
    if (arr.size() > 1) {
        return findMinCost(arr);
    }
    else {
        return 0;
    }
}

// Driver code
int main()
{
    // Start with the empty list
    Node* head = NULL;

    push(&head, 20);
    push(&head, 4);
    push(&head, 15);
    push(&head, 10);

    // Create a loop for testing
    head->next->next->next->next = head;

    cout << minimumCost(head);
    return 0;
}
Java Python3 C# JavaScript

Output
19

Time Complexity: O(N)
Auxiliary Space: O(N)


Next Article
Practice Tags :

Similar Reads