Find maximum pairwise sum in Linked List that are equidistant from front and back
Last Updated : 09 Mar, 2023
Given a linked list lis of length N, where N is even. The task is to maximize the sum of two equidistant nodes from the front and back ends of the given linked list.
Note: Two nodes (i and j) are equidistant from both ends if the distance of ith node from front is same as distance of jth node from back.
Examples:
Input: lis = {5, 4, 2, 1}
Output: 6
Explanation: The nodes with pairs present in this linked list are:
Node 0 and node 3 are equidistant having a sum of 5 + 1 = 6.
Node 1 and node 2 are equidistant having a sum of 4 + 2 = 6.
Thus, the maximum sum of equidistant nodes of the linked list is max(6, 6) = 6.
Input: lis = {4, 2, 2, 3}
Output: 7
Explanation: The nodes with pairs present in this linked list are:
Node 0 and node 3 are equidistant having a sum of 4 + 3 = 7.
Node 1 and node 2 are equidistant having a sum of 2 + 2 = 4.
Thus, the maximum sum of equidistant nodes of the linked list is max(7, 4) = 7.
Approach: The solution is based on dividing the linked list into two equal halves and then using two pointer approach. Follow the steps mentioned below to solve the problem:
- Get mid and separate the linked list into two parts.
- Reverse the second part, for traversing it in the forward direction.
- Traverse in both parts and get the maximum sum.
- Recover the Linked List Again, by connecting the parts again, for good practice.
Below is the implementation of the above approach.
C++ // C++ code to implement above approach #include <bits/stdc++.h> using namespace std; // Structure of a node struct ListNode { int val; ListNode* next; ListNode() : val(0), next(nullptr) { } ListNode(int x) : val(x), next(nullptr) { } ListNode(int x, ListNode* next) : val(x), next(next) { } }; // Function to add node in linked list void push(struct ListNode** head_ref, int new_data) { // Allocate node struct ListNode* new_node = new ListNode; // Put in the data new_node->val = new_data; // Link the old list of the new node new_node->next = (*head_ref); // Move the head to point the new node (*head_ref) = new_node; } // Function for reversing the linked list void reverse(ListNode** head) { ListNode *curr = *head, *prev = 0, *nxt; while (curr) nxt = curr->next, curr->next = prev, prev = curr, curr = nxt; *head = prev; } // Function to find the maximum sum // of equidistant elements int pairSum(ListNode* head) { // Get mid and separate // the linked list into two parts ListNode *prev = 0, *slow = head, *fast = head; // Find mid while (fast and fast->next) prev = slow, slow = slow->next, fast = fast->next->next; // Separate them prev->next = 0; // Reverse the second part, // for traversing it // in forward direction reverse(&slow); // Traverse in both parts and // get the maximum sum int sum = 0; ListNode *ptr1 = head, *ptr2 = slow; while (ptr1) sum = max(sum, (ptr1->val + ptr2->val)), ptr1 = ptr1->next, ptr2 = ptr2->next; // Recover the Linked List again, by // connection the parts again reverse(&slow); prev->next = slow; // Return sum return sum; } // Driver code int main() { struct ListNode* head = NULL; push(&head, 4); push(&head, 2); push(&head, 2); push(&head, 3); cout << pairSum(head); return 0; }
Java // Java code to implement above approach class GFG{ // Structure of a node static class ListNode { int val; ListNode next; ListNode() { this(0); } ListNode(int x) { this.val = x; this.next = null; } ListNode(int x, ListNode next) { this.val = x; this.next = next; } }; // Function to add node in linked list static ListNode push(ListNode head_ref, int new_data) { // Allocate node ListNode new_node = new ListNode(); // Put in the data new_node.val = new_data; // Link the old list of the new node new_node.next = head_ref; // Move the head to point the new node head_ref = new_node; return head_ref; } // Function for reversing the linked list static ListNode reverse(ListNode head) { ListNode curr = head, prev = new ListNode(), nxt=new ListNode(); while (curr.next!=null) { nxt = curr.next; curr.next = prev; prev = curr; curr = nxt; } head = prev; return head; } // Function to find the maximum sum // of equidistant elements static int pairSum(ListNode head) { // Get mid and separate // the linked list into two parts ListNode prev = new ListNode(), slow = head, fast = head; // Find mid while (fast!=null && fast.next!=null) { prev = slow; slow = slow.next; fast = fast.next.next; } // Separate them prev.next = new ListNode(); // Reverse the second part, // for traversing it // in forward direction slow = reverse(slow); // Traverse in both parts and // get the maximum sum int sum = 0; ListNode ptr1 = head, ptr2 = slow; while (ptr1!=null) { sum = Math.max(sum, (ptr1.val + ptr2.val)); ptr1 = ptr1.next; ptr2 = ptr2.next; } // Recover the Linked List again, by // connection the parts again slow = reverse(slow); prev.next = slow; // Return sum return sum; } // Driver code public static void main(String[] args) { ListNode head = new ListNode(); head = push(head, 4); head = push(head, 2); head = push(head, 2); head = push(head, 3); System.out.print(pairSum(head)); } } // This code is contributed by 29AjayKumar
Python3 # Python code to implement above approach # Structure of a node class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next # Function to add node in linked list def push(head_ref, new_data): # Allocate node new_node = ListNode(new_data) # Link the old list of the new node new_node.next = head_ref # Move the head to point the new node head_ref = new_node return head_ref # Function for reversing the linked list def reverse(head): curr = head prev = None nxt = None while curr.next is not None: nxt = curr.next curr.next = prev prev = curr curr = nxt head = prev return head # Function to find the maximum sum # of equidistant elements def pairSum(head): # Get mid and separate # the linked list into two parts prev = None slow = head fast = head # Find mid while fast is not None and fast.next is not None: prev = slow slow = slow.next fast = fast.next.next # Separate them prev.next = None # Reverse the second part, # for traversing it # in forward direction slow = reverse(slow) # Traverse in both parts and # get the maximum sum sum = 0 ptr1 = head ptr2 = slow while ptr1 is not None: sum = max(sum, (ptr1.val + ptr2.val)) ptr1 = ptr1.next ptr2 = ptr2.next # Recover the Linked List again, by # connection the parts again slow = reverse(slow) prev.next = slow # Return sum return sum head = ListNode() head = push(head, 4) head = push(head, 2) head = push(head, 2) head = push(head, 3) print(pairSum(head)) # This code is contributed by lokesh.
C# // C# code to implement above approach using System; public class GFG{ // Structure of a node class ListNode { public int val; public ListNode next; public ListNode() { new ListNode(0); } public ListNode(int x) { this.val = x; this.next = null; } public ListNode(int x, ListNode next) { this.val = x; this.next = next; } }; // Function to add node in linked list static ListNode push(ListNode head_ref, int new_data) { // Allocate node ListNode new_node = new ListNode(); // Put in the data new_node.val = new_data; // Link the old list of the new node new_node.next = head_ref; // Move the head to point the new node head_ref = new_node; return head_ref; } // Function for reversing the linked list static ListNode reverse(ListNode head) { ListNode curr = head, prev = new ListNode(), nxt=new ListNode(); while (curr.next!=null) { nxt = curr.next; curr.next = prev; prev = curr; curr = nxt; } head = prev; return head; } // Function to find the maximum sum // of equidistant elements static int pairSum(ListNode head) { // Get mid and separate // the linked list into two parts ListNode prev = new ListNode(), slow = head, fast = head; // Find mid while (fast!=null && fast.next!=null) { prev = slow; slow = slow.next; fast = fast.next.next; } // Separate them prev.next = new ListNode(); // Reverse the second part, // for traversing it // in forward direction slow = reverse(slow); // Traverse in both parts and // get the maximum sum int sum = 0; ListNode ptr1 = head, ptr2 = slow; while (ptr1!=null) { sum = Math.Max(sum, (ptr1.val + ptr2.val)); ptr1 = ptr1.next; ptr2 = ptr2.next; } // Recover the Linked List again, by // connection the parts again slow = reverse(slow); prev.next = slow; // Return sum return sum; } // Driver code public static void Main(String[] args) { ListNode head = new ListNode(); head = push(head, 4); head = push(head, 2); head = push(head, 2); head = push(head, 3); Console.Write(pairSum(head)); } } // This code is contributed by shikhasingrajput
JavaScript // Javascript code to implement above approach // Structure of a node class ListNode { constructor(x = null, next = null) { this.val = x; this.next = next; } }; // Function to add node in linked list function push(head_ref, new_data) { // Allocate node let new_node = new ListNode(); // Put in the data new_node.val = new_data; // Link the old list of the new node new_node.next = head_ref; // Move the head to point the new node head_ref = new_node; return head_ref; } // Function for reversing the linked list function reverse(head) { let curr = head, prev = new ListNode(), nxt = new ListNode(); while (curr.next != null) { nxt = curr.next; curr.next = prev; prev = curr; curr = nxt; } head = prev; return head; } // Function to find the maximum sum // of equidistant elements function pairSum(head) { // Get mid and separate // the linked list into two parts let prev = new ListNode(), slow = head, fast = head; // Find mid while (fast != null && fast.next != null) { prev = slow; slow = slow.next; fast = fast.next.next; } // Separate them prev.next = new ListNode(); // Reverse the second part, // for traversing it // in forward direction slow = reverse(slow); // Traverse in both parts and // get the maximum sum let sum = 0; let ptr1 = head, ptr2 = slow; while (ptr1 != null) { sum = Math.max(sum, (ptr1.val + ptr2.val)); ptr1 = ptr1.next; ptr2 = ptr2.next; } // Recover the Linked List again, by // connection the parts again slow = reverse(slow); prev.next = slow; // Return sum return sum; } // Driver code let head = new ListNode(); head = push(head, 4); head = push(head, 2); head = push(head, 2); head = push(head, 3); console.log(pairSum(head)); // This code is contributed by Saurabh Jaiswal
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Pair with given sum and maximum shortest distance from end Given an array of N integers and an integer K, pick two distinct elements whose sum is K and find the maximum shortest distance of the picked elements from the endpoints. Examples: Input : a[] = {2, 4, 3, 2, 1} k = 5. Output : 2 Explanation: Select the pair(4, 1). Shortest distance of 4 from ends =
8 min read
Create a new linked list from maximum square differences Given a linked list with an even number of nodes, the task is to generate a new linked list that contains the maximum difference of squares of node values, arranged in decreasing order. Each node should be included in exactly one pair.Examples:Input: 1 -> 6 -> 4 -> 3 -> 5 ->2Output: 3
11 min read
Find a triplet from three linked lists with sum equal to a given number Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8, and 90->20->59, and the given number is 101, the output should be triple "
14 min read
Find pairs with given sum in doubly linked list Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in a doubly-linked list whose sum is equal to the given value x in sorted order. Examples:Input: Output: (1, 6), (2,5)Explanation: We can see that there are two pairs (1, 6) and (2, 5) with sum 7.Input: Output
14 min read
Find a point whose sum of distances from all given points on a line is K Given a sorted array arr[] consisting of N integers, representing points on a line and an integer K, the task is to find any point P between the first and last point such that the sum of distances of all given points from P is equal to K. If no such point exists, then print "-1". Examples: Input: ar
14 min read
Count pairs from two linked lists whose sum is equal to a given value Given two linked lists(can be sorted or unsorted) of size n1 and n2 of distinct elements. Given a value x. The problem is to count all pairs from both lists whose sum is equal to the given value x. Note: The pair has an element from each linked list. Examples: Input : list1 = 3->1->5->7 lis
15+ min read