Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 97 of 377

Program to find maximum length of subarray with positive product in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Oct-2021 314 Views

Suppose we have an array called nums, we have to find the maximum length of a subarray where the product of all its elements is positive. We have to find the maximum length of a subarray with positive product.So, if the input is like nums = [2, -2, -4, 5, -3], then the output will be 4 because first four elements are forming a subarray whose product is positive.To solve this, we will follow these steps :Define a function util() . This will take s, eneg := 0ns := -1, ne := -1for i in range s to e, doif ...

Read More

Program to find out the greatest subarray of a given length in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Oct-2021 714 Views

Suppose we have an array containing various integer values and a given length k. We have to find out the greatest subarray from the array of the given length. A subarray is said to be greater than another subarray, if subarray1[i] ≠ subarry2[i] and subarray1[i] > subarry2[i].So, if the input is like nums = [5, 3, 7, 9], k = 2, then the output will be [7, 9].To solve this, we will follow these steps −start := size of nums - kmax_element := nums[start]max_index := startwhile start >= 0, doif nums[start] > max_element is non-zero, thenmax_element := nums[start]max_index := startreturn ...

Read More

Program to check if number of compass usage to get out of a maze is enough in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Aug-2021 157 Views

Suppose, we are playing a game where we are trapped in a maze. We have to find our way out of the maze. The maze can be presented as an x m matrix, where n is the number of rows and m is the number of columns. Each cell/element of the matrix contains any of the symbols 'O', 'D', 'S', or '-'. 'O' means that the path is blocked, 'D' is the way out from the maze, 'S' is our starting position, and '-' means we can move through the path. We can move freely through any of the '-' ...

Read More

Program to find out the minimum number of moves for a chess piece to reach every position in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Aug-2021 679 Views

Suppose, we have a chessboard and a special knight piece K, that moves in an L shape within the board. If the piece is in position (x1, y1) and if it moves to (x2, y2) the movement can be described as x2 = x1 ± a ; y2 = y1 ± b or x2 = x1 ± b ; y2 = y1 ± a ; where a and b are integer numbers. We have to find out the minimum number of moves for that chess piece to reach to reach position (n-1, n-1) on the chessboard from the position (0, ...

Read More

Program to find longest nice substring using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 640 Views

Suppose we have a string s. We have to find longest nice substring of s. For a string s, it will be said to nice when, for every letter of the alphabet in s, it appears in uppercase and lowercase both. If there are multiple such substring, then return the substring of the earliest occurrence.So, if the input is like s = "ZbybBbz", then the output will be "bBb" as this contains lowercase and uppercase B's.To solve this, we will follow these steps −cur_max:= -1res:= blank stringfor i in range 0 to size of s, doc := s[i]upper := a ...

Read More

Program to merge strings alternately using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 6K+ Views

Suppose we have two strings s and t. We have to merge them by adding letters in alternating fashion, starting from s. If s and t are not of same length, add the extra letters onto the end of the merged string.So, if the input is like s = "major" t = "general", then the output will be "mgaejnoerral", as t is larger than s, so we have added extra part "ral" at the end.To solve this, we will follow these steps −i := j := 0result := blank stringwhile i < size of s and j < size of ...

Read More

Program to count items matching a rule using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 527 Views

Suppose we have an array nums, where each nums[i] contains three elements [type_i, color_i, name_i]. These are describing the type, color, and name of the ith item. We also have a rule represented by two other strings, ruleKey and ruleValue. Now we can say the ith item is matched the rule if one of the following is true −ruleKey = "type" and ruleValue = type_i.ruleKey = "color" and ruleValue = color_i.ruleKey = "name" and ruleValue = name_i.We have to find number of matching we can find.So, if the input is likeBikeblueElecBCarsilverSumoBikeblueTVSAnd ruleKey = "color", ruleValue = "blue", then the output ...

Read More

Program to find nearest point that has the same x or y coordinate using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 2K+ Views

Suppose we have a set of points given in an array called pts. We also have another point (x, y) which is our current location. We are defining a valid point as, a point which shares the same x-coordinate or the same y-coordinate as our current point. We have to return the index of the valid point with the smallest Manhattan distance from our current location (x, y). If there are more than one points, then return the valid point with the smallest index. (Note: the Manhattan distance between two points (a, b) and (p, q) is |a - p| ...

Read More

Program to check if binary string has at most one segment of ones or not using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 541 Views

Suppose we have a binary string s (without leading zeros), We have to check whether s contains at most one contiguous segment of ones or not.So, if the input is like s = "11100", then the output will be True as there is one segment of ones "111".To solve this, we will follow these steps −count := -1if size of s is same as 1, thenreturn Truefor each i in s, doif i is same as "1" and count > -1, thenreturn Falseotherwise when i is same as "0", thencount := count + 1return TrueLet us see the following implementation ...

Read More

Program to check whether one string swap can make strings equal or not using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 524 Views

Suppose we have two strings s and t of same length. Consider an operation where we choose two indices in a string (not necessarily different) and swap the characters at the selected indices. We have to check whether it is possible to make both strings same by performing at most one string swap on exactly one of the strings or not.So, if the input is like s = "hello" t = "hlelo", then the output will be True because we need to swap 'e' and 'l' at either s or t to make them equal.To solve this, we will follow ...

Read More
Showing 961–970 of 3,768 articles
« Prev 1 95 96 97 98 99 377 Next »
Advertisements