Python Articles

Page 632 of 855

Program to sort all even and odd numbers in increasing and decreasing order respectively in Python

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

Sometimes we need to sort a list where even and odd numbers follow different sorting rules while maintaining their relative positions. This problem requires sorting even numbers in ascending order and odd numbers in descending order. Problem Statement Given a list of numbers, we need to sort the array by maintaining the following criteria ? Even numbers are sorted in ascending order Odd numbers are sorted in descending order The relative positions of even and odd numbers should not be changed For example, if the input is [9, 14, 12, 91, -4, 5], the ...

Read More

Program to find all missing numbers from 1 to N in Python

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

Finding missing numbers from a range is a common programming problem. Given a list of numbers where each number should be in the range [1, N], we need to identify which numbers are missing. Some numbers may appear multiple times while others are missing entirely. So, if the input is like [4, 4, 2, 2, 6, 6], then the output will be [1, 3, 5] since these numbers are missing from the range [1, 6]. Using Array Counting Method This approach uses an auxiliary array to count occurrences of each number ? def find_missing_numbers(nums): ...

Read More

Program to find an element in list whose value is same as its frequency in Python

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

Suppose we have a list of numbers called nums, we have to check whether there is an element whose frequency in the list is same as its value or not. So, if the input is like [2, 4, 8, 10, 4, 4, 4], then the output will be True because the number 4 appears 4 times in the list. Algorithm To solve this, we will follow these steps − Create a frequency map to store each element's count For each key-value pair (element, frequency) in the map, ...

Read More

Program to find the minimum cost to arrange the numbers in ascending or descending order in Python

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

Suppose we have a list of numbers called nums, we have to find the minimum cost to sort the list in any order (ascending or descending). Here the cost is the sum of differences between any element's old and new value. So, if the input is like [2, 5, 4], then the output will be 2. Algorithm To solve this, we will follow these steps − Create a copy of the array nums and sort it Calculate cost for ascending order: sum of absolute differences between original and sorted positions Calculate cost for descending order: ...

Read More

Program to merge two sorted list to form larger sorted list in Python

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

Suppose we have two sorted lists A and B. We have to merge them and form only one sorted list C. The size of lists may be different. For example, suppose A = [1, 2, 4, 7] and B = [1, 3, 4, 5, 6, 8], then merged list C will be [1, 1, 2, 3, 4, 4, 5, 6, 7, 8]. Algorithm We will solve this using the two-pointer technique. The algorithm works as follows ? Initialize an empty result list and two pointers i=0, j=0 While both pointers are within bounds: If ...

Read More

Program to split a list of numbers such that the absolute difference of median values are smallest in Python

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

Suppose we have a list of numbers called nums, we have to divide it into two parts of same size where the absolute difference between each list's median is as small as possible and we have to find this difference. We have to keep in mind that here length of nums / 2 will be odd. So, if the input is like [2, 10, 8, 5, 4, 7], then the output will be 2, as we can make two lists like [2, 5, 10] and [4, 7, 8], then the medians are 5 and 7, their difference is 2. ...

Read More

Program to count how many times we can find "pizza" with given string characters in Python

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

When we need to count how many times we can form the word "pizza" from available characters in a string, we need to check the frequency of each required character. Since "pizza" contains 'p', 'i', 'z', 'z', 'a', we need at least 1 'p', 1 'i', 2 'z', and 1 'a' to form one complete word. So, if the input is like "ihzapezlzzilaop", then the output will be 2. Algorithm To solve this problem, we follow these steps ? Count frequency of 'p' in the string Count ...

Read More

Program to find length of contiguous strictly increasing sublist in Python

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

Suppose we have a list of numbers called nums, we have to find the maximum length of a contiguous strictly increasing sublist when we can remove one or zero elements from the list. So, if the input is like nums = [30, 11, 12, 13, 14, 15, 18, 17, 32], then the output will be 7, as when we remove 18 in the list we can get [11, 12, 13, 14, 15, 17, 32] which is the longest, contiguous, strictly increasing sublist, and its length is 7. Algorithm To solve this, we will follow these steps − ...

Read More

Program to find length of substring with consecutive common characters in Python

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

Finding the length of the longest substring with consecutive identical characters is a common string processing problem. We need to iterate through the string and track the maximum count of consecutive characters. Problem Statement Given a string, find the length of the longest substring containing the same consecutive characters. For example, in the string "abbbaccabbbba", the longest substring with consecutive identical characters is "bbbb" with length 4. Algorithm The approach involves iterating through the string and maintaining two counters ? Track the current consecutive character count Track the maximum consecutive count seen so far ...

Read More

Program to replace each element by smallest term at left side in Python

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

Suppose we have a list of numbers called nums, we have to replace every nums[i] with the smallest element left of i. We have to replace nums[0] with 0. So, if the input is like [15, 7, 9, 16, 12, 25], then the output will be [0, 15, 7, 7, 7, 7]. Algorithm To solve this, we will follow these steps − If nums is empty, then return a new ...

Read More
Showing 6311–6320 of 8,546 articles
« Prev 1 630 631 632 633 634 855 Next »
Advertisements