Sum of distances between the two nearest perfect squares to all the nodes of the given linked list
Given a linked list, the task is to find the sum of distances between the two nearest perfect squares for all the nodes of the given linked list.
Examples:
Input: 3 -> 15 -> 7 -> NULL
Output: 15
For 3: closest left perfect square is 1 and closest right 4 i.e. 4-1 = 3
For 15: 16 – 9 = 7
For 7: 9 – 4 = 5
3 + 7 + 5 = 15
Input: 1 -> 5 -> 10 -> 78 -> 23 -> NULL
Output: 38
Approach: Initialise sum = 0 and for every node, if the current node’s value is a perfect square itself then the left and right closest perfect square will be the value itself and distance will be 0. Else, find the left and right closest perfect squares say leftPS and rightPS and update sum = sum + (rightPS – leftPS).
Algorithm:
- create a function with an int return type that takes the head of the linked list as input.
- Set the base condition that is if the head is equal to null then return 0.
- Now initialize an int variable to store the total distance sum initially assigned 0 to it.
- Now create a pointer named “temp” which points to the head of the linked list.
- start a while loop with the conditioning temp not equal to NULL
- Now for each iteration.
- Find the square root of the node data and store the value in a variable sq_root
- It indicates that “temp->data” is not a perfect square if “sq root” is less than “temp->data”. Get the left and right perfect squares of the given “temp->data” and place them, respectively, in the variables “left ps” and “right ps.”
- To “tsum,” add the discrepancy between “right ps” and “left ps.
- Transfer the “temp” pointer to the following node.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
Node( int data)
{
this ->data = data;
this ->next = NULL;
}
};
int distanceSum(Node* head)
{
if (head == NULL)
return 0;
int tsum = 0;
Node* temp = head;
while (temp != NULL) {
double sq_root = sqrt (temp->data);
if (sq_root < temp->data) {
int left_ps = ( int ) floor (sq_root)
* ( int ) floor (sq_root);
int right_ps = ( int ) ceil (sq_root)
* ( int ) ceil (sq_root);
tsum += right_ps - left_ps;
}
temp = temp->next;
}
return tsum;
}
int main()
{
Node* head = new Node(3);
head->next = new Node(15);
head->next->next = new Node(7);
head->next->next->next = new Node(40);
head->next->next->next->next = new Node(42);
int result = distanceSum(head);
cout << result << endl;
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
Node( int data)
{
this .data = data;
this .next = null ;
}
}
static int distanceSum(Node head)
{
if (head == null )
return 0 ;
int tsum = 0 ;
Node temp = head;
while (temp != null ) {
double sq_root = Math.sqrt(temp.data);
if (sq_root < temp.data) {
int left_ps = ( int )Math.floor(sq_root)
* ( int )Math.floor(sq_root);
int right_ps = ( int )Math.ceil(sq_root)
* ( int )Math.ceil(sq_root);
tsum += right_ps - left_ps;
}
temp = temp.next;
}
return tsum;
}
public static void main(String[] args)
{
Node head = new Node( 3 );
head.next = new Node( 15 );
head.next.next = new Node( 7 );
head.next.next.next = new Node( 40 );
head.next.next.next.next = new Node( 42 );
int result = distanceSum(head);
System.out.println(result);
}
}
|
Python3
import sys
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def distanceSum(head):
if not head:
return
tsum = 0
temp = head
while (temp):
sq_root = math.sqrt(temp.data)
if sq_root < temp.data:
left_ps = math.floor(sq_root) * * 2
right_ps = math.ceil(sq_root) * * 2
tsum + = (right_ps - left_ps)
temp = temp. next
return tsum
if __name__ = = '__main__' :
head = Node( 3 )
head. next = Node( 15 )
head. next . next = Node( 7 )
head. next . next . next = Node( 40 )
head. next . next . next . next = Node( 42 )
result = distanceSum(head)
print ( "{}" . format (result))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
static int distanceSum(Node head)
{
if (head == null )
return 0;
int tsum = 0;
Node temp = head;
while (temp != null )
{
double sq_root = Math.Sqrt(temp.data);
if (sq_root < temp.data)
{
int left_ps = ( int )Math.Floor(sq_root)
* ( int )Math.Floor(sq_root);
int right_ps = ( int )Math.Ceiling(sq_root)
* ( int )Math.Ceiling(sq_root);
tsum += right_ps - left_ps;
}
temp = temp.next;
}
return tsum;
}
public static void Main( string [] args)
{
Node head = new Node(3);
head.next = new Node(15);
head.next.next = new Node(7);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(42);
int result = distanceSum(head);
Console.Write(result);
}
}
|
Javascript
<script>
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function distanceSum(head) {
if (head == null ) return 0;
var tsum = 0;
var temp = head;
while (temp != null ) {
var sq_root = Math.sqrt(temp.data);
if (sq_root < temp.data) {
var left_ps =
parseInt(Math.floor(sq_root)) *
parseInt(Math.floor(sq_root));
var right_ps =
parseInt(Math.ceil(sq_root)) *
parseInt(Math.ceil(sq_root));
tsum += right_ps - left_ps;
}
temp = temp.next;
}
return tsum;
}
var head = new Node(3);
head.next = new Node(15);
head.next.next = new Node(7);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(42);
var result = distanceSum(head);
document.write(result);
</script>
|
Time Complexity: O(n*sqrt(maximum element in linked list))
Space Complexity: O(1)
Last Updated :
03 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...