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 135 of 377
Find N distinct numbers whose bitwise Or is equal to K in Python
Given two integers N and K, we need to find N distinct numbers whose bitwise OR equals K. If no such combination exists, we return -1. For example, if N = 4 and K = 6, one possible output is [6, 0, 1, 2] because 6 | 0 | 1 | 2 = 6. Algorithm Approach The key insight is that we need at least 2^(number of set bits in K) different numbers to construct all possible combinations. We start with K itself, then generate additional numbers by systematically setting different bit patterns. Implementation ...
Read MoreFind missing element in a sorted array of consecutive numbers in Python
When working with a sorted array of consecutive numbers that has one missing element, we can use binary search to efficiently find the missing number in O(log n) time complexity. So, if the input is like A = [1, 2, 3, 4, 5, 6, 7, 9], then the output will be 8. Algorithm The approach uses binary search with the following logic ? For each element at index i, if no element is missing before it, then A[i] - i should equal A[0] If A[mid] - mid equals A[0], the missing element is in the ...
Read MoreFind minimum time to finish all jobs with given constraints in Python
We need to find the minimum time to finish all jobs when we have k assignees and each assignee takes t time units per job unit. The key constraints are that each assignee can only work on contiguous jobs, and no job can be split between assignees. The problem can be solved using binary search on the answer. We search for the minimum possible time limit such that all jobs can be completed by k assignees within that limit. Algorithm Overview The solution uses binary search between the minimum possible time (maximum single job) and maximum possible ...
Read MoreFind minimum adjustment cost of an array in Python
The minimum adjustment cost problem involves modifying array elements to ensure adjacent differences don't exceed a target value while minimizing the total cost of changes. We use dynamic programming to find the optimal solution. Problem Statement Given an array of positive numbers, we need to replace elements so that the difference between any two adjacent elements is at most equal to a given target. The goal is to minimize the adjustment cost, which is the sum of absolute differences between original and new values: ∑|A[i] - Anew[i]|. Example If input array is [56, 78, 53, 62, ...
Read MoreFind median of BST in O(n) time and O(1) space in Python
Finding the median of a Binary Search Tree (BST) efficiently requires understanding that an inorder traversal of a BST gives nodes in sorted order. The median is the middle element for odd number of nodes, or the average of two middle elements for even number of nodes. For a BST with n nodes: Odd nodes: median = (n+1)/2th node Even nodes: median = average of (n/2)th and (n/2+1)th nodes We'll use Morris Traversal to achieve O(n) time complexity with O(1) space complexity by avoiding recursion stack. Example Tree ...
Read MoreFind 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 More