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 13 of 377
Program to get final string after shifting characters with given number of positions in Python
Suppose we have a lowercase string s and another list of integers called shifts whose length is same as the length of s. Here each element in shifts[i] indicates it to shift the first i + 1 letters of s by shifts[i] positions. If shifting crosses 'z' it will wrap up to 'a'. We have to find the resulting string after applying shifts to s. So, if the input is like s = "tomato" shifts = [2, 5, 2, 3, 7, 4], then the output will be "qjcoes". After shifting first character 2 places, it will be 't' to ...
Read MoreProgram to count number of horizontal brick pattern can be made from set of bricks in Python
Suppose we have a list of numbers called bricks and two other values width and height. Each element in bricks[i] represents a brick whose length is bricks[i] units and width is 1 unit. We have to find the number of ways to lay the bricks such that we get full layout of bricks with the given width and height. We can reuse the bricks but can only be laid horizontally. So, if the input is like bricks = [2, 1] width = 3 height = 2, then the output will be 9 because − ...
Read MoreProgram to find number of pairs where elements square is within the given range in Python
Given two lists of numbers nums1 and nums2, and two boundary values lower and upper, we need to find the number of pairs (i, j) such that lower ≤ nums1[i]² + nums2[j]² ≤ upper. Problem Understanding For example, if nums1 = [5, 3, 2], nums2 = [8, 12, 6], lower = 10, and upper = 50, we need to check all possible combinations: Pair (1, 2): 3² + 6² = 9 + 36 = 45 (10 ≤ 45 ≤ 50) ✓ Pair (2, 2): 2² + 6² = 4 + 36 = 40 (10 ≤ 40 ...
Read MoreProgram to check whether robot is moving inside a bounded box or not in Python
Suppose we have a string s that represents the moves of a robot. The robot starts at position (0, 0) and faces north. We need to determine if the robot stays within a bounded area after repeatedly executing the move sequence. Move Commands The move string s contains these characters ? "F" − move forward one unit in current direction "L" − rotate 90 degrees left "R" − rotate 90 degrees right Algorithm Logic If a robot returns to its starting position (0, 0) after executing the move sequence up to 4 times, ...
Read MoreProgram to find bitwise AND of range of numbers in given range in Python
Suppose we have two values start and end, we have to find the bitwise AND of all numbers in the range [start, end] (both inclusive). So, if the input is like start = 8 end = 12, then the output will be 8. Here's why: 8 is 1000 in binary and 12 is 1100 in binary, so 1000 AND 1001 AND 1010 AND 1011 AND 1100 is 1000 which is 8. Algorithm To solve this, we will follow these steps − n := end - start + 1 ...
Read MoreProgram to find number of sublists with sum k in a binary list in Python
Given a binary list containing only 0s and 1s, we need to find the number of contiguous sublists whose sum equals a given value k. This problem can be efficiently solved using the prefix sum technique with a hash map to track cumulative sums. Problem Understanding For a binary list nums = [1, 0, 0, 1, 1, 1, 0, 1] and k = 3, we need to find all contiguous sublists that sum to 3. The valid sublists are: [1, 0, 0, 1, 1] starting at index 0 [0, 0, 1, 1, 1] starting at index ...
Read MoreProgram to find list showing total distance required to move all balls to current position in Python
Suppose we have a binary list called nums containing only 0s and 1s where a 0 indicates empty cell and 1 indicates the cell is filled by a ball. We have to find a new list of say L, whose size is also same like nums size, where L[i] is set to the total distance required to move all the balls to L[i]. Here the distance to move a ball from index j to index i is |j - i|. So, if the input is like nums = [1, 1, 0, 1], then the output will be [4, 3, ...
Read MoreProgram to balance the direction string so that each direction occurred quarter times in Python
Suppose we have a string s with four directions "N", "S", "W" and "E" for North, South, West and East respectively. We have to find the size of the shortest substring we can update such that each of the four directions occur n/4 times each, where n is the size of string s. So, if the input is like s = "NNSWWESN", then the output will be 1. Here n is 8, so 8/4 is 2, so if we change the last N to E, all directions will be there twice. Algorithm To solve this, we will ...
Read MoreProgram to check how many queries finds valid arithmetic sequence in Python
Suppose we have a list of numbers called nums and also have a list of queries. Each query element contains [i, j] asking whether the sublist of nums from index i to j (both inclusive) forms an arithmetic sequence. We need to count how many queries return true. An arithmetic sequence has a constant difference between consecutive elements. For example, [2, 4, 6, 8] has a common difference of 2. Example If the input is nums = [2, 4, 6, 8, 7, 6, 5, 2] and queries = [[3, 4], [0, 3], [2, 4]], then the output ...
Read MoreProgram to find maximum number of people we can make happy in Python
Sometimes we need to maximize the number of happy customers in a store by strategically changing their moods. This problem involves finding the optimal sublist of customers to make happy using a sliding window approach. Problem Statement We have two lists of equal length: customers and mood, plus an integer k. At minute i, customers[i] people enter the store. When mood[i] = 1, customers are happy; when mood[i] = 0, they are sad. We can change at most k consecutive sad customers to happy and find the maximum total happy customers. Example If customers = [2, ...
Read More