Check if N can be represented as sum of positive integers containing digit D at least once
Given a positive integer N and a digit D, the task is to check if N can be represented as a sum of positive integers containing the digit D at least once. If it is possible to represent N in such format, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 24, D = 7
Output: Yes
Explanation: The value 24 can be represented as 17 + 7, both containing the digit 7.Input: N = 27 D = 2
Output: Yes
Approach: Follow the steps to solve the problem:
- Check if the given N contains digit D or not. If found to be true, then print “Yes”.
- Otherwise, iterate until N is greater than 0 and perform the following steps:
- After completing the above steps, if none of the above conditions satisfies, then print “No”.
Below is the implementation of the above approach:
C++
|
Yes
Time Complexity: O(N)
Space Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Original page link
Best Cool Tech Gadgets
Top favorite technology gadgets
0 comments:
Post a Comment