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 72 of 377
Check if the array has an element which is equal to sum of all the remaining elements in Python
Sometimes we need to check if an array contains an element whose value equals the sum of all other elements. This problem can be solved efficiently by calculating the total sum and using a frequency map. For example, in the array [3, 2, 10, 4, 1], the element 10 equals the sum of remaining elements: 3 + 2 + 4 + 1 = 10. Algorithm Approach The key insight is that if an element equals the sum of all other elements, then that element must be exactly half of the total array sum ? Calculate ...
Read MoreCheck if the array has an element which is equal to product of remaining elements in Python
In this problem, we need to check if an array contains an element whose value equals the product of all other elements in the array. For example, if we have nums = [3, 2, 24, 4, 1], the output will be True because 24 = (3*2*4*1). Algorithm To solve this problem, we follow these steps ? Calculate the total product of all elements For each element, check if it equals the total product divided by itself Return True if any element satisfies this ...
Read MoreCheck if sums of i-th row and i-th column are same in matrix in Python
In matrix operations, we often need to compare row and column sums. This problem asks us to check if the sum of the i-th row equals the sum of the i-th column for any row-column pair in a given matrix. Problem Understanding Given a matrix, we need to verify if there exists at least one index i where the sum of row i equals the sum of column i. For the example matrix: 2345 10642 1467 1567 Row 0 sum = 2 + 3 + ...
Read MoreCheck if sum of divisors of two numbers are same in Python
Sometimes we need to check whether two numbers have the same sum of divisors. This problem involves finding all proper divisors (excluding the number itself) of both numbers and comparing their sums. For example, if we have p = 559 and q = 703: Divisors of 559: 1, 13, 43 (sum = 57) Divisors of 703: 1, 19, 37 (sum = 57) Since both sums equal 57, the numbers have the same divisor sum. Algorithm To solve this efficiently, we'll: Create a function to calculate the sum of proper divisors Iterate only up ...
Read MoreCheck if suffix and prefix of a string are palindromes in Python
A string has palindromic prefix and suffix when both the beginning and end portions read the same forwards and backwards. This problem requires finding if a string contains at least one palindrome as a prefix and at least one palindrome as a suffix. For example, in the string "levelishighforracecar", the prefix "level" and suffix "racecar" are both palindromes. Algorithm The solution follows these steps ? Find the first palindromic prefix of length ≥ 2 If no palindromic prefix exists, return False Find any ...
Read MoreCheck if subarray with given product exists in an array in Python
When working with arrays containing positive and negative numbers, we sometimes need to check if there's a subarray whose product equals a target value. A subarray is a contiguous sequence of elements within an array. For example, given nums = [-2, -1, 1, 3, 5, 8] and k = 6, we need to find if any subarray has a product of 6. The subarray [-2, -1, 3] gives us (-2) × (-1) × 3 = 6, so the answer is True. Algorithm Approach We use Kadane's algorithm modified for products instead of sums. The key insight is ...
Read MoreCheck if right triangle possible from given area and hypotenuse in Python
Given the hypotenuse and area of a right triangle, we need to find the base and height. If it's not possible to form such a triangle, we return False. So, if the input is like hypo = 10, area = 24, then the output will be (6, 8). Algorithm To solve this, we will follow these steps − Calculate the maximum possible area for the given hypotenuse If the required area exceeds the maximum, return False Use binary search to find the base ...
Read MoreCheck if reversing a sub array make the array sorted in Python
Sometimes we need to check if an array can be sorted by reversing exactly one sub-array. This problem involves finding a decreasing sequence and verifying if reversing it would make the entire array sorted. Problem Understanding Given an array with unique elements, we need to determine if reversing one sub-array will make the entire array sorted. If the array is already sorted, return True. For example, in [4, 6, 27, 25, 15, 9, 37, 42], reversing the sub-array [27, 25, 15, 9] gives us [4, 6, 9, 15, 25, 27, 37, 42], which is sorted. Algorithm ...
Read MoreCheck if each internal node of a BST has exactly one child in Python
Suppose we have the preorder traversal of a binary search tree (BST). We have to check whether each internal node has only one child or not. So, if the input is like preorder = [22, 12, 13, 15, 14], then the output will be True as BST is like − 22 12 13 15 14 ...
Read MoreCheck if difference of areas of two squares is prime in Python
When working with two numbers, we can check if the difference of their square areas is a prime number. This problem uses the algebraic identity that x² - y² = (x + y)(x - y). For example, if x = 7 and y = 6, the difference is 49 - 36 = 13, which is prime. Understanding the Mathematical Approach The key insight is using the factorization: x² - y² = (x + y)(x - y) For this product to be prime, one factor must equal 1 (since a prime has only two factors: 1 and ...
Read More