Programming Articles

Page 468 of 2547

Check if the elements of stack are pairwise sorted in Python

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

Suppose we have a stack of numbers; we have to check whether values in the stack are pairwise consecutive or not. These pairs can be increasing or decreasing. If the stack has an odd number of values, the top element is left out of a pair. We should retain the original stack content after checking. To solve this problem, we can use basic stack operations: push, pop and check if the stack is empty. Example If the input is like stk = [5, 6, -4, -5, 12, 11, 6, 7, 22], then the output will be True. ...

Read More

Check if the characters of a given string are in alphabetical order in Python

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

Suppose we have a string s. We have to check whether characters in s are in alphabetical order or not. So, if the input is like s = "mnnooop", then the output will be True. Method 1: Using Sorting Approach To solve this, we will follow these steps ? char_arr := a new list from the characters present in s sort the list char_arr return char_arr is same as list of all characters in s then true otherwise false Example def solve(s): char_arr = list(s) ...

Read More

Check if the characters in a string form a Palindrome in O(1) extra space in Python

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

A palindrome reads the same forwards and backwards. When checking if a string forms a palindrome, we often need to ignore non-letter characters and focus only on alphabetic characters. This problem requires solving it in O(1) extra space, meaning we cannot create additional data structures. So, if the input is like s = "ra$5ce58car", then the output will be True, as the letters form "racecar" which is a palindrome. Algorithm We use a two-pointer approach with helper functions to find the next valid letter from both ends ? Use two pointers: left (start) and right ...

Read More

Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python

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

When working with binary representations, we sometimes need to check if consecutive blocks of 0s and 1s have the same length. This means that all groups of consecutive identical bits should have equal sizes. For example, if we have the number 455, its binary representation is 111000111. Here we have three consecutive 1s, followed by three consecutive 0s, followed by three consecutive 1s − all blocks have the same length of 3. Problem Statement Given a number, we need to check whether its binary representation has the same number of consecutive blocks of 0s and 1s. Note ...

Read More

Check if the array is beautiful in Python

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

A beautiful array is defined as an array of unique elements that satisfies two specific conditions. In this tutorial, we'll learn how to check if an array meets the criteria for being "beautiful" in Python. Conditions for a Beautiful Array An array is considered beautiful if it meets these requirements: All elements are in the range 1 to n (where n is the array length) The array is not sorted in ascending order All elements are unique (no duplicates) Algorithm Steps To check if an array is beautiful, we follow these steps: ...

Read More

Check if the array has an element which is equal to sum of all the remaining elements in Python

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

Sometimes we need to check if an array contains an element whose value equals the sum of all other elements. This problem can be solved efficiently by calculating the total sum and using a frequency map. For example, in the array [3, 2, 10, 4, 1], the element 10 equals the sum of remaining elements: 3 + 2 + 4 + 1 = 10. Algorithm Approach The key insight is that if an element equals the sum of all other elements, then that element must be exactly half of the total array sum ? Calculate ...

Read More

Check if the array has an element which is equal to product of remaining elements in Python

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

In this problem, we need to check if an array contains an element whose value equals the product of all other elements in the array. For example, if we have nums = [3, 2, 24, 4, 1], the output will be True because 24 = (3*2*4*1). Algorithm To solve this problem, we follow these steps ? Calculate the total product of all elements For each element, check if it equals the total product divided by itself Return True if any element satisfies this ...

Read More

Check if sums of i-th row and i-th column are same in matrix in Python

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

In matrix operations, we often need to compare row and column sums. This problem asks us to check if the sum of the i-th row equals the sum of the i-th column for any row-column pair in a given matrix. Problem Understanding Given a matrix, we need to verify if there exists at least one index i where the sum of row i equals the sum of column i. For the example matrix: 2345 10642 1467 1567 Row 0 sum = 2 + 3 + ...

Read More

Check if sum of divisors of two numbers are same in Python

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

Sometimes we need to check whether two numbers have the same sum of divisors. This problem involves finding all proper divisors (excluding the number itself) of both numbers and comparing their sums. For example, if we have p = 559 and q = 703: Divisors of 559: 1, 13, 43 (sum = 57) Divisors of 703: 1, 19, 37 (sum = 57) Since both sums equal 57, the numbers have the same divisor sum. Algorithm To solve this efficiently, we'll: Create a function to calculate the sum of proper divisors Iterate only up ...

Read More

Check if suffix and prefix of a string are palindromes in Python

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

A string has palindromic prefix and suffix when both the beginning and end portions read the same forwards and backwards. This problem requires finding if a string contains at least one palindrome as a prefix and at least one palindrome as a suffix. For example, in the string "levelishighforracecar", the prefix "level" and suffix "racecar" are both palindromes. Algorithm The solution follows these steps ? Find the first palindromic prefix of length ≥ 2 If no palindromic prefix exists, return False Find any ...

Read More
Showing 4671–4680 of 25,466 articles
« Prev 1 466 467 468 469 470 2547 Next »
Advertisements