Maximum and Minimum element of a linked list which is divisible by a given number k
Last Updated :
01 Mar, 2023
Given a singly linked list of nodes. Find the smallest and largest elements in linked list divisible by a given number .
Examples:
Input : List = 15 -> 14 -> 13 -> 22 -> 50 , K = 5
Output :
Maximum element in linked list divisible by K: 50
Minimum element in linked list divisible by K: 5
Input : List = 10 -> 14 -> 13 -> 22 -> 100 , K = 10
Output :
Maximum element in linked list divisible by K: 100
Minimum element in linked list divisible by K: 10
Approach:
- The idea is to traverse the linked list to the end and initialize the max and min variable to INT_MIN and INT_MAX respectively.
- After that check a condition that if the max value is less than the current node’s value and divisible by K then current node’s value is assigned to max.
- Similarly, check if the current node’s value is less than min value and divisible by k then the current node’s value is assigned to min. Repeat above two steps until the end of the list is reached.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void findMaxMin( struct Node* head, int k)
{
int min = INT_MAX;
int max = INT_MIN;
while (head != NULL) {
if ((head->data < min) && (head->data % k == 0))
min = head->data;
if ((head->data > max) && (head->data % k == 0))
max = head->data;
head = head->next;
}
cout << "Max Element : " << max << endl;
cout << "Min Element : " << min;
}
void push( struct Node** head, int data)
{
struct Node* newNode = new Node();
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
}
int main()
{
struct Node* head = NULL;
push(&head, 15);
push(&head, 14);
push(&head, 13);
push(&head, 5);
push(&head, 50);
int k = 5;
findMaxMin(head, k);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static void findMaxMin( Node head, int k)
{
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
while (head != null )
{
if ((head.data < min) && (head.data % k == 0 ))
min = head.data;
if ((head.data > max) && (head.data % k == 0 ))
max = head.data;
head = head.next;
}
System.out.println( "Max Element : " + max);
System.out.println( "Min Element : " + min);
}
static Node push( Node head, int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
return head;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 15 );
head = push(head, 14 );
head = push(head, 13 );
head = push(head, 5 );
head = push(head, 50 );
int k = 5 ;
findMaxMin(head, k);
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def findMaxMin(head, k):
min = 32767
max = - 32767
while (head ! = None ) :
if ((head.data < min ) and (head.data % k = = 0 )) :
min = head.data
if ((head.data > max ) and (head.data % k = = 0 )) :
max = head.data
head = head. next
print ( "Max Element : " , max )
print ( "Min Element : " , min )
def push( head, data):
newNode = Node()
newNode.data = data
newNode. next = (head)
(head) = newNode
return head
head = None
head = push(head, 15 )
head = push(head, 14 )
head = push(head, 13 )
head = push(head, 5 )
head = push(head, 50 )
k = 5
findMaxMin(head, k)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static void findMaxMin( Node head, int k)
{
int min = int .MaxValue;
int max = int .MinValue;
while (head != null )
{
if ((head.data < min) && (head.data % k == 0))
min = head.data;
if ((head.data > max) && (head.data % k == 0))
max = head.data;
head = head.next;
}
Console.WriteLine( "Max Element : " + max);
Console.WriteLine( "Min Element : " + min);
}
static Node push( Node head, int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
return head;
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 15);
head = push(head, 14);
head = push(head, 13);
head = push(head, 5);
head = push(head, 50);
int k = 5;
findMaxMin(head, k);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function findMaxMin(head , k) {
var min = Number.MAX_VALUE;
var max = Number.MIN_VALUE;
while (head != null ) {
if ((head.data < min) && (head.data % k == 0))
min = head.data;
if ((head.data > max) && (head.data % k == 0))
max = head.data;
head = head.next;
}
document.write( "Max Element : " + max);
document.write( "<br/>Min Element : " + min);
}
function push(head , data) {
var newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
return head;
}
var head = null ;
head = push(head, 15);
head = push(head, 14);
head = push(head, 13);
head = push(head, 5);
head = push(head, 50);
var k = 5;
findMaxMin(head, k);
</script>
|
OutputMax Element : 50
Min Element : 5
Complexity Analysis:
- Time complexity: O(n)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...