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 64 of 377
Check if it is possible to transform one string to another in Python
When we need to check if one string can be transformed into another by converting lowercase letters to uppercase and removing remaining lowercase letters, we can use dynamic programming to solve this step by step. Given two strings s and t (where t is in uppercase), we need to determine if we can transform s to t by performing these operations: Convert some lowercase letters to uppercase Remove all remaining lowercase letters Problem Understanding For example, if s = "fanToM" and t = "TOM", we can: Convert 'o' to 'O' Remove lowercase letters ...
Read MoreCheck if it is possible to survive on Island in Python
Suppose there is an island with only one store that remains open every day except Sunday. We need to determine if someone can survive for a given number of days and find the minimum number of days they need to buy food. Given the following parameters: N - Maximum number of food units someone can buy each day S - Number of days someone is required to survive M - Number of food units required each day to survive Starting from Monday, we need to check ...
Read MoreCheck if it is possible to sort the array after rotating it in Python
Sometimes we have an array that can be sorted by rotating it. Rotation means taking some contiguous elements from the end and moving them to the front. We need to check if such a rotation can make the array sorted. For example, the array [4, 5, 6, 1, 2, 3] can be sorted by rotating the last three elements [1, 2, 3] to the front, giving us [1, 2, 3, 4, 5, 6]. Algorithm The approach works by finding the rotation point where the sorted order breaks ? If the array is already sorted, return ...
Read MoreCheck if it is possible to sort an array with conditional swapping of adjacent allowed in Python
Suppose we have an unordered array of numbers called nums and all elements are in range 0 to n-1. We can swap adjacent elements in nums as many times as required but only when the absolute difference between these elements is 1. We have to check whether we can sort the nums or not. So, if the input is like nums = [1, 0, 3, 2, 5, 4], then the output will be True as we can swap these pairs [(1, 0), (3, 2), (5, 4)] to sort [0, 1, 2, 3, 4, 5]. Algorithm To solve ...
Read MoreCheck if it is possible to rearrange rectangles in a non-ascending order of breadths in Python
Sometimes we have a list of rectangles represented by their length and breadth coordinates, and we need to arrange them in non-ascending (non-increasing) order of breadths. Since rectangles can be rotated 90 degrees, we can swap length and breadth to optimize the arrangement. The key insight is that for each rectangle, we can choose either dimension as the breadth. We need to make optimal choices to create a non-increasing sequence. Problem Understanding Given rectangles with dimensions [length, breadth], we want to: Optionally rotate each rectangle (swap dimensions) Arrange them so breadths are in non-increasing order ...
Read MoreCheck if it is possible to serve customer queue with different notes in Python
Suppose we have an array called notes representing different rupee notes held by customers in a queue. They are all waiting to buy tickets worth Rs 50. The possible notes are [50, 100, and 200]. We need to check whether we can sell tickets to all customers in order, starting with Rs 0 in our hand. For example, if the input is notes = [50, 50, 100, 100], the output will be True. For the first two customers, we don't need to return any change, but we collect two Rs 50 notes. For the last two customers with Rs ...
Read MoreCheck if it is possible to rearrange a binary string with alternate 0s and 1s in Python
Given a binary string, we need to check if we can rearrange it to have alternating 0s and 1s. For this to be possible, the counts of 0s and 1s must differ by at most 1. For example, with input s = "1000111", the output will be True because we can form "1010101" from the given string. Algorithm To solve this problem, we follow these steps: Count the number of 1s and 0s in the binary string If the string length is even, both counts must be equal If the string length is odd, the ...
Read MoreCheck if it is possible to reach vector B by rotating vector A and adding vector C to its in Python
In this problem, we need to check if we can transform vector A into vector B using two operations: adding vector C any number of times and rotating 90 degrees clockwise. This is a mathematical problem involving vector transformations and linear combinations. Problem Understanding Given three 2D vectors A, B, and C, we want to determine if B can be reached from A using: Adding vector C any number of times (positive or negative) Rotating the resulting vector 90 degrees clockwise The key insight is that we need to check four possible scenarios based ...
Read MoreCheck if it is possible to reach a number by making jumps of two given length in Python
We need to find the minimum number of steps to reach position q from starting position p by making jumps of length d1 or d2 in either direction (left or right). The key insight is that we can only reach a target if the difference between start and end positions is divisible by the GCD of the two jump distances. Algorithm Steps To solve this problem, we follow these steps ? Calculate GCD of d1 and d2 Check if (p - q) is divisible by GCD - if not, return -1 Use BFS to find ...
Read MoreCheck if it is possible to move from (0, 0) to (x, y) in N steps in Python
Given a coordinate point (x, y) and a number of steps n, we need to determine if it's possible to move from the origin (0, 0) to the target point using exactly n steps. We can move in four directions: left, right, up, and down. For example, if we want to reach point (2, 1) in 3 steps, we can move two steps right and one step up, which is possible. Understanding the Problem The key insight is that we need at least the Manhattan distance (|x| + |y|) steps to reach the target. Any extra steps ...
Read More