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 79 of 377
Program to find maximum score of a good subarray in Python
Suppose we have an array called nums and a value k. Consider the score of a subarray (i, j) is defined as minimum of subarray nums[i..j] * (j-i+1). Now, a good subarray is a subarray where i -1 and nums[i] >= minNum, doi := i - 1while j < size of nums and nums[j] >= minNum, doj := j + 1ans := maximum of ans and ((j - i - 1) * minNum)minNum := maximum of (nums[i] if i > -1 otherwise -1) and (nums[j] if j < size of nums otherwise -1)return ansExampleLet us see the following implementation ...
Read MoreProgram to make the XOR of all segments equal to zero in Python
Suppose we have an array called nums and another value k. The XOR of a segment [left, right] (left 0, thenfor each index j and value prev in dp, donew_dp[i XOR j] := maximum of new_dp[i XOR j] and prev+cntdp := new_dpreturn size of nums - new_dp[0]ExampleLet us see the following implementation to get better understandingdef solve(nums, k): LIMIT = 2**10 temp = [[0 for _ in range(LIMIT)] for _ in range(k)] for i, x in enumerate(nums): temp[i%k][x] += 1 dp = [-2000 for _ in range(LIMIT)] ...
Read MoreProgram to find out if a vertex in an undirected graph has a lesser cost path in Python
Suppose, we are given a weighted, undirected graph. We have to implement a function query that takes two vertices and a cost 'limit' as input and checks if there exists a lower cost path than the cost given as input. We return true if there exists a path or otherwise, we return false.So, if the input is likeand the queries are (0, 2, 10), (3, 1, 30), (4, 3, 30).then the output will beFalse True TrueThe result of the first query is False because there is no path between vertex 0 to 2 of cost 10.The result of the second ...
Read MoreProgram to find max chunks to make array sorted in Python
Suppose we have an array nums, we have to split the array into some number of partitions, and individually sort each of them. Now after concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made?So, if the input is like [3, 2, 4, 5, 5], then the output will be 4, as we can make partitions like [3, 2], [4], [5], [5] .To solve this, we will follow these steps −real:= sort the list numsp1 := 0, p2 := 1, c := 0Do the following infinitely, doflag:= Truetmp:= sort ...
Read MoreProgram to find out the sum of evenly spaced elements in an array in Python
Suppose, there is an array 'nums' of size n containing positive integers. We have another array 'queries' that contain integer pairs (pi, qi). For every query in the array queries, the answer will be the sum of numbers in the array nums[j] where pi
Read MoreProgram to find maximize palindrome length from subsequences in Python
Suppose we have two strings, s and t. We want to make a string in the following manner −Select some non-empty subsequence sub1 from s.Select some non-empty subsequence sub2 from t.Concatenate sub1 and sub2, to make the string.We have to find the length of the longest palindrome that can be formed in the described manner. If we cannot make any palindrome, then return 0.So, if the input is like s = "hillrace" t = "cargame", then the output will be 7 because we can take "race" from s and "car" from r, so "racecar" is the palindrome with length 7.To ...
Read MoreProgram to count the number of ways to distribute n number of candies in k number of bags in Python
Suppose, there are n number of candies and k bags in which the candies have to put into. We have to find out the number of possible ways the candies can be distributed so that each bag contains at least one candy. Every candy in this scenario is unique, so we have to count all the possible ways the candies can be distributed in the bags.So, if the input is like n = 3, k = 2, then the output will be 3.The candies can be put in this manner −(1, 2), (3) (1) , (2, 3) (2), (1, 3)To ...
Read MoreProgram to check whether one point can be converted to another or not in Python
Suppose we have a starting points (sx, sy), and target point (tx, ty), we have to check whether a sequence of moves exists from the start point to the end point. Here move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).So, if the input is like (sx, sy) = (1, 1) (tx, ty) = (4, 5), then the output will be True, this is because move (1, 1) to (2, 1), then (3, 1), then (4, 1), then (4, 5).To solve this, we will follow these steps −Define a function solve() ...
Read MoreProgram to find smallest value of K for K-Similar Strings in Python
Suppose we have two strings s and t. These two strings are K-similar if we can swap the positions of two letters in s exactly K times so that the resulting string is t. So, we have two anagrams s and t, we have to find the smallest K for which s and t are K-similar.So, if the input is like s = "abc" t = "bac", then the output will be 1.To solve this, we will follow these steps −Define a function neighbors() . This will take new_datafor each index i and value c in new_data, doif c is ...
Read MoreProgram to find maximum subarray min-product in Python
Suppose we have an array nums, we have to find the maximum min-product of each non-empty subarray of nums. Since the answer can be big enough, return it in modulo 10^9+7. The min-product of the array is equal to the minimum value in the array multiplied by the sum of the array. So if we have an array is like [4, 3, 6] (minimum value is 3) has a min-product of 3*(4+3+6) = 3*13 = 39.So, if the input is like nums = [2, 3, 4, 3], then the output will be 30 because we can get subarray [3, 4, ...
Read More