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
Programming Articles
Page 469 of 2547
Check 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 MoreRotate Matrix in Python
Rotating a matrix in Python can be done using various approaches. The most common method is the Transpose and Reverse technique, which converts rows to columns and then reverses each row to achieve a 90-degree clockwise rotation. Original Matrix Let's consider a 3×3 matrix that we want to rotate 90 degrees clockwise ? 1 5 7 9 6 3 2 1 3 Using Transpose and Reverse This method is efficient and involves two main steps ? Transpose the matrix ? Convert rows to columns and ...
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 MoreCheck if Decimal representation of an Octal number is divisible by 7 in Python
We need to check whether the decimal representation of a given octal number is divisible by 7. An octal number uses base 8, so we convert it to decimal and then check divisibility. For example, if the octal number is 61, its decimal representation is 6×8¹ + 1×8⁰ = 48 + 1 = 49, which is divisible by 7. Algorithm To solve this problem, we follow these steps − Initialize sum = 0 While the octal number is non-zero: ...
Read MoreCheck if concatenation of two strings is balanced or not in Python
Suppose we have two bracket sequences s and t containing only parentheses '(' and ')'. We need to check whether concatenating these strings in either order (s + t or t + s) results in a balanced parentheses string. So, if the input is like s = "()()))", t = "()(()(", then the output will be True because if we concatenate t + s, we get "()(()(()()))", which is balanced. Algorithm To solve this problem, we follow these steps ? Create a function is_balanced_parenthesis() to check if a string has balanced parentheses Use a stack ...
Read MoreCheck if characters of one string can be swapped to form other in Python
Sometimes we need to check if we can rearrange the characters of one string to form another string. This is essentially checking if two strings are anagrams of each other. So, if the input is like s = "worldlloeh" and t = "helloworld", then the output will be True as we can swap characters from "worldlloeh" to make "helloworld". Algorithm To solve this, we will follow these steps − Check if both strings have the same length Count the frequency of each character in the first string For each character in the second string, decrease ...
Read MoreCheck if both halves of the string have at least one different character in Python
When working with strings, sometimes we need to check if two halves have different character compositions. This involves splitting a string from the middle and comparing character frequencies between the left and right halves. For odd-length strings, we ignore the middle character. Problem Understanding Given a string, we need to ? Split the string into two equal halves (ignoring middle character if odd length) Count character frequencies in each half Check if any character has different frequencies between halves Algorithm Steps The solution follows these steps ? Create frequency maps for ...
Read More