Found 10476 Articles for Python

Check if array can be sorted with one swap in Python

Yaswanth Varma
Updated on 08-Aug-2025 17:10:14

337 Views

Sorting is a task used to organize the numbers in increasing order. Usually, we use the sorting algorithms to do this, but in most cases, the array is almost sorted, with two or three numbers in the wrong position. In such scenarios, instead of sorting the entire array, we can check if swapping just one pair of elements will make the entire array sorted. Checking if Array can be Sorted with One Swap The given task is to check if an array can be sorted with one swap in Python. i.e., given an array with distinct integers, we need ... Read More

Check if array can be divided into two sub-arrays such that their absolute difference is Ks in Python

Yaswanth Varma
Updated on 28-Aug-2025 12:40:37

119 Views

In this article we are going to check whether the given array can be divided into two sub-array in such a way that the absolute difference between the sums of these two sub-array is equal to the given value k. The absolute difference is nothing but the non-negative gap between two numbers, calculated as abs(sum1-sum2). In this task the split must produce two non-empty sub-array i.e. at least one element should be present in each part. Example scenarios Following are the example scenarios: Scenario 1 Input: array = [3, 1, 4, 2, 2], K = 4 ... Read More

Check if any square (with one colored cell) can be divided into two equal parts in Python

Yaswanth Varma
Updated on 08-Aug-2025 16:17:50

188 Views

Dividing a Square into two Equal Parts In this article, we are given a square of size n*n with exactly one cell coloured. The task is to determine whether this square can be divided into two equal splits by making a single straight cut along the square, ensuring that the coloured cell lies entirely within one of the splits. Two equal splits here indicate that both parts must contain the same number of cells, which is possible if the square side length is even. In this article we are not only checking whether the split is possible but also whether the ... Read More

Check if any permutation of N equals any power of K in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:09:06

167 Views

Suppose, we have two positive integers n and m, such that 2 ≤ n ≤ 1018 and 2 ≤ m ≤ n. Our goal is to find out if there are any all-digit-permutations of the number n; so that it is equal to some power of m. If there is one, we state that there exists an all-digit-permutation of n that is equal to a power of m, otherwise we state the previous statement as false.For example, we are given n = 7182 and m = 12. As 1728 is an all-digit-permutation of 7182 and 1728 = 12^3, we state ... Read More

Check if any permutation of a number is divisible by 3 and is Palindromic in Python

Yaswanth Varma
Updated on 07-Aug-2025 18:49:53

198 Views

Divisibility by 3 The given task is to determine whether the permutation of the digits of the given number can form a new number that satisfies two conditions: it must be divisible by 3 and also be a palindromic number. A number is said to be palindromic if the reversal of a number is the same as the original, such as 121 or 1331, and a number is said to be divisible by 3 if the sum of its digits is divisible by 3. For example, let's look at the following scenarios:  Scenario 1 Input: 121 Output: False Explanation: ... Read More

Check if any permutation of a large number is divisible by 8 in Python

Yaswanth Varma
Updated on 07-Aug-2025 18:38:46

933 Views

Divisibility By 8 According to the divisibility rule of 8, if the last three digits of a number are divisible by 8, then the entire number is divisible by 8. While this is easy to check for small numbers, when it comes to large numbers, generating all the possible permutations is impractical for large inputs. However, we can solve this problem by taking advantage of the divisibility rule and limiting our check to possible combinations of three digits. Scenario 1 Input: "61" Output: Yes Explanation: For the given input, the possible permutations of the digits are 16 and 61. Among ... Read More

Check if any large number is divisible by 19 or not in Python

Yaswanth Varma
Updated on 07-Aug-2025 13:10:58

415 Views

In mathematics, Divisibility helps us to determine whether a number can be divided by another without leaving a remainder. For example, 6 is divisible by 2 because 6/2 = 3 with no remainder. In this article, we are going to learn how to check if any large number is divisible by 19 or not in Python. Checking if any Large Number is Divisible by 19 Checking for the divisibility with small numbers can be easily handled by using the standard arithmetic operations. While dealing with the large numbers (contains 20, 30 or even 100 digits), the programming languages may run ... Read More

Check if any large number is divisible by 17 or not in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:01:12

700 Views

Suppose, we are given a number and we have to check whether the number is divisible by 17.So, if the input is like 99943, then the output will be Divisible.We will solve this problem using the repeated subtraction method, where we extract the last digit of the number and subtract it 5 times from the number until we get a two-digit number that is divisible by 17.To solve this, we will follow these steps −while number is divisible by 100, dolast_digit := number mod 10number := floor value of (number divided by 10)number := number - last_digit * 5return true ... Read More

Check if any interval completely overlaps the other in Python

Yaswanth Varma
Updated on 07-Aug-2025 12:59:50

2K+ Views

In many programming problems, Intervals are used to represent ranges such as time periods, numeric spans, etc. An interval is typically represented as a pair of numbers (start, end) where start

Check if any anagram of a string is palindrome or not in Python

Yaswanth Varma
Updated on 30-Jul-2025 18:50:48

491 Views

Check if any Anagram of a String is Palindrome An anagram is a rearrangement of the characters of a word or a phrase to generate a new word, using all the original characters exactly once. For example, thing and night are anagrams of each other. While palindrome is a word or phrase that reads the same forward and backward, like madam, racecar. In this article, we are going to check if any anagram of a string is a palindrome or not, i.e., we are not just checking whether the given string itself is a palindrome, but also whether it can ... Read More

Advertisements