Programming Articles

Page 378 of 2547

Program to sort array by increasing frequency of elements in Python

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

Suppose we have an array with some elements where elements may appear multiple times. We have to sort the array such that elements are sorted according to their increasing frequency. Elements that appear fewer times will come first, and when frequencies are equal, larger elements come first. So, if the input is like nums = [1, 5, 3, 1, 3, 1, 2, 5], then the output will be [2, 5, 5, 3, 3, 1, 1, 1] Algorithm Steps To solve this, we will follow these steps ? Create a frequency map to count occurrences of ...

Read More

Program to find largest substring between two equal characters in Python

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

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 More

Program to find mean of array after removing some elements in Python

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

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 More

Program to find X for special array with X elements greater than or equal X in Python

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

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 More

Program to design parking system in Python

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

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 More

Program to find minimum jump needed to return from a folder to home in Python

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

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 More

Program to rearrange spaces between words in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 461 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 572 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 358 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 514 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
Showing 3771–3780 of 25,466 articles
« Prev 1 376 377 378 379 380 2547 Next »
Advertisements