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
Server Side Programming Articles
Page 375 of 2109
Program to find sum of all odd length subarrays in Python
Given an array of positive integers, we need to find the sum of all possible odd-length subarrays. A subarray is a contiguous subsequence of the array. For example, if the input is nums = [3, 8, 2, 5, 7], the odd-length subarrays are ? Length 1: [3], [8], [2], [5], [7] → sums: 3, 8, 2, 5, 7 Length 3: [3, 8, 2], [8, 2, 5], [2, 5, 7] → sums: 13, 15, 14 Length 5: [3, 8, 2, 5, 7] → sum: 25 Total sum: 3+8+2+5+7+13+15+14+25 = 92 Approach We iterate ...
Read MoreProgram to find number of special positions in a binary matrix using Python
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 like ? 1 0 ...
Read MoreProgram to replace all question symbols to avoid consecutive repeating characters in Python
When working with strings containing question marks, we often need to replace them with letters to avoid consecutive repeating characters. This problem requires us to replace all '?' characters in a string with lowercase letters such that no two adjacent characters are the same. So, if the input is like s = "hel??", then the output will be "helab". The first question mark can be anything except 'l', and the second one can be anything except 'a'. Algorithm To solve this problem, we will follow these steps − Handle the single character ...
Read MoreProgram to find diagonal sum of a matrix in Python
A square matrix has two main diagonals: the primary diagonal (from top-left to bottom-right) and the secondary diagonal (from top-right to bottom-left). To find the diagonal sum, we add all elements from both diagonals while avoiding double-counting the center element in odd-sized matrices. Problem Example Consider this 4×4 matrix ? 10 5 9 6 8 15 3 2 3 8 12 3 2 11 7 3 The primary diagonal elements are [10, 15, 12, 3] with sum = 40. The secondary diagonal elements are ...
Read MoreProgram to check pattern of length m repeated K or more times exists or not in Python
Suppose we have an array nums with positive values, we have to find a pattern of length m that is repeated k or more 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 MoreProgram to find most visited sector in a circular track using Python
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) ...
Read MoreProgram to find number with thousand separator in Python
When working with large numbers, it's often helpful to format them with thousand separators for better readability. Python provides several ways to add commas as thousand separators to numbers. So, if the input is like n = 512462687, then the output will be "512, 462, 687" Method 1: Using Built-in format() Function The simplest approach is to use Python's built-in format() function with the comma separator ? def format_number(n): return format(n, ', ') n = 512462687 result = format_number(n) print(result) print(type(result)) 512, 462, 687 ...
Read MoreProgram to check three consecutive odds are present or not in Python
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]. Algorithm To solve this, we will follow these steps − Get the length of nums If length is 1 or 2, return False (impossible to have 3 consecutive elements) ...
Read MoreProgram to find a good string from a given string in Python
Suppose we have a string s with lower and upper case English letters. We consider a string is a good string which does not have any two adjacent characters s[i] and s[i + 1] where ? 0
Read MoreProgram to find kth missing positive number in an array in Python
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. Approach To solve this, we will follow these steps − Convert the array to a set for O(1) ...
Read More