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
Articles by Arnab Chakraborty
Page 107 of 377
Find maximum distance between any city and station in Python
Suppose we have N number of cities, and they are numbered from 0 to N-1 and we also have the cities in which stations are located, we have to find the maximum distance between any city and its nearest station. We have to keep in mind that the cities with stations can be given in any order.So, if the input is like N = 6 and stations = [2, 4], then the output will be 2To solve this, we will follow these steps −station_present := a list of size N, and fill with Falsefor each city in station, dostation_present[city] := ...
Read MoreFind maximum length Snake sequence in Python
Suppose we have a grid of numbers; we have to find a snake sequence and return it. If there are multiple snake sequence, then return only one. As we know a snake sequence is made using adjacent numbers in the grid so for each number, the number on the right-hand side or the number below it is either +1 or -1 its value. So, if current value is in grid cell (a, b), we can either move right (a, b+1) if that number is ± 1 or move below (a+1, b) if that number is ± 1.So, if the input ...
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 first N natural numbers should not exceed the value X.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 of the series will exceed X = 7 So, 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14.To solve this, we will follow these steps −Define a function sum_of_squares() . This will take Nres :=(N *(N + ...
Read MoreFind maximum operations to reduce N to 1 in Python
Suppose we have two numbers P and Q and they form a number N = (P!/Q!). We have to reduce N to 1 by performing maximum number of operations possible. In each operation, one can replace N with N/X when N is divisible by X. We will return the maximum number of operations that can be possible.So, if the input is like A = 7, B = 4, then the output will be 4 as N is 210 and the divisors are 2, 3, 5, 7.To solve this, we will follow these steps −N := 1000005factors := an array of ...
Read MoreFind maximum points which can be obtained by deleting elements from array in Python
Suppose we have an array A with N elements, we also have two integers l and r where, 1≤ ax ≤ 10^5 and 1≤ l≤ r≤ N. Taking an element from the array say ax and remove it, and also remove all elements equal to ax+1, ax+2 … ax+R and ax-1, ax-2 … ax-L from that array. By doing this it will cost ax points. We have to maximize the total cost after removing all of the elements from the array.So, if the input is like A = [2, 4, 3, 10, 5], l = 1, r = 2, then ...
Read MoreFind maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
Suppose we have an array of positive numbers, there are n elements in that array, we have to find the maximum sum of triplet (ai + aj + ak ) such that 0 A[i], thensecond_max := maximum of second_max, A[j]if first_max and second_max is non-zero, thenres := maximum of res, first_max + A[i] + second_maxreturn resExampleLet us see the following implementation to get better understanding −def get_max_triplet_sum(A) : n = len(A) res = 0 for i in range(1, (n - 1)) : first_max = 0 second_max = 0 ...
Read MoreFind median of BST in O(n) time and O(1) space in Python
Suppose we have Binary Search Tree(BST), we have to find median of it. We know for even number of nodes, median = ((n/2th node + (n+1)/2th node) /2 For odd number of nodes, median = (n+1)/2th node.So, if the input is likethen the output will be 7To solve this, we will follow these steps −if root is same as None, thenreturn 0node_count := number of nodes in the treecount_curr := 0current := rootwhile current is not null, doif current.left null, thencount_curr := count_curr + 1if node_count mod 2 is not 0 and count_curr is same as(node_count + 1) /2, thenreturn ...
Read MoreFind minimum adjustment cost of an array in Python
Suppose we have an array of positive numbers; we replace each element from that array array so that the difference between two adjacent elements in the array is either less than or equal to a given target. Now, we have to minimize the adjustment cost, so the sum of differences between new value and old value. More precisely, we minimize ∑|A[i] – Anew[i]| where i in range 0 to n-1, here n is denoted as size of A and Anew is the array with adjacent difference less than or equal to target.So, if the input is like [56, 78, 53, ...
Read MoreFind minimum time to finish all jobs with given constraints in Python
Suppose we have an array of jobs with different time requirements, there are k different persons to assign jobs and we also have how much time t an assignee takes to do one unit of the job. We have to find the minimum time to complete all jobs with following constraints.An assignee can be assigned only the contiguous jobs.Two assignees cannot share or perform a single job.So, if the input is like k = 4, t = 5, job = {12, 6, 9, 15, 5, 9}, then the output will be 75 as we get this time by assigning [12], ...
Read MoreFind the largest area rectangular sub-matrix whose sum is equal to k in C++
Suppose we have a 2D matrix mat and a value K, we have to find the longest rectangular submatrix whose sum is same as K.So, if the input is like28-56-778-311-1443-43110And K = 9then the output will be Top-Left point is (1, 0) and Bottom-Right point is (3, 2).-77811-144-431To solve this, we will follow these steps −MAX := 100Define a function sum_k(), this will take one array arr, start, end, n, k, Define one mapsum := 0, maximum_length := 0for initialize i := 0, when i < n, update (increase i by 1), do −sum := sum + arr[i]if sum is ...
Read More