Programming Articles

Page 296 of 2547

Program to find how many swaps needed to sort an array in Python

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

Finding the minimum number of swaps needed to sort an array is a classic problem. We need to consider both ascending and descending order, then return the minimum swaps required. So, if the input is like nums = [2, 5, 6, 3, 4], then the output will be 2 because initially nums has [2, 5, 6, 3, 4]. If we swap numbers 6 and 4, the array will be [2, 5, 4, 3, 6]. Then, if we swap the numbers 5 and 3, the array will be [2, 3, 4, 5, 6]. So 2 swaps are needed to make ...

Read More

Program to find maximum value at a given index in a bounded array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 456 Views

Suppose we have three values: n, index and maxSum. We need to construct an array nums and find the maximum possible value at nums[index] that satisfies the following conditions: Size of nums is n All elements in nums are positive |nums[i] - nums[i+1]|

Read More

Program to find partition array into disjoint intervals in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 331 Views

Given an array nums, we need to partition it into two disjoint subarrays called left and right such that: Each element in left subarray is less than or equal to each element in right subarray Both left and right subarrays are non-empty Left subarray has the smallest possible size We have to find the length of left after such a partitioning. So, if the input is like nums = [5, 0, 3, 8, 6], then the output will be 3 because left array will be [5, 0, 3] and right subarray will be [8, 6]. ...

Read More

Program to find maximum number of consecutive values you can make in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 527 Views

Suppose we have an array called coins with n elements, representing the coins that we own. The value of the ith coin is denoted as coins[i]. We can make some value x if we can select some of our n coins such that their values sum up to x. We have to find the maximum number of consecutive values that we can get with the coins starting from and including 0. So, if the input is like coins = [1, 1, 3, 4], then the output will be 10, because ? 0 = [] (empty selection) 1 ...

Read More

Program to find out the number of accepted invitations in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 434 Views

This is a bipartite matching problem where we need to find the maximum number of boy-girl pairs for a party. Each boy can invite multiple girls, but each girl can accept only one invitation. We'll use a depth-first search (DFS) approach to solve this matching problem. Problem Understanding Given an m × n matrix where m boys invite n girls: Matrix[i][j] = 1 means boy i sent an invitation to girl j Matrix[i][j] = 0 means no invitation was sent Each girl can accept at most one invitation We need to find the maximum number of ...

Read More

Program to find out the number of pairs of equal substrings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 422 Views

Suppose we are given two strings, both made of lowercase alphabets. We have to find out the number of quadruples (p, q, r, s) satisfying the given conditions − 0

Read More

Program to sort out phrases based on their appearances in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 195 Views

Suppose we are given two lists: phrases that contains selected phrases and sentences that contains several sentences. We need to find which phrases appear in the sentences and sort the phrases based on their frequency of appearances. The phrase with the most appearances comes first. So, if the input is like phrases = ['strong', 'durable', 'efficient'], sentences = ['the product is durable and efficient', 'strong and durable', 'it is efficient', 'like it because it is efficient'], then the output will be ['efficient', 'durable', 'strong'] The phrase 'efficient' appears in 3 sentences (indices 0, 2, and 3), making it ...

Read More

Program to find out the buildings that have a better view in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 386 Views

Suppose, we are provided with an array that contains the heights of different buildings. The buildings are located on a line, and a building has a better view if it is not obstructed by another taller building. So provided the array containing heights, we have to find out the buildings that do not have other taller buildings to obstruct the view from them. The indices are returned from the array that satisfies the criteria. So, if the input is like height = [5, 6, 8, 7], then the output will be [2, 3]. The buildings in array index 0 ...

Read More

Program to design a queue that moves the most recently used element to the end of it in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 528 Views

We need to design a queue that moves the most recently used element to the end. The queue is initialized with integers 1 to n, and we implement a function that moves an element from a given position to the end while returning that element's value. For example, with n = 5 (queue contains [1, 2, 3, 4, 5]) and move operations at positions 5, 2, 3, and 1, the output will be 5, 2, 4, 1 respectively. Algorithm Steps To solve this efficiently, we use a square root decomposition approach − Find the correct ...

Read More

Program to find out the sum of the maximum subarray after a operation in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 258 Views

Given an array of integers, we can perform one operation where we replace any element array[i] with its squared value array[i] * array[i]. Our goal is to find the maximum possible subarray sum after performing this operation. So, if the input is like array = [4, 1, -2, -1], then the output will be 17. If we replace the value in array[0] with its squared value, the array becomes [16, 1, -2, -1]. The maximum subarray from this is [16, 1], which has the sum 16 + 1 = 17. Algorithm We use dynamic programming with ...

Read More
Showing 2951–2960 of 25,466 articles
« Prev 1 294 295 296 297 298 2547 Next »
Advertisements