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 N nodes. Find the smallest and largest elements in linked list divisible by a given number K .
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++
// Program to find the smallest and largest
// elements divisible by a given number k
// in singly linked list
#include <bits/stdc++.h>
using namespace std;
/* Linked list node */
struct Node {
int data;
struct Node* next;
};
// Function to print max and min elements of
// linked list divisible by K
void findMaxMin(struct Node* head, int k)
{
// Declare a min variable and initialize
// it with INT_MAX value.
// INT_MAX is integer type and its value
// is 32767 or greater.
int min = INT_MAX;
// Declare a max variable and initialize
// it with INT_MIN value.
// INT_MIN is integer type and its value
// is -32767 or less.
int max = INT_MIN;
// Check loop while head not equal to NULL
while (head != NULL) {
// If head->data is less than min and divisible
// by k then assign value of head->data to min
// otherwise node point to next node.
if ((head->data < min) && (head->data % k == 0))
min = head->data;
// If head->data is greater than max and divisible
// by k then assign value of head->data to max
// otherwise node point to next node.
if ((head->data > max) && (head->data % k == 0))
max = head->data;
head = head->next;
}
// Print max element
cout << "Max Element : " << max << endl;
// print min element
cout << "Min Element : " << min;
}
// Function that pushes the element in the linked list.
void push(struct Node** head, int data)
{
// Allocate dynamic memory for newNode.
struct Node* newNode = new Node();
// Assign the data into newNode.
newNode->data = data;
// newNode->next assign the address of
// head node.
newNode->next = (*head);
// newNode become the headNode.
(*head) = newNode;
}
// Driver Code
int main()
{
// Start with empty list
struct Node* head = NULL;
// Using push() function to construct
// singly linked list
// 50->5->13->14->15
push(&head, 15);
push(&head, 14);
push(&head, 13);
push(&head, 5);
push(&head, 50);
int k = 5;
findMaxMin(head, k);
return 0;
}
Java
// Java program to find the smallest and largest
// elements divisible by a given number k
// in singly linked list
class GFG
{
// Linked list node /
static class Node
{
int data;
Node next;
};
// Function to print max and min elements of
// linked list divisible by K
static void findMaxMin( Node head, int k)
{
// Declare a min variable and initialize
// it with INT_MAX value.
// INT_MAX is integer type and its value
// is 32767 or greater.
int min = Integer.MAX_VALUE;
// Declare a max variable and initialize
// it with INT_MIN value.
// INT_MIN is integer type and its value
// is -32767 or less.
int max = Integer.MIN_VALUE;
// Check loop while head not equal to null
while (head != null)
{
// If head.data is less than min and divisible
// by k then assign value of head.data to min
// otherwise node point to next node.
if ((head.data < min) && (head.data % k == 0))
min = head.data;
// If head.data is greater than max and divisible
// by k then assign value of head.data to max
// otherwise node point to next node.
if ((head.data > max) && (head.data % k == 0))
max = head.data;
head = head.next;
}
// Print max element
System.out.println( "Max Element : " + max);
// print min element
System.out.println( "Min Element : " + min);
}
// Function that push the element in linked list.
static Node push( Node head, int data)
{
// Allocate dynamic memory for newNode.
Node newNode = new Node();
// Assign the data into newNode.
newNode.data = data;
// newNode.next assign the address of
// head node.
newNode.next = (head);
// newNode become the headNode.
(head) = newNode;
return head;
}
// Driver Code
public static void main(String args[])
{
// Start with empty list
Node head = null;
// Using push() function to construct
// singly linked list
// 50.5.13.14.15
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);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to find the smallest and largest
# elements divisible by a given number k
# in singly linked list
# Linked list node /
class Node:
def __init__(self):
self.data = 0
self.next = None
# Function to print max and min elements of
# linked list divisible by K
def findMaxMin(head, k):
# Declare a min variable and initialize
# it with INT_MAX value.
# INT_MAX is integer type and its value
# is 32767 or greater.
min = 32767
# Declare a max variable and initialize
# it with INT_MIN value.
# INT_MIN is integer type and its value
# is -32767 or less.
max = -32767
# Check loop while head not equal to None
while (head != None) :
# If head.data is less than min and divisible
# by k then assign value of head.data to min
# otherwise node point to next node.
if ((head.data < min) and (head.data % k == 0)) :
min = head.data
# If head.data is greater than max and divisible
# by k then assign value of head.data to max
# otherwise node point to next node.
if ((head.data > max) and (head.data % k == 0)) :
max = head.data
head = head.next
# Print max element
print( "Max Element : " , max)
# print min element
print( "Min Element : " , min)
# Function that push the element in linked list.
def push( head, data):
# Allocate dynamic memory for newNode.
newNode = Node()
# Assign the data into newNode.
newNode.data = data
# newNode.next assign the address of
# head node.
newNode.next = (head)
# newNode become the headNode.
(head) = newNode
return head
# Driver Code
# Start with empty list
head = None
# Using push() function to construct
# singly linked list
# 50.5.13.14.15
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)
# This code is contributed by Arnab Kundu
C#
// C# program to find the smallest and largest
// elements divisible by a given number k
// in singly linked list
using System;
class GFG
{
// Linked list node /
public class Node
{
public int data;
public Node next;
};
// Function to print max and min elements of
// linked list divisible by K
static void findMaxMin( Node head, int k)
{
// Declare a min variable and initialize
// it with INT_MAX value.
// INT_MAX is integer type and its value
// is 32767 or greater.
int min = int.MaxValue;
// Declare a max variable and initialize
// it with INT_MIN value.
// INT_MIN is integer type and its value
// is -32767 or less.
int max = int.MinValue;
// Check loop while head not equal to null
while (head != null)
{
// If head.data is less than min and divisible
// by k then assign value of head.data to min
// otherwise node point to next node.
if ((head.data < min) && (head.data % k == 0))
min = head.data;
// If head.data is greater than max and divisible
// by k then assign value of head.data to max
// otherwise node point to next node.
if ((head.data > max) && (head.data % k == 0))
max = head.data;
head = head.next;
}
// Print max element
Console.WriteLine( "Max Element : " + max);
// print min element
Console.WriteLine( "Min Element : " + min);
}
// Function that push the element in linked list.
static Node push( Node head, int data)
{
// Allocate dynamic memory for newNode.
Node newNode = new Node();
// Assign the data into newNode.
newNode.data = data;
// newNode.next assign the address of
// head node.
newNode.next = (head);
// newNode become the headNode.
(head) = newNode;
return head;
}
// Driver Code
public static void Main(String []args)
{
// Start with empty list
Node head = null;
// Using push() function to construct
// singly linked list
// 50.5.13.14.15
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);
}
}
// This code contributed by Rajput-Ji
JavaScript
<script>
// javascript program to find the smallest and largest
// elements divisible by a given number k
// in singly linked list // Linked list node /
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
// Function to print max and min elements of
// linked list divisible by K
function findMaxMin(head , k) {
// Declare a min variable and initialize
// it with INT_MAX value.
// INT_MAX is integer type and its value
// is 32767 or greater.
var min = Number.MAX_VALUE;
// Declare a max variable and initialize
// it with INT_MIN value.
// INT_MIN is integer type and its value
// is -32767 or less.
var max = Number.MIN_VALUE;
// Check loop while head not equal to null
while (head != null) {
// If head.data is less than min and divisible
// by k then assign value of head.data to min
// otherwise node point to next node.
if ((head.data < min) && (head.data % k == 0))
min = head.data;
// If head.data is greater than max and divisible
// by k then assign value of head.data to max
// otherwise node point to next node.
if ((head.data > max) && (head.data % k == 0))
max = head.data;
head = head.next;
}
// Print max element
document.write("Max Element : " + max);
// print min element
document.write("<br/>Min Element : " + min);
}
// Function that push the element in linked list.
function push(head , data) {
// Allocate dynamic memory for newNode.
var newNode = new Node();
// Assign the data into newNode.
newNode.data = data;
// newNode.next assign the address of
// head node.
newNode.next = (head);
// newNode become the headNode.
(head) = newNode;
return head;
}
// Driver Code
// Start with empty list
var head = null;
// Using push() function to construct
// singly linked list
// 50.5.13.14.15
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);
// This code contributed by umadevi9616
</script>
OutputMax Element : 50
Min Element : 5
Complexity Analysis:
- Time complexity: O(n)
- Auxiliary Space: O(1)
Similar Reads
Minimum and Maximum element of an array which is divisible by a given number k Given an array, the task is to find the minimum and maximum elements in the array which are divisible by a given number k. Examples: Input: arr[] = {12, 1235, 45, 67, 1}, k=5 Output: Minimum = 45, Maximum = 1235 Input: arr[] = {10, 1230, 45, 67, 1}, k=10 Output: Minimum = 10, Maximum = 1230 Approach
7 min read
Sum of all nodes in a doubly linked list divisible by a given number K Given a doubly-linked list containing N nodes and given a number K. The task is to find the sum of all such nodes which are divisible by K. Examples: Input: List = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17 K = 3 Output: Sum = 30 Input: List = 5 <=> 3 <=
9 min read
Product of all nodes in a doubly linked list divisible by a given number K Given a doubly-linked list containing N nodes and given a number K. The task is to find the product of all such nodes which are divisible by K. Examples: Input : List = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17 K = 3 Output : Product = 810 Input : List = 5 <=
9 min read
Minimize the maximum element in constructed Array with sum divisible by K Given two integers N and K, the task is to find the smallest value for maximum element of an array of size N consisting of positive integers whose sum of elements is divisible by K. Examples: Input: N = 4, K = 3Output: 2Explanation: Let the array be [2, 2, 1, 1]. Here, sum of elements of this array
4 min read
Minimum and Maximum Prime Numbers of a Singly Linked List Given a singly linked list containing N nodes, the task is to find the minimum and maximum prime number. Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Minimum : 7 Maximum : 17 Input : List = 15 -> 3 -> 4 -> 2 -> 9 Output : Minimum : 2 Maximum : 3 Approach: The id
9 min read