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 67 of 377
Check If every group of a is followed by a group of b of same length in Python
Suppose we have a lowercase string s with only two characters a and b. We have to check whether every group of consecutive a's is followed by group of consecutive b's of equal length. So, if the input is like s = "abaaabbbaabbaabbab", then the output will be True, as all groups are (ab), (aaabbb), (aabb), (aabb), (ab). Algorithm To solve this, we will follow these steps − Initialize a_count to 0 and get the length of string s Set index i to 0 While i ...
Read MoreCheck if elements of Linked List are present in pair in Python
A linked list has elements in pairs when every element appears an even number of times. This problem can be efficiently solved using the XOR operation, which has a useful property: XORing identical numbers results in 0. Algorithm Explanation The XOR approach works because: XORing any number with itself gives 0 XOR is commutative, so order doesn't matter If all elements appear in pairs (even times), the final XOR result will be 0 If any element appears an odd number of times, the result will be non-zero Implementation Steps To solve this problem, we ...
Read MoreCheck if elements of array can be made equal by multiplying given prime numbers in Python
Given two arrays nums and primes, we need to check whether all elements in nums can be made equal by multiplying each element with one or more prime numbers from the primes array. For example, if nums = [25, 100] and primes = [2, 5], we can multiply 25 by 2 twice (25 × 2 × 2 = 100) to make both elements equal to 100. Approach The solution involves finding the LCM (Least Common Multiple) of all numbers in nums and checking if each element can be transformed to this LCM using only the given prime ...
Read MoreCheck if elements of an array can be arranged satisfying the given condition in Python
Sometimes we need to check if array elements can be rearranged to satisfy a specific pairing condition. In this problem, we want to arrange elements such that for every even index i, the element at odd index 2*i + 1 equals twice the element at even index 2*i. The condition is: nums[2*i + 1] = 2 * nums[2*i] for all valid indices i. Understanding the Problem For the array [8, -4, 4, -8], we can rearrange it as [-4, -8, 4, 8]: For i = 0: nums[1] = -8, which equals 2 ...
Read MoreCheck if edit distance between two strings is one in Python
The edit distance between two strings represents the minimum number of operations needed to transform one string into another. To check if the edit distance is exactly one, we need to verify that only a single operation is required. The three possible operations are ? Insert a character Delete a character Replace a character For example, if s = "hello" and t = "heillo", we need to insert one character 'i' into s to get t, so the edit distance is 1. Algorithm ...
Read MoreCheck if at least half array is reducible to zero by performing some operation in Python
Given an array of positive integers and a positive integer m, we need to determine if at least half of the array elements can be reduced to zero through a specific operation. In each iteration, we decrease some elements by 1 and increase the remaining elements by m. The key insight is that elements with the same remainder when divided by (m + 1) will follow the same pattern and can potentially reach zero together. Algorithm To solve this problem, we follow these steps: Create a frequency array to count elements by their remainder when ...
Read MoreCheck if all levels of two trees are anagrams or not in Python
In this problem, we need to check if each level of two binary trees contains the same elements (forming anagrams). Two strings are anagrams if they contain the same characters in different order. Similarly, two tree levels are anagrams if they contain the same values in any order. The approach uses level-order traversal (BFS) to compare each level of both trees simultaneously. Tree 1: 5 6 7 ...
Read MoreCheck if absolute difference of consecutive nodes is 1 in Linked List in Python
Suppose we have a singly linked list where each node contains an integer value. We need to check if the absolute difference between every pair of consecutive nodes is exactly 1. For example, if the input is 5→6→7→8→7→6→5→4, the output will be True because each consecutive pair has an absolute difference of 1: |5-6|=1, |6-7|=1, |7-8|=1, |8-7|=1, |7-6|=1, |6-5|=1, |5-4|=1. Algorithm To solve this problem, we follow these steps: Start with the head node as current While current node exists: If current.next is None, break (reached end) If |current.value - current.next.value| ≠ 1, return ...
Read MoreCheck if a word exists in a grid or not in Python
When working with grids or matrices of characters, we often need to search for words that can appear horizontally, vertically, or diagonally. This problem involves finding whether a given word exists in a 2D grid by checking all possible directions from each starting position. Problem Understanding Given a grid of characters and a target word, we need to check if the word exists in the grid. The word can be found in four directions ? Horizontally left to right Horizontally right to left Vertically top to bottom Vertically bottom to top Here's the sample ...
Read MoreCheck if a triplet with given sum exists in BST in Python
In this problem, we need to find if there exists any triplet (group of three elements) in a Binary Search Tree (BST) that sums up to a given target value. We'll use the BST's inorder traversal property to get a sorted array, then apply the two-pointer technique. Approach The solution involves two main steps: Perform inorder traversal of BST to get elements in sorted order Use three pointers to find triplet with target sum BST Structure 5 3 ...
Read More