Server Side Programming Articles

Page 375 of 2109

Program to rearrange spaces between words in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 475 Views

Sometimes we need to rearrange spaces between words to create uniform spacing. This involves redistributing all spaces evenly between words, with any extra spaces placed at the end. Given a string with words separated by varying numbers of spaces, we need to rearrange the spaces so that there are equal spaces between every pair of adjacent words. If we cannot redistribute all spaces equally, the extra spaces go at the end. Example If the input is " I love programming ", the output will be "I love programming ". The original ...

Read More

Program to find sum of all odd length subarrays in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 607 Views

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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 380 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 like ? 1 0 ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 538 Views

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 More

Program to find diagonal sum of a matrix in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 10K+ Views

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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 306 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 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
Arnab Chakraborty
Updated on 25-Mar-2026 348 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) ...

Read More

Program to find number with thousand separator in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 587 Views

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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 757 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]. 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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More
Showing 3741–3750 of 21,090 articles
« Prev 1 373 374 375 376 377 2109 Next »
Advertisements