Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 122 of 377
Program to find the product of three elements when all are unique in Python
Sometimes we need to find the product of three numbers, but only when all numbers are unique. If any two numbers are equal, they should not contribute to the final product. So, if the input is like x = 5, y = 4, z = 2, then the output will be 40, as all three numbers are distinct so their product is 5 * 4 * 2 = 40. However, if x = 5, y = 5, z = 2, the output would be 2 since only z is unique. Algorithm To solve this, we will follow ...
Read MoreProgram to find final text in an editor by performing typing and backspacing in Python
Suppose we have a string that represents characters typed into an editor, where the symbol "
Read MoreProgram to find number of tasks can be finished with given conditions in Python
Suppose we have a list of tasks and another list of people. The tasks[i] determines the amount of strength required to perform the ith task. And the people[i] determines the amount of strength the ith person has. We need to find the maximum number of tasks that can be finished if one person can perform at most one task. So, if the input is like tasks = [4, 3, 9, 15], people = [10, 5, 3, 2], then the output will be 3. The first person (strength 10) can perform task requiring 9, second person (strength 5) can perform ...
Read MoreProgram to find minimum number of operations required to make one number to another in Python
Given two numbers start and end (where start < end), we need to find the minimum number of operations required to convert start to end using these operations: Increment by 1 Multiply by 2 For example, if start = 5 and end = 11, the output will be 2. We can multiply 5 by 2 to get 10, then add 1 to get 11. Algorithm The key insight is to work backwards from the end number. Instead of thinking "how to reach end from start", we think "how did we reach end from start" ...
Read MoreProgram to find any two numbers in a list that sums up to k in Python
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 not. 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 = 31. Approach To solve this, we will follow these steps − temp_set := a new set ...
Read MoreProgram to find the sum of all digits of given number in Python
Finding the sum of digits of a number is a common programming problem. We need to extract each digit from the number and add them together without converting the number to a string. So, if the input is like num = 512, then the output will be 8, as 8 = 5 + 1 + 2. To solve this, we will follow these steps ? Initialize sum = 0 while num is not equal to 0, do ...
Read MoreProgram to find minimum number of operations required to make one string substring of other in Python
Suppose we have two strings s and t, we have to find the minimum number of operations required to make t a substring of s. In each operation, we can choose any position in s and change the character at that position to any other character. For example, if s = "abbpqr" and t = "bbxy", then the output will be 2. We can take the substring "bbpq" from s and change 'p' to 'x' and 'q' to 'y' to make "bbxy" a substring. Approach The strategy is to check all possible substrings of s that have ...
Read MoreProgram to find nth sequence after following the given string sequence rules in Python
Suppose we have two strings s, t and another positive number n is also given, we have to find the nth term of the sequence A where: A[0] = s A[1] = t A[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" ("b" + "a"), "bba" ("b" + "ba"), "bbaba" ("bba" + "ba")] ...
Read MoreProgram to find the maximum width of a binary tree in Python
The maximum width of a binary tree is the number of nodes that can fit between the leftmost and rightmost nodes at any level. This includes null nodes that could exist in between. Approach We'll use a depth-first search (DFS) approach where each node is assigned a position index. For any node at position pos, its left child gets position 2*pos and right child gets 2*pos+1. At each level, we track the minimum and maximum positions to calculate the width. Algorithm Steps Create a dictionary to store minimum and maximum positions for each depth level ...
Read MoreProgram to check whether one string can be 1-to-1 mapped into another string in Python
Sometimes we need to check whether one string can be mapped to another string with a 1-to-1 character mapping. This means each character in the first string maps to exactly one character in the second string, and vice versa. For example, if we have strings "papa" and "lili", we can create a valid mapping: "p" → "l" and "a" → "i". The character order remains unchanged. Algorithm To solve this problem, we use two dictionaries to track mappings in both directions ? Create two dictionaries: one for mapping characters from string s to t, another ...
Read More