Maximum distance between Peaks in the given Linked List


A linked list is a linear data structure that stores the data in the nodes and each node contains the address of the next node to make the connection. A peak or the peak node is the node that is not present at the first or the last place and has a value strictly greater than both of the neighbors. We have to find the maximum distance between two consecutive peaks as there could be more than one peak available.

Sample Examples

Input

Given Linked list: 1 -> 2 -> 3 -> 2 -> 7 -> 1 -> 6 -> 9 -> 8 -> NULL

Output

2

Explanation: Here we have peaks at the third node (3), 5th node (7), and 8th node (9). The difference between the 3rd and 5th node is 1 while the difference between the 5th and 8th node is 2, so we will return 2.

Input

Given Linked list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL

Output

0

Explanation: Here, all the values of the nodes are in increasing order makes there is no peak here. So, we will return 0.

Approach

We have seen the examples of the given problem above and now we will move to the steps required to implement the code:

  • First, we will create a class to create the node blocks for the linked list and in class, we will define the integer to store the value, a pointer to store the address of the next pointer and a constructor to initialize the nodes.

  • We will create a function that will take the head of the linked list as the parameter and will return the required value as the return value.

  • We will create a temporary pointer pointing at the head node and then using it we will traverse over the linked list using a while loop.

  • We will check if the current node is a peak and if it is a peak then we will calculate the distance of it from the previous peak (if any ) and update the value of the answer and the previous peak and move to the next node.

  • In the main function, we will create the linked list and will call to the function to get and print the answer.

Example

#include <bits/stdc++.h>
using namespace std; 

// creating class for nodes 
class Node{
public:
   int val;
   Node* next;
   // constructor
   Node(int data, Node* next_ptr){
      val = data;
      next = next_ptr;
   }
};
void printLL(Node* head){
   Node* temp = head;
   while(temp != nullptr){
      cout<<temp->val<<" -> ";
      temp = temp->next;
   }
   cout<<"NULL"<<endl;
}
// function to get the required distance 
int findDistance(Node* head){
   Node* temp = head; // temporary variable to traverse over the linked list
   // variables to store current and previous peak 
   int curP = -1, prevP = -1;
   int ans = 0;
   // variable to store the previous element.  we will make it int max as we will compare it to the current element to handle the edge case for the first node we will make it int max 
   int prev = INT_MAX; 
   int cur = 0; // variable to get the node number 
   while(temp->next != nullptr){
      // if the current node is at peak 
      if(temp->val > prev && temp->val > temp->next->val){
         if(prevP == -1){
            prevP = cur;
         }
         curP = cur;
         ans = max(ans, curP-prevP-1);
         prevP = curP;
      }
      prev = temp->val;
      temp = temp->next; 
      cur ++;
   }
   return ans; 
} 
int main(){
   // creating head of the linked list 
   Node* head = new Node(1, NULL);
   // adding data to the linked list 
   head->next = new Node(2, NULL);
   head->next->next = new Node(3, NULL);
   head->next->next->next = new Node(2, NULL);
   head->next->next->next->next = new Node(7, NULL);
   head->next->next->next->next->next = new Node(1, NULL);
   head->next->next->next->next->next->next
      = new Node(6, NULL);
   head->next->next->next->next->next->next->next
      = new Node(9, NULL);
   head->next->next->next->next->next->next->next->next
      = new Node(8, NULL); 
   cout<<"The given linked list is: "<<endl; 
   printLL(head);
   cout<<"The distance between the two peaks of the given linked list is: "<<findDistance(head)<<endl;
}

Output

The given linked list is: 
1 -> 2 -> 3 -> 2 -> 7 -> 1 -> 6 -> 9 -> 8 -> NULL
The distance between the two peaks of the given linked list is: 2

Time and Space Complexity

The time complexity of the above code is O(N), where N is the number of the nodes present in the given linked list. We are traversing over the linked list only once making the linear time complexity.

We are not using any extra space here, which makes the constant space complexity that is O(1).

Conclusion

In this problem, we are given a linked list and we have to find the distance between the two consecutive peaks of the given linked list. Peaks are the nodes that have a value strictly greater than the values of its neighbors and it must not be the first or the last node of the given linked list. We have traversed over the linked list and got the peaks in the O(N) time and O(1) space complexity.

Updated on: 31-Aug-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements