• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

Pro Programming

Professional way of Programming: Learn C, C++, Java, Python, Dot Net, Android the professional way

  • Home
  • C MCQs
  • C/C++ Programs
  • Java Programs
  • C#
  • Python
  • MySQL
  • Topics
    • Arrays
    • Strings
    • Link Lists
    • Trees
    • Shapes
  • Projects
  • Articles
  • Games
You are here: Home / Archives for Maximum and Minimum element of a linked list which is divisible by a given number k

Maximum and Minimum element of a linked list which is divisible by a given number k

Add the given digit to a number stored in a linked list

Leave a Comment


Given a linked list which represents an integer number where every node is a digit if the represented integer. The task is to add a… Read More »

The post Add the given digit to a number stored in a linked list appeared first on GeeksforGeeks.





Source link

Filed Under: c programming Tagged With: •   Dynamic Programming, About Us, Add 1 to a number represented as linked list, Add one to a number represented as linked list | Set 2, Add Two Numbers Represented by Linked Lists | Set 3, Advanced Data Structure, Advanced Topics, Algo ▼, Algorithm Paradigms ►, Algorithms, All Algorithms, All Data Structures, Analysis of Algorithms, Aptitude, Array, Backtracking, Binary Search Tree, Binary Tree, Bit Algorithms, Bitonic point in the given linked list, Branch & Bound, Bubble Sort for Linked List by Swapping nodes, C, Campus Ambassador Program, Campus Geek of the Month, Careers, Check if absolute difference of consecutive nodes is 1 in Linked List, Check whether the sum of element of a node is equal to the given key value, Company Prep, Company-wise, Competitive Programming, Compiler Design, Computer Graphics, Computer Networks, Computer Organization, Computer Organization & Architecture, Contact Us, contribute.geeksforgeeks.org, Convert a given Binary Tree to Circular Doubly Linked List | Set 2, Convert singly linked list into circular linked list, Convert Singly Linked List to XOR Linked List, Core Subjects ►, Correct the Random Pointer in Doubly Linked List, Count duplicates in a given linked list, Courses, Coviam Software Developer Internship Experience, Create a linked list from two linked lists by choosing max element at each position, Create linked list from a given array, Create new linked list from two given linked list with greater element at each node, CS Subjects, CS Subjects ▼, CS Subjectwise ►, CSS, Data Structures, DBMS, Delete nodes which have a greater value on right side using recursion, Delete Nth node from the end of the given linked list, Design Patterns, Difference between Singly linked list and Doubly linked list, Digital Electronics, Divide and Conquer, Divide the given linked list in two lists of size ratio p:q, DS ▼, Engg. Mathematics, Experienced Interviews, Find sum of even and odd nodes in a linked list, Find the balanced node in a Linked List, Find the first duplicate element in the linked list, Find the product of first k nodes of the given Linked List, Finding Median in a Sorted Linked List, Game Theory, GATE ▼, GATE 2019, GATE CS Corner, GATE Notes, GATE Official Papers, GBlog, Geek of the Month, Geek on the Top, Geometric Algorithms, Graph, Graph Algorithms, Greedy Algorithms, Hashing, Heap, Hire with Us , How to begin?, HTML, HTML & XML, ide.geeksforgeeks.org, Insert N elements in a Linked List one after other at middle position, Internship, Internship Interviews, Internships, Interview ▼, Interview Experiences, ISRO CS Exam, Iterative selection sort for linked list, Java, JavaScript, jQuery, Languages, Languages ►, Languages ▼, Large number arithmetic using doubly linked list, Last Minute Notes, Linked List, Linked List Implementation in C#, LinkedList, Machine Learning, Mathematical Algorithms, Matrix, Maximum and Minimum element of a linked list which is divisible by a given number k, Maximum sum of K consecutive nodes in the given Linked List, Merge a linked list into another linked list at alternate positions, Microprocessor, Microsoft, Modify contents of Linked List - Recursive approach, Multiple Choice Quizzes, Number of connected components in a doubly linked list, Operating Systems, Pairwise swap adjacent nodes of a linked list by changing pointers | Set 2, Pattern Searching, PHP, Placement Course, Practice, Practice Company Questions, Print the last k nodes of the linked list in reverse order | Iterative Approaches, Print the last k nodes of the linked list in reverse order | Recursive approach, Privacy Policy, Product of all nodes in a doubly linked list divisible by a given number K, Program Output, Program to implement Run Length Encoding using Linked Lists, Program to reverse a linked list using Stack, Program to unfold a folded linked list, Project, Puzzles, Python, Queue, Quizzes ▼, Randomized Algorithms, Recommended: Please try your approach on, Replace each node with its Surpasser Count in Linked List, Reverse a Linked List in groups of given size (Iterative Approach), Reverse alternate K nodes in a Singly Linked List - Iterative Solution, Run Length Decoding in Linked List, Scala, School Programming, Searching Algorithms, Skip to content, Software Engineering, Some rights reserved, Sorting Algorithms, SQL, Stack, Strings, Students ▼, Suggest an Article, Sum of all distinct nodes in a linked list, Sum of all nodes in a doubly linked list divisible by a given number K, Sum of all odd frequency nodes of the Linked List, Sum of nodes in a linked list which are greater than next node, Sum of smaller elements of nodes in a linked list, Testimonials, Theory of Computation, this, Top Topics, Topic-wise, Topicwise ►, Tree based DS ►, UGC NET CS Paper II, UGC NET CS Paper III, UGC NET Papers, Video Tutorials, Videos, Web Technology, What’s Difference?, Write a function that counts the number of times a given int occurs in a Linked List, Write an Article, Write Interview Experience, XOR Linked List - A Memory Efficient Doubly Linked List | Set 1, XOR Linked List – A Memory Efficient Doubly Linked List | Set 2

Minimum removals in a number to be divisible by 10 power raised to K

Leave a Comment


Given two positive integers N and K. Find the minimum number of digits that can be removed from the number N such that after removals the number is divisible by 10K or print -1 if it is impossible.

Examples:

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
 

<strong>Input :</strong> N = 10904025, K = 2&#13;
<strong>Output :</strong> 3&#13;
<strong>Explanation :</strong> We can remove the digits 4, 2 and 5 such that the number &#13;
becomes 10900 which is divisible by 10<sup>2</sup>.&#13;
&#13;
<strong>Input :</strong> N = 1000, K = 5&#13;
<strong>Output :</strong> 3&#13;
<strong>Explanation :</strong> We can remove the digits 1 and any two zeroes such that the&#13;
number becomes 0 which is divisible by 10<sup>5</sup><strong>Input :</strong> N = 23985, K = 2&#13;
<strong>Output :</strong> -1&#13;
 
 

Approach : The idea is to start traversing the number from the last digit while keeping a counter. If the current digit is not zero, increment the counter variable, otherwise decrement variable K. When K becomes zero, return counter as answer. After traversing the whole number, check if the current value of K is zero or not. If it is zero, return counter as answer, otherwise return answer as number of digits in N – 1, since we need to reduce the whole number to a single zero which is divisible by any number. Also, if the given number does not contain any zero, return -1 as answer.

Below is the implementation of above approach.

#include <bits/stdc++.h>

using namespace std;

int countDigitsToBeRemoved(int N, int K)

{

string s = to_string(N);

int res = 0;

int f_zero = 0;

for (int i = s.size() - 1; i >= 0; i--) {

if (K == 0)

return res;

if (s[i] == '0') {

f_zero = 1;

K--;

}

else

res++;

}

if (!K)

return res;

else if (f_zero)

return s.size() - 1;

return -1;

}

int main()

{

int N = 10904025, K = 2;

cout << countDigitsToBeRemoved(N, K) << endl;

N = 1000, K = 5;

cout << countDigitsToBeRemoved(N, K) << endl;

N = 23985, K = 2;

cout << countDigitsToBeRemoved(N, K) << endl;

return 0;

}

Time Complexity :Number of digits in the given number.




If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.

Article Tags :


thumb_up
Be the First to upvote.

Please write to us at [email protected] to report any issue with the above content.


Post navigation


Previous

first_page Subset Sum Queries in a Range using Bitset







Source link

Filed Under: c programming Tagged With: •   Dynamic Programming, About Us, Advanced Data Structure, Advanced Topics, Algo ▼, Algorithm Paradigms ►, Algorithms, All Algorithms, All Data Structures, Analysis of Algorithms, Aptitude, Array, Backtracking, Berkeley's Algorithm, Binary Search Tree, Binary Tree, Bit Algorithms, Branch & Bound, C, Campus Ambassador Program, Careers, Check if a number can be expressed as x^y (x raised to power y), Check if given number is a power of d where d is a power of 2, Check if item can be measured using a scale and some weights, Company Prep, Company-wise, Competitive Programming, Compiler Design, Computer Graphics, Computer Networks, Computer Organization, Computer Organization & Architecture, Construct a binary string following the given constraints, Contact Us, Contests, contribute.geeksforgeeks.org, contributed articles, Core Subjects ►, Count all possible position that can be reached by Modified Knight, Count unique subsequences of length K, Count valid pairs in the array satisfying given conditions, Courses, Cristian's Algorithm, CS Subjects, CS Subjects ▼, CS Subjectwise ►, CSS, Cyclomatic Complexity, Data Structures, DBMS, Design Patterns, Difference between Algorithm, Difference between Deterministic and Non-deterministic Algorithms, Digital Electronics, Dijkstra's shortest path with minimum edges, Divide and Conquer, divisibility, DS ▼, Egg Dropping Puzzle with 2 Eggs and K Floors, Eggs dropping puzzle (Binomial Coefficient and Binary Search Solution), Engg. Mathematics, Experienced Interviews, Expressing a fraction as a natural number under modulo 'm', Find alphabet in a Matrix which has maximum number of stars around it, Find i’th index character in a binary string obtained after n iterations | Set 2, Find maximum in a stack in O(1) time and O(1) extra space, Find multiple of x closest to or a ^ b (a raised to power b), Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B, Find the number of distinct islands in a 2D matrix, Find unit digit of x raised to power y, Find value of y mod (2 raised to power x), Game Theory, GATE ▼, GATE 2019, GATE CS Corner, GATE Notes, GATE Official Papers, GBlog, GCD of a number raised to some power and another number, Geek of the Month, Geek on the Top, Geometric Algorithms, Graph, Graph Algorithms, Greedy Algorithms, Hashing, Heap, How can one become good at Data structures and Algorithms easily?, HTML, HTML & XML, ide.geeksforgeeks.org, Internship, Internship Interviews, Internships, Interview ▼, Interview Experiences, ISRO CS Exam, Java, JavaScript, K-th digit in 'a' raised to power 'b', Languages, Languages ►, Languages ▼, Larger of a^b or b^a (a raised to power b or b raised to power a), Largest Divisor of a Number not divisible by a perfect square, Largest factor of a given number which is a perfect square, Largest number in an array that is not a perfect cube, Largest perfect square number in an Array, Last Minute Notes, Length of largest sub-array having primes strictly greater than non-primes, LinkedList, Lower and Upper Bound Theory, Machine Learning, Mathematical, Mathematical Algorithms, maths-power, Matrix, Maximize the total profit of all the persons, Maximum and Minimum element of a linked list which is divisible by a given number k, Maximum possible time that can be formed from four digits, Microprocessor, Minimum and Maximum element of an array which is divisible by a given number k, Minimum cost to reach the top of the floor by climbing stairs, Minimum number of changes required to make the given array an AP, Minimum number of items to be delivered, Minimum number of moves to make all elements equal, Minimum number of power terms with sum equal to n, Minimum odd cost path in a matrix, Minimum removals from array to make GCD greater, Minimum splits in a binary string such that every substring is a power of 4 or 6., Minimum steps to reach target by a Knight | Set 2, Multiple Choice Quizzes, Number of balanced bracket expressions that can be formed from a string, Number of digits in 2 raised to power n, Number of special pairs possible from the given two numbers, number-digits, Operating Systems, Optimal sequence for AVL tree insertion (without any rotations), Pattern Searching, PHP, Placement Course, Practice, Practice Company Questions, Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation), Print last k digits of a^b (a raised to power b), Print the nodes of binary tree as they become the leaf node, Privacy Policy, Program for SSTF disk scheduling algorithm, Program Output, Program to print the Zigzag pattern, Project, Pseudocode and Program, Puzzles, Python, Queries of nCr%p in O(1) time complexity, Queries to check whether a given digit is present in the given Range, Queue, Quick Sort vs Merge Sort, Quizzes ▼, Randomized Algorithms, Range and Update Sum Queries with Factorial, Range Sum Queries and Update with Square Root, Reduce the array to a single element with the given operation, Samsung Semiconductor Institute of Research(SSIR Software) intern/FTE | Set-3, School Programming, Searching Algorithms, Shannon-Fano Algorithm for Data Compression, Skip to content, Smallest Greater (than S) String of length K whose letters are subset of S, Smallest Pair Sum in an array, Software Engineering, Some rights reserved, Sort only non-prime numbers of an array in increasing order, Sorting Algorithms, SQL, Stack, Strings, Students ▼, Subjective Questions, Subset Sum Queries in a Range using Bitset, Sum of similarities of string with all of its suffixes, Sum of the minimum elements in all connected components of an undirected graph, Technical Scripter 2018, Testimonials, Theory of Computation, Top Topics, Topic-wise, Topicwise ►, Tree based DS ►, UGC NET CS Paper II, UGC NET CS Paper III, UGC NET Papers, Video Tutorials, Videos, Web Technology, What’s Difference?, Write an Article, Write Interview Experience

Primary Sidebar

Recent Posts

  • CAT 2019 Preparation Strategy : How to Ace the CAT Exam in 4 Months
  • WWE Network Sees Increase in Subscribers
  • Fermentation Technology Questions and Answers – Health Benefits of Fermented Foods
  • Add the given digit to a number stored in a linked list
  • Internships in Marine Engineering
  • Privacy Policy
  • About
  • Contact US

© 2019 ProProgramming
 Privacy Policy About Contact Us