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
Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
Given an array of positive numbers, we need to find the maximum sum of a triplet (a[i] + a[j] + a[k]) such that 0 ≤ i < j < k < n and a[i] < a[j] < a[k]. This means we're looking for three increasing elements at increasing indices. For example, if the input is A = [3, 6, 4, 2, 5, 10], the valid triplets are (3, 4, 5): sum = 12, (3, 6, 10): sum = 19, (3, 4, 10): sum = 17, (4, 5, 10): sum = 19, and (2, 5, 10): sum = 17. The ...
Read MoreFind maximum points which can be obtained by deleting elements from array in Python
When we delete an element from an array, we might need to remove related elements as well. This problem asks us to find the maximum points we can obtain by strategically deleting elements, where deleting an element ax gives us ax points but also forces us to remove elements in the range [ax-L, ax+R]. So, if the input is like A = [2, 4, 3, 10, 5], l = 1, r = 2, then the output will be 18. Algorithm Steps To solve this, we will follow these steps − Find the ...
Read MoreFind maximum operations to reduce N to 1 in Python
We have two numbers P and Q that form N = P!/Q!. Our goal is to reduce N to 1 by performing the maximum number of operations possible. In each operation, we can replace N with N/X when N is divisible by X. We need to find the maximum number of operations possible. The key insight is that the maximum number of operations equals the total count of prime factors in N. Since N = P!/Q!, we need to count prime factors in factorials efficiently. Algorithm Approach We use a sieve-like approach to precompute the count of ...
Read MoreFind maximum N such that the sum of square of first N natural numbers is not more than X in Python
Suppose we have a given integer X, we have to find the maximum value N so that the sum of squares of first N natural numbers should not exceed the value X. The sum of squares of first N natural numbers follows the formula: 1² + 2² + 3² + ... + N² = N × (N + 1) × (2N + 1) / 6 So, if the input is like X = 7, then the output will be 2 as 2 is the maximum possible value of N. For N = 3, the sum would be 1² ...
Read MoreFind maximum length Snake sequence in Python
A snake sequence is a path in a grid where adjacent numbers differ by exactly 1. You can move right or down in the grid, and each step must be to a number that is either +1 or -1 from the current value. Problem Understanding Given a grid of numbers, we need to find the longest snake sequence. For example, in this grid ? 10 7 6 3 9 8 7 6 8 4 2 7 2 2 2 8 The longest snake sequence has length ...
Read MoreFind maximum distance between any city and station in Python
Sometimes we need to find the maximum distance between any city and its nearest station. Given N cities numbered from 0 to N-1 and a list of cities with stations, we need to calculate the furthest any city is from a station. So, if the input is like N = 6 and stations = [2, 4], then the output will be 2, because city 0 is distance 2 from the nearest station at city 2. Algorithm Overview To solve this problem, we will follow these steps − Create a boolean array to mark which cities ...
Read MoreFind maximum difference between nearest left and right smaller elements in Python
Given an array of integers, we need to find the maximum absolute difference between the nearest left and right smaller elements for each position. If no smaller element exists on either side, we consider it as 0. For example, with array [3, 5, 9, 8, 8, 10, 4], the left smaller elements are [0, 3, 5, 5, 5, 8, 3] and right smaller elements are [0, 4, 8, 4, 4, 4, 0]. The maximum absolute difference is |8 - 4| = 4. Algorithm Steps We use a stack-based approach to find nearest smaller elements efficiently ? ...
Read MoreFind lost element from a duplicated array in Python
Finding a lost element from a duplicated array is a common problem where two arrays are identical except one element is missing from one of them. We need to identify which element is missing efficiently. So, if the input is like A = [2, 5, 6, 8, 10], B = [5, 6, 8, 10], then the output will be 2 as 2 is missing from second array. Using Binary Search Approach The most efficient approach uses binary search to find the missing element in O(log n) time complexity ? def solve(A, B, N): ...
Read MoreFind longest palindrome formed by removing or shuffling chars from string in Python
Suppose we have a string; we have to find the longest palindrome that can be generated by deleting or shuffling the characters from the string. If there are more than one palindrome, then return only one. So, if the input is like pqqprrs, then the output will be pqrsrqp. Approach To solve this problem, we will follow these steps ? Count the frequency of each character in the string For each character, use pairs to form the left and right sides of the palindrome ...
Read MoreFind longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python
A bitonic sequence is a sequence that first increases and then decreases. In this problem, we need to find the longest bitonic sequence where the increasing part comes from the first array and the decreasing part comes from the second array, both as subsequences. Given two arrays A and B, we find the longest increasing subsequence from A, then the longest increasing subsequence from the reverse of B (which gives us a decreasing sequence), and combine them. Algorithm Overview The solution uses dynamic programming with binary search optimization: Find the longest increasing subsequence (LIS) from ...
Read More