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 on Trending Technologies
Technical articles with clear explanations and examples
Check 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 MoreCheck if it is possible to make two matrices strictly increasing by swapping corresponding values only in Python
Given two matrices of size n x m named mat1 and mat2, we need to check if both matrices can be made strictly increasing by swapping corresponding elements at position (i, j) between the matrices. A matrix is strictly increasing if each element is smaller than the element to its right and below it. Problem Understanding Consider these input matrices ? mat1 7 15 16 10 mat2 ...
Read MoreCheck if it is possible to get back to 12 O-clock only by adding or subtracting given seconds in Python
Suppose we have an array of n different second values. We have to check whether it is possible to start from 12 O'clock and go back to 12 by only adding or subtracting the given seconds or not. We can use all given seconds exactly once, we can either add seconds or subtract it. So, if the input is like seconds = [40, 90, 50], then the output will be True as we can add 40, then subtract 90, then again add 50 to get back to 12 O'clock. Algorithm To solve this, we will follow these ...
Read More