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 63 of 377
Check if max occurring character of one string appears same no. of times in other in Python
When working with string analysis, we sometimes need to check if the most frequently occurring character in one string appears the same number of times in another string. This can be useful in text analysis and pattern matching scenarios. Problem Statement Given two strings s and t, we need to find the most frequent character in string s and check whether that character appears the same number of times in string t. For example, if s = "crosssection" and t = "securesystem", the most frequent character in s is 's' (appears 3 times). Since 's' also appears ...
Read MoreCheck if Matrix remains unchanged after row reversals in Python
When working with matrices, sometimes we need to check if a matrix remains unchanged after reversing each row. This means checking if each row is a palindrome − reads the same forwards and backwards. For example, if we have a matrix like: 686 282 333 After reversing each row, we get the same matrix, so the output will be True. Algorithm To solve this problem, we follow these steps: Get the number of rows in the matrix For each row, use two pointers (left and right) Compare elements from ...
Read MoreCheck if matrix can be converted to another matrix by transposing square sub-matrices in Python
Suppose we have two N X M matrices called mat1 and mat2. In an operation, we can transpose any square sub-matrix in mat1. We have to check whether we can get mat2 from mat1 or not by performing the given operation. The key insight is that transposing square sub-matrices preserves the elements along each diagonal. Elements that can be rearranged among themselves through transpositions must have the same sorted order in both matrices. Example Matrices Consider these input matrices ? Matrix 1 (mat1) 567 123 689 Matrix 2 (mat2) 562 173 689 ...
Read MoreCheck if matrix A can be converted to B by changing parity of corner elements of any submatrix in Python
Suppose we have two N X M binary matrices A and B. In a single operation, we can select a sub-matrix (at least 2x2) and flip the parity of its four corner elements (toggle bits 0↔1). We need to check whether matrix A can be converted to B by performing any number of such operations. Understanding the Problem When we select a 2x2 submatrix and flip its corners, we're essentially performing an XOR operation on four specific positions. The key insight is that we can systematically work through the matrix from bottom-right to top-left, fixing mismatches using operations ...
Read MoreCheck if lowercase and uppercase characters are in same order in Python
Suppose we have a string with only lowercase or uppercase letters. We need to check whether both lowercase and uppercase letters follow the same order respectively. This means if we extract all lowercase letters and all uppercase letters separately, converting the lowercase sequence to uppercase should match the uppercase sequence. So, if the input is like s = "piPpIePE", then the output will be True, as the lowercase letters "piepe" when converted to uppercase become "PIEPE", which matches the extracted uppercase sequence "PIEPE". Algorithm To solve this, we follow these steps ? ...
Read MoreCheck if linked list is sorted (Iterative and Recursive) in Python
A linked list is sorted in non-increasing order when each node's value is greater than or equal to the next node's value. We can check this condition using both iterative and recursive approaches. So, if the input is like L = [15, 13, 8, 6, 4, 2], then the output will be True because 15 ≥ 13 ≥ 8 ≥ 6 ≥ 4 ≥ 2. LinkedList Node Structure First, let's define the basic structure of a linked list node ? class ListNode: def __init__(self, data, next=None): ...
Read MoreCheck if leaf traversal of two Binary Trees is same in Python
Suppose we have two binary trees. We need to check whether the leaf traversal of these two trees is the same or not. The leaf traversal is the sequence of leaf nodes traversed from left to right. So, if the input is like ? Tree 1 2 3 4 5 ...
Read MoreCheck if LCM of array elements is divisible by a prime number or not in Python
Suppose we have an array called nums and another value k, we have to check whether LCM of nums is divisible by k or not. So, if the input is like nums = [12, 15, 10, 75] and k = 10, then the output will be True as the LCM of the array elements is 300, which is divisible by 10. Mathematical Approach The key insight is that if any element in the array is divisible by k, then the LCM of all elements will also be divisible by k. This is because LCM contains all prime ...
Read MoreCheck if item can be measured using a scale and some weights in Python
When you have weights like a0, a1, a2, ..., a100 and a weighing scale where weights can be placed on both sides, you need to determine if a particular item of weight W can be measured. This involves finding a combination where some weights are added and some are subtracted to equal the target weight. For example, if a = 4 and W = 17, the weights are 1, 4, 16, 64, ... We can achieve 17 by using 16 + 1 = 17. Algorithm The solution uses recursive backtracking to try all possible combinations of weights: ...
Read MoreCheck if it possible to partition in k subarrays with equal sum in Python
Given an array of numbers and a value K, we need to check whether we can partition the array into K contiguous subarrays such that each subarray has equal sum. This problem requires calculating cumulative sums and checking if valid partitions exist. For example, with nums = [2, 5, 3, 4, 7] and k = 3, we can create partitions [(2, 5), (3, 4), (7)] where each subarray sums to 7. Algorithm Steps To solve this problem, we follow these steps: Calculate the total sum of all elements Check if total sum is divisible by ...
Read More