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
Articles by Arnab Chakraborty
Page 59 of 377
Program to find largest substring between two equal characters in Python
Suppose we have a string s, we have to find the length of the longest substring between two equal letters or elements, excluding the two characters. If we cannot find such substring, then return -1. So, if the input is like s = "level", then the output will be 3 as optimal substrings can be either "lev" or "vel". Algorithm To solve this, we will follow these steps − memo := a new map for i in range 0 to size of s - 1, do if s[i] is in memo, then insert ...
Read MoreProgram to find mean of array after removing some elements in Python
Suppose we have an array called nums, we have to find the mean of the remaining values after removing the smallest 5% and the largest 5% of the elements. So, if the input is like nums = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8], then the output will be 4.0 because after removing smallest and largest values, all remaining elements are the same. Algorithm To solve this problem, we will follow these steps − Sort the array nums n := size of ...
Read MoreProgram to find X for special array with X elements greater than or equal X in Python
A special array is one where there exists a number x such that exactly x elements in the array are greater than or equal to x. We need to find this value x, or return -1 if no such value exists. For example, in the array [4, 6, 7, 7, 1, 0], there are 4 numbers (4, 6, 7, 7) that are greater than or equal to 4, making x = 4 the special value. Algorithm To solve this problem, we follow these steps ? Iterate through all possible values of ...
Read MoreProgram to design parking system in Python
Suppose you want to design a parking system. A parking lot has three different kinds of parking spaces − big, medium, and small. Each size has a fixed number of available slots. We'll create a class called ParkingSystem with two methods ? constructor(big, medium, small) − Takes the number of slots available for different spaces and initializes the ParkingSystem object. addCar(carType) − Checks whether there is a parking space of the given carType for the car that wants to park. The three slot types big, medium, and small are represented by 1, 2, and 3 respectively. ...
Read MoreProgram to find minimum jump needed to return from a folder to home in Python
Suppose we have logs where we have a path to enter into folders. There are different symbols that represent navigation operations − "../": Move to the parent folder from current one. (If we are at main folder, do not change location). "./": Remain in the current folder. "x/": Move to the child folder named x. From the logs we need to find the minimum number of operations needed to come back from the last folder where we stop to the main folder. Problem Understanding If the input is like logs = ["Dir1/", "Dir2/", "../", ...
Read MoreProgram to rearrange spaces between words in Python
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 MoreProgram 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 More