Programming Articles

Page 379 of 2547

Program to find diagonal sum of a matrix in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 9K+ 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 292 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 324 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 576 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 739 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

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 689 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. Approach To solve this, we will follow these steps − Convert the array to a set for O(1) ...

Read More

Program to shuffle string with given indices in Python

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

Suppose we have a string s and a list of indices ind, they are of same length. The string s will be shuffled such that the character at position i moves to indices[i] in the final string. We have to find the final string. So, if the input is like s = "ktoalak" and ind = [0, 5, 1, 6, 2, 4, 3], then the output will be "kolkata". Original String: Index: Char: 0 ...

Read More

Program to count odd numbers in an interval range using Python

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

Suppose we have two non-negative numbers left and right. We have to find the number of odd numbers between left and right (inclusive). So, if the input is like left = 3, right = 15, then the output will be 7 because there are 7 odd numbers in range, these are [3, 5, 7, 9, 11, 13, 15], there are 7 elements. Algorithm To solve this, we will follow these steps: If left is odd or right is odd, then ...

Read More

Program to find maximum how many water bottles we can drink in Python

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

Suppose there are n number of full water bottles, and we can exchange m empty water bottles for one full water bottle. Drinking a full water bottle makes it an empty bottle. We have to find the maximum number of water bottles we can drink. So, if the input is like n = 9, m = 3, then the output will be 13 because initially we have 9 bottles. After drinking all bottles, we get 9/3 = 3 full bottles from the empty ones. After drinking those 3, we have 3 empty bottles which gives us 1 more full ...

Read More
Showing 3781–3790 of 25,466 articles
« Prev 1 377 378 379 380 381 2547 Next »
Advertisements