

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum and Minimum element of a linked list which is divisible by a given number k in C++
A linked list is a linear data structure in which elements are linked via pointers. Each element or node of a linked list has a data part and link, or we can say pointer to the next element in sequence. The elements can take noncontiguous locations in memory.
We are given a singly linked list in which there is a data part and link to the next element. The other input is a number K. Task is to find the Maximum and Minimum element of a linked list which is divisible by the number K. The linear linked list can only be traversed in one direction. At each node we will check the divisibility of it’s data part with K. If that number is maximum or minimum found so far then we will update the values of MaxD and MinD.
Input
SList : 5-->2-->10-->12-->3-->20-->7, K=5
Output
Maximum element which is divisible by K : 20 Maximum element which is divisible by K : 5
Explanation − Traversing from the Head node, keep on dividing the data part with K and check if its completely divisible, that is remainder comes 0.
Only 5, 10 and 20 are divisible by 5 out of which 5 is minimum and 20 is maximum.
Input
SList : 12-->2-->5-->18-->3-->144-->7, K=4
Output
Maximum element which is divisible by K : 144 Maximum element which is divisible by K : 12
Explanation − Traversing from the Head node, keep on dividing the data part with K and check if its completely divisible, that is remainder comes 0.
Only 12, and 144 are divisible by 4 out of which 12 is minimum and 144 is maximum.
Approach used in the below program is as follows
Create a linked list node. Here we have created a class SLLnode, which has info part and next pointer.
Create a linked list. Here we have created a class SLList with SLLnode object as it’s member. So SLList consists of SLLnodes.
The function addtohead(int) is adding nodes to this list.
To add elements to SLList we are calling addtohead(int) using SLList object named LIST.
Once the SLList is created we call the function Divisible(SLLnode,int) which takes two input parameters head of list and integer K.
Now inside Divisible we take two variables maxD and minD to store Maximum and Minimum element of a linked list which is divisible by a given number K.
maxD is initialized with -1 and minD is initialized 9999. This is supposed as range in which input lies.
Inside for loop we traverse the linked list starting from head. For this variable start is pointing to head.
Compare the info part of each node with maxD and minD and it’s divisibility with K. If current node’s info is divisible and less than minD update minD with current info part.
If current node’s info is divisible by K and greater than maxD, update maxD with current info part.
Print the result obtained in minD and maxD.
Example
#include<iostream.h> #include<process.h> #include<conio.h> class SLLnode{ public: int info; SLLnode *next; SLLnode(int e1,SLLnode *ptr=0){ info=e1; next=ptr; } }; class SLList{ public: SLLnode *head; SLList() { head=0; } void addtohead(int); }; void SLList::addtohead(int el) { head=new SLLnode(el,head); } void Divisible(SLLnode* head, int K){ int minD=9999; int maxD=-1; SLLnode* start=head; for(start;start->next!=NULL;start=start->next){ if ((start->info < minD) && (start->info % K == 0)) minD = start->info; if ((start->info > maxD) && (start->info % K == 0)) maxD = start->info; } cout << "Max Element divisible by K: " << maxD << endl; cout << "Min Element divisible by K: " << minD; } // Driver Code int main(){ clrscr(); // Start with empty list SLList LIST; LIST.addtohead(50); LIST.addtohead(21); LIST.addtohead(32); LIST.addtohead(45); LIST.addtohead(11); LIST.addtohead(23); LIST.addtohead(90); LIST.addtohead(56); int K = 5; Divisible(LIST.head, K); getch(); return 0; }
Output
If we run the above code it will generate the following output −
Max Element divisible by K: 90 Min Element divisible by K: 45
- Related Questions & Answers
- Minimum and Maximum Prime Numbers of a Singly Linked List in C++.
- Rearrangement of a number which is also divisible by it in C++
- Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python
- Maximize the number of sum pairs which are divisible by K in C++
- Find if a number is divisible by every number in a list in C++
- Product of all the elements in an array divisible by a given number K in C++
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Minimum removals in a number to be divisible by 10 power raised to K in C++
- Count the number of elements in an array which are divisible by k in C++
- How to check which element of a list which is a logical list is TRUE in R?
- Program to rotate a linked list by k places in C++
- Move first element to end of a given Linked List in C++
- Move last element to front of a given Linked List in C++
- Check whether product of digits at even places of a number is divisible by K in Python
- Check whether sum of digits at odd places of a number is divisible by K in Python