Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Programming Articles - Page 1696 of 3366
848 Views
Suppose we have a list of numbers called nums and we have another number k, we have to check whether any two numbers present in the list add up to k or now. Same elements must not be used twice. And numbers can be negative or 0.So, if the input is like nums = [45, 18, 9, 13, 12], k = 31, then the output will be True, as 18 + 13 = 31To solve this, we will follow these steps −temp_set:= a new setfor each num in nums, doif num is in temp_set, thenreturn Trueadd (k-num) into temp_setreturn FalseLet ... Read More
1K+ Views
Suppose we have a number num, we have to find the sum of its digits. We have to solve it without using strings.So, if the input is like num = 512, then the output will be 8, as 8 = 5 + 1 + 2.tput will be 8, as 8 = 5 + 1 + 2. To solve this, we will follow these steps −sum:= 0while num is not same as 0, dosum := sum + (num mod 10)num:= quotient of num/10return sumLet us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, num): ... Read More
694 Views
Suppose we have two strings s and t, we have to find the minimum amount of operations required for s to make t a substring of s. Now, in each operation, we can choose any position in s and change the character at that position to any other character.So, if the input is like s = "abbpqr", t = "bbxy", then the output will be 2, as we can take the substring "bbpq" and change 'p' to 'x' and 'q' to 'y'.To solve this, we will follow these steps −k := size of t, n := size of sans := ... Read More
322 Views
Suppose we have two strings s, t and another positive number n is also given, we have to find return the nth term of the sequence A where −A[0] = sA[1] = tA[n] = A[n - 1] + A[n - 2] when n is even, otherwise A[n] = A[n - 2] + A[n - 1].As an example, if s = "a" and t = "b", then the sequence A would be − ["a", "b", "ba" ("a" + "b"), "bba" ("b" + "ba"), "bbaba" ("bba" + "ba")]So, if the input is like s = "pk", t = "r", n = 4, ... Read More
193 Views
Suppose we have a binary tree, we have to find the maximum width of any level in the tree. Here the width of a level is the number of nodes that can hold between the leftmost node and the rightmost node.So, if the input is like then the output will be 2To solve this, we will follow these steps−create a map d, to hold minimum and maximum values, minimum is initially infinity and maximum is 0Define a function dfs() . This will take root, pos := 0, depth := 0if root is null, then o returnd[depth, 0] = minimum of d[depth, ... Read More
506 Views
Suppose we have two lowercase strings s, and t we have to check whether we can create one 1-to-1 mapping for each letter in s to another letter (may be the same letter) such that s can be mapped to t. (The ordering of characters will not be changed).So, if the input is like s = "papa", t = "lili", then the output will be True, as we can create this mapping: "p" to "l", "a" -> "i"To solve this, we will follow these steps −s_dict := a new mapt_dict := a new mapfor i in range 0 to minimum ... Read More
1K+ Views
Suppose we have a binary search tree and another input called val, we have to check whether val is present in the tree or not.So, if the input is likeval = 7, then the output will be True, as 7 is present in the tree.To solve this, we will follow these steps−Define a function solve() . This will take root, valif root is null, thenreturn Falseif data of root is same as val, thenreturn Trueif data of root < val, thenreturn solve(left of root, val)return solve(right of root, val)Let us see the following implementation to get better understanding− Live DemoExampleclass TreeNode: ... Read More
435 Views
Suppose we have two strings p and q, and also have a number r, we have to check whether p can be converted to q by shifting some characters clockwise at most r times. So, as an example, "c" can be turned into "e" using 2 clockwise shifts.So, if the input is like p = "abc", q = "ccc", r = 3, then the output will be True, as we can make "a" into "c" by using 2 clockwise shifts and then convert "b" into "c" by using 1 clockwise shift, for a total of 3 shifts.To solve this, we ... Read More
3K+ Views
Suppose we have two strings S, and T, these two are representing an integer, we have to add them and find the result in the same string representation.So, if the input is like "256478921657", "5871257468", then the output will be "262350179125", as 256478921657 + 5871257468 = 262350179125To solve this, we will follow these steps −convert S and T from string to integerret = S + Treturn ret as stringLet us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, a, b): return str(int(a) + int(b)) ob = Solution() print(ob.solve("256478921657", "5871257468"))Input"256478921657", "5871257468"Output262350179125
2K+ Views
Suppose we have a list of numbers; we have to check whether the list is strictly increasing or strictly decreasing.So, if the input is like nums = [10, 12, 23, 34, 55], then the output will be True, as all elements are distinct and each element is larger than the previous one, so this is strictly increasing.To solve this, we will follow these steps −if size of nums