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
Articles by Arnab Chakraborty
Page 304 of 377
Program to check n can be represented as sum of k primes or not in Python
Suppose we have two inputs n and k. We have to check whether n can be represented as a sum of k number of prime values or not.So, if the input is like n = 30 k = 3, then the output will be True because 30 can be represented like 2 + 11 + 17.To solve this, we will follow these steps −if n < k*2, then return Falseif k > 2, then return Trueif k is same as 2, thenif n is even, then return Trueif (n-2) is prime, then return Truereturn Falseif n is prime, then return ...
Read MoreProgram to find all substrings whose anagrams are present in a string in Python
Suppose we have a string s with lowercase letters. We have to find all in s where there must be another substring in s at a different location that is an anagram of that taken substrings. We have to find a list of substrings in in lexicographic order.So, if the input is like s = "abcba", then the output will be ['a', 'a', 'ab', 'abc', 'abcb', 'b', 'b', 'ba', 'bc', 'bcba', 'cb', 'cba'] for each of them we can find different anagrams present in the string itself.To solve this, we will follow these steps −res := a new listL := ...
Read MoreProgram to find smallest index for which array element is also same as index in Python
Suppose we have a list of elements called nums where all items are unique, and they are sorted in ascending order, we have to find the minimum i such that nums[i] = i. If we cannot find any solution, then return -1. We have to solve this problem in O(log(n)) time.So, if the input is like nums = [-4, -1, 2, 3, 8], then the output will be 2, because both nums[2] = 2 and nums[3] = 3 but 2 is smaller.To solve this, we will follow these steps −ret := -1, lhs := 0, rhs := size of nums ...
Read MoreProgram to find first fit room from a list of rooms in Python
Suppose we have a list of numbers called rooms and another target value t. We have to find the first value in rooms whose value is at least t. If there is no such room, return -1.So, if the input is like rooms = [20, 15, 35, 55, 30] t = 30, then the output will be 35. Because 30 is smaller than 35 and previous rooms are not sufficient for target 30.To solve this, we will follow these steps −for each room in rooms, doif room >= t, thenreturn roomreturn -1ExampleLet us see the following implementation to get better ...
Read MoreProgram to find multiple of n with only two digits in Python
Suppose we have a number n. We have to find the least positive value x such that x is made up of only two digits 9's and 0's, and x is multiple of n.So, if the input is like n = 26, then the output will be 90090.To solve this, we will follow these steps −m := 9x := 1while m is not divisible by n, dox := x + 1m := replace all 1s with 9s in the binary form of xreturn m as integerExampleLet us see the following implementation to get better understanding −def solve(n): m = ...
Read MoreProgram to check whether elements frequencies are even or not in Python
Suppose we have a list of elements called nums, we have to check whether all numbers appear even times or not. We have to solve it using constant space.So, if the input is like nums = [8, 9, 9, 8, 5, 5], then the output will be True, because all number have occurred twice.To solve this, we will follow these steps −if size of nums is odd, thenreturn Falsesort the list numsfor i in range 1 to size of nums, doif nums[i] is same as nums[i - 1], thennums[i] := 0, nums[i - 1] := 0return true when sum of ...
Read MoreProgram to sort given set of Cartesian points based on polar angles in Python
Suppose we have a set of Cartesian points in a list called points. We have to sort them based on their polar angles. The polar angles vary in range 0 and 2*PI. If some points have same polar angles, then arrange them based on the distance of that point from the origin.So, if the input is like points = [(1, 1), (1, -2), (-2, 2), (5, 4), (4, 5), (2, 3), (-3, 4)], then the output will be [(5, 4), (1, 1), (4, 5), (2, 3), (-3, 4), (-2, 2), (1, -2)]To solve this, we will follow these steps −Define ...
Read MoreProgram to count elements whose next element also in the array in Python
Suppose we have a list of numbers say nums, we have to find the number of elements x in the array, such that x + 1 also exists in the array.So, if the input is like nums = [4, 2, 3, 3, 7, 9], then the output will be 3, because 2+1 = 3 is present, 3+1 = 4 is present and another 3 is present so total 3.To solve this, we will follow these steps −answer := 0c := a list containing frequencies of each elements present in numsdlist := a list from list of all keys of cfor ...
Read MoreProgram to find number of optimal steps needed to reach destination by baby and giant steps in Python
Suppose we have a list of queries Q, where each query Q[i] contains a triplet [a_i, b_i and d_i]. Consider we are at position (0, 0) initially, then in one step we can move from some position (x1, y1) to (x2, y2) where Euclidean distance between these two points is at least a and at most b. Now for each queries we have to find minimum number of steps needed to reach (d_i, 0) from (0, 0).So, if the input is like Q = [(2, 3, 1), (1, 2, 0), (3, 4, 11)], then the output will be [2, 0, ...
Read MoreProgram to check all 1s are present one after another or not in Python
Suppose we have a list of numbers called nums that contains at least one element whose value is 1. We have to check whether all the 1s appear consecutively or not.So, if the input is like nums = [8, 2, 1, 1, 1, 3, 5], then the output will be True.To solve this, we will follow these steps −visited := 0for each x in nums, doif x is same as 1, thenif visited is same as 2, thenreturn Falsevisited := 1otherwise when visited is non-zero, thenvisited := 2return TrueExampleLet us see the following implementation to get better understandingdef solve(nums): ...
Read More