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 29 of 377
Program to find maximum score by splitting binary strings into two parts in Python
Suppose we have a binary string s. We need to split it into two non-empty substrings s1 and s2. The score of this split is the count of "0"s in s1 plus the count of "1"s in s2. We have to find the maximum score we can obtain. So, if the input is like s = "011001100111", then the output will be 8, because we can split the string like "01100" + "1100111". Then, the score is 3 + 5 = 8. Algorithm To solve this, we will follow these steps − ones := number ...
Read MoreProgram to find matrix for which rows and columns holding sum of behind rows and columns in Python
Given a matrix, we need to find a new matrix where each element at position res[i, j] contains the sum of all elements from the original matrix where row r ≤ i and column c ≤ j. This is known as calculating the prefix sum matrix or cumulative sum matrix. Problem Understanding For each position (i, j) in the result matrix, we sum all elements in the rectangle from (0, 0) to (i, j) in the original matrix. If the input matrix is ? 8 2 7 4 Then ...
Read MoreProgram to find length of longest sublist containing repeated numbers by k operations in Python
Suppose we have a list called nums and a value k, now let us consider an operation by which we can update the value of any number in the list. We have to find the length of the longest sublist which contains repeated numbers after performing at most k operations. So, if the input is like nums = [8, 6, 6, 4, 3, 6, 6] k = 2, then the output will be 6, because we can change 4 and 3 to 6, to make this array [8, 6, 6, 6, 6, 6, 6], and the length of sublist ...
Read MoreProgram to find length of longest contiguous sublist with same first letter words in Python
Suppose we have a list of lowercase alphabet strings called words. We have to find the length of the longest contiguous sublist where each word has the same first letter. So, if the input is like words = ["she", "sells", "seashells", "on", "the", "sea", "shore"], then the output will be 3, because the longest contiguous sublist is ["she", "sells", "seashells"] where each word starts with 's'. Algorithm To solve this, we will follow these steps − Initialize cnt = 1 to track current sequence length Initialize maxcnt = 0 to track maximum length found Initialize ...
Read MoreProgram to find number of possible position in n-person line with few person at front and back in Python
Suppose we have three numbers n, a and b. Consider we are in a line of n people, and we are unaware about our position in the line. But we know there are at least a number of people in front of us and at most b number of people behind us. We have to find the number of possible positions for us. So, if the input is like n = 10, a = 3, b = 4, then the output will be 5. There are 10 people in the line with at least 3 people in front and ...
Read MoreProgram to find lexicographically largest mountain list in Python
A mountain list is a sequence that strictly increases to a peak and then strictly decreases. Given three positive numbers n, lower, and upper, we need to find the lexicographically largest mountain list of length n with all values in range [lower, upper]. The challenge is ensuring both increasing and decreasing parts are non-empty while maximizing the lexicographic order. Problem Analysis For a valid mountain list of length n: Must have at least one increasing element and one decreasing element Total possible unique values = upper − lower + 1 Maximum mountain length = 2 ...
Read MoreProgram to find maximum sum by performing at most k negate operations in Python
Suppose we have a list of elements called nums and another value k. We can perform an operation where we select an element from nums and negate it. We can perform exactly k number of operations and need to find the maximum resulting sum. So, if the input is like nums = [2, 1, -6, -2] and k = 3, then the output will be 9. If we negate -6 and -2 and 1, we get [2, -1, 6, 2] and its sum is 9. Algorithm To solve this problem, we follow these steps − ...
Read MoreProgram to find value for which given array expression is maximized in Python
Suppose we have two arrays called nums and values, both contain integers where the values of nums are strictly increasing and their lengths are the same. We have to find the maximum value of v for a pair of indices i, j such that i ≤ j that maximizes v = values[i] + values[j] + nums[j] - nums[i]. So, if the input is like nums = [1, 2, 7] and values = [-4, 6, 5], then the output will be 16. If we pick i = 1 and j = 2 we get 6 + 5 + 7 - ...
Read MoreProgram to find number of elements in matrix follows row column criteria in Python
Suppose we have a binary matrix; we have to find the number of elements in matrix that follows the following rules − matrix[r, c] = 1 matrix[r, j] = 0 for every j when j is not equal to c and matrix[i, c] = 0 for every i when i is not equal to r. In simple terms, we need to find cells that contain 1 and are the only 1 in both their row and column. Example Input So, if the input is like ...
Read MoreProgram to find k where k elements have value at least k in Python
Suppose we have a list of numbers called nums, that contains only non-negative numbers. If there are exactly k number of elements in nums that are greater than or equal to k, find the value k. If we cannot find such value, then return -1. So, if the input is like nums = [6, 4, 0, 8, 2, 9], then the output will be 4, because there are exactly 4 elements that are greater than or equal to 4: [6, 4, 8, 9]. Algorithm To solve this, we will follow these steps − ...
Read More