Programming Articles

Page 290 of 2547

Program to find all substrings whose anagrams are present in a string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 568 Views

In this problem, we need to find all substrings in a string where there exists another substring at a different location that is an anagram of the first substring. An anagram contains the same characters but in different order. For example, if the input is s = "abcba", we need to find substrings like "a" (appears at positions 0 and 4), "ab" and "ba" (anagrams of each other), etc. Algorithm To solve this problem, we follow these steps − Generate all possible substrings of each length Group substrings by their sorted character representation (anagram signature) ...

Read More

Program to find smallest index for which array element is also same as index in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 217 Views

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. Algorithm To solve this, we will follow these steps − ret := ...

Read More

Program to find first fit room from a list of rooms in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 293 Views

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 35 is the first room that can accommodate the target size of 30. Algorithm To solve this, we will follow these steps − for each ...

Read More

Program to check whether elements frequencies are even or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 162 Views

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 numbers have occurred twice. Algorithm To solve this, we will follow these steps ? If size of nums is odd, then return False Sort the list nums For i in range 1 ...

Read More

Program to find multiple of n with only two digits in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 299 Views

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. Algorithm To solve this, we will follow these steps − m := 9 x := 1 while m is not divisible by n, do x := x + 1 m := replace all 1s with 9s in the binary form of x return ...

Read More

Program to sort given set of Cartesian points based on polar angles in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 1K+ Views

Suppose we have a set of Cartesian points in a list. We need to sort them based on their polar angles, which vary between 0 and 2π. If points have the same polar angles, we arrange them by their distance from the origin. For example, if we have points = [(1, 1), (1, -2), (-2, 2), (5, 4), (4, 5), (2, 3), (-3, 4)], the output will be [(5, 4), (1, 1), (4, 5), (2, 3), (-3, 4), (-2, 2), (1, -2)]. ...

Read More

Program to count elements whose next element also in the array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 286 Views

Given an array of numbers, we need to count how many elements x have their next consecutive number (x + 1) also present in the array. This problem involves frequency counting and checking for consecutive elements. Problem Understanding For the input nums = [4, 2, 3, 3, 7, 9], we need to find elements whose next consecutive number exists: Element 2: 2 + 1 = 3 exists in array (count: 1) Element 3: 3 + 1 = 4 exists in array (count: 2, since 3 appears twice) Element 4: 4 + 1 = 5 does not ...

Read More

Program to check all 1s are present one after another or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 207 Views

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. Algorithm To solve this, we will follow these steps − visited := 0 for each x in nums, do ...

Read More

Program to find number of optimal steps needed to reach destination by baby and giant steps in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 467 Views

Suppose we have a list of queries Q, where each query Q[i] contains a triplet [a_i, b_i, 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 query we have to find minimum number of steps needed to reach (d_i, 0) from (0, 0). For example, if the input is Q = [(2, 3, 1), (1, 2, 0), (3, 4, 11)], then the output will be [2, ...

Read More

Program to find removed term from arithmetic sequence in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 296 Views

Suppose we have an array called nums holding n-1 arithmetic sequence terms. One element except the first or last element was removed from the original sequence. We have to find the removed number. So, if the input is like nums = [5, 7, 11, 13], then the output will be 9 because the items follow an arithmetic sequence with common difference 2, and the missing term at index 2 should be 9. Algorithm To solve this, we will follow these steps ? If size of nums is 2, return the average ...

Read More
Showing 2891–2900 of 25,466 articles
« Prev 1 288 289 290 291 292 2547 Next »
Advertisements