Programming Articles - Page 1194 of 3363

Program to find number of special positions in a binary matrix using Python

Arnab Chakraborty
Updated on 17-May-2021 13:00:44

320 Views

Suppose we have a binary matrix of order m x n, we have to find the number of special positions in the matrix. A position (i, j) is a special position when mat[i, j] = 1 and all other elements in row i and column j are 0.So, if the input is like10000001000001101000then the output will be 3, here the special positions are (0, 0), (1, 2) and (3, 1).To solve this, we will follow these steps −special := 0for i in range 0 to row count of matrix, doif number of 1s in row matrix[i] is 1, thennumOfOne := ... Read More

Program to replace all question symbols to avoid consecutive repeating characters in Python

Arnab Chakraborty
Updated on 17-May-2021 12:53:59

472 Views

Suppose we have a lowercase string s that contains only letters and '?' character, we have to convert all '?' characters into lower case letters such that the final string will not have any consecutive repeating characters. If there is more than one solution, return any of them.So, if the input is like s = "hel??", then the output will be helab, se first question mark may be anything except 'l' and when first one is given, then second one can be anything except 'a'.To solve this, we will follow these steps −if size of s is same as 1, ... Read More

Program to find diagonal sum of a matrix in Python

Arnab Chakraborty
Updated on 17-May-2021 12:53:35

9K+ Views

Suppose we have a square matrix; we have to find the sum of the matrix diagonals. So only include the sum of all of the elements on the primary diagonal and all the elements on the secondary diagonal and ignore the crossing element.So, if the input is like10596815323812321173then the output will be The primary diagonal elements are [10, 15, 12, 3] sum is 40, secondary diagonal [6, 3, 8, 2] sum is 19, so total sum 59.To solve this, we will follow these steps −m := row count of matrixif m is same as 1, thenreturn matrix[0, 0]count := 0for ... Read More

Program to check pattern of length m repeated K or more times exists or not in Python

Arnab Chakraborty
Updated on 17-May-2021 12:53:11

247 Views

Suppose we have an array nums with positive values, we have to find a pattern of length m that is repeated k or more than k times. Here a pattern is a non-overlapping subarray (consecutive) that consists of one or more values and are repeated multiple times. A pattern is defined by its length and number of repetitions. We have to check whether there exists a pattern of length m that is repeated k or more times or not.So, if the input is like nums = [3, 5, 1, 4, 3, 1, 4, 3, 1, 4, 3, 9, 6, 1], ... Read More

Program to find most visited sector in a circular track using Python

Arnab Chakraborty
Updated on 17-May-2021 12:52:52

291 Views

Suppose we have a number n and an array called rounds. We have a circular track which consists of n different sectors labeled from 1 to n. Now consider a race will be held on this track, the race consists of m different rounds. The ith round starts at sector rounds[i - 1] and ends at sector rounds[i]. For example, if the round 1 starts at sector rounds[0] and ends at sector rounds[1]. So we have to find the most visited sectors sorted in ascending order. (The track numbers are in ascending order of sector numbers in the counter-clockwise direction)So, ... Read More

Program to find number with thousand separator in Python

Arnab Chakraborty
Updated on 17-May-2021 12:51:49

548 Views

Suppose we have a number n, we have to return this number into string format where thousands are separated by comma (", ").So, if the input is like n = 512462687, then the output will be "512, 462, 687"To solve this, we will follow these steps −res := n as stringres := reversed form of resans := a blank stringfor i in range 0 to size of res - 1, doif i mod 3 is same as 0 and i is not same as 0, thenans := ans concatenate ', 'ans := ans concatenate res[i]ans := reversed form of ansreturn ... Read More

Program to check three consecutive odds are present or not in Python

Arnab Chakraborty
Updated on 17-May-2021 12:29:21

700 Views

Suppose we have an array called nums, we have to check whether there are three consecutive odd numbers in nums or not.So, if the input is like nums = [18, 15, 2, 19, 3, 11, 17, 25, 20], then the output will be True as there are three consecutive odds [3, 11, 17].To solve this, we will follow these steps −length:= size of numsif length is same as 1 or length is same as 2, thenreturn Falseotherwise, for i in range 0 to size of nums - 3, doif nums[i], nums[i+1] and nums[i+2] all are odds, thenreturn Truereturn FalseExample (Python)Let ... Read More

Program to find a good string from a given string in Python

Arnab Chakraborty
Updated on 17-May-2021 12:30:33

1K+ Views

Suppose we have a string s with lower and upper case English letters. We shall consider a string is a good string which does not have any two adjacent characters s[i] and s[i + 1] where −0

Program to find kth missing positive number in an array in Python

Arnab Chakraborty
Updated on 17-May-2021 12:30:13

651 Views

Suppose we have an array called nums with positive sorted strictly increasing values, and also have an integer k. We have to find the kth positive integer that is missing from this array.So, if the input is like nums = [1, 2, 4, 8, 12], k = 6, then the output will be 10 because the missing numbers are [3, 5, 6, 7, 9, 10, 11], here the 6th term is 10.To solve this, we will follow these steps −nums := a new set from the elements present in numscount := 0num := 1while count < k, doif num is ... Read More

Program to find number of good triplets in Python

Arnab Chakraborty
Updated on 17-May-2021 12:29:49

2K+ Views

Suppose we have an array nums, and three different integers a, b and c. We have to find the number of good triplets. A triplet (nums[i], nums[j], nums[k]) is said to be a good triplet if the following conditions are true −0

Advertisements