Articles on Trending Technologies

Technical articles with clear explanations and examples

Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python

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

We need to check if an array contains consecutive integers in O(n) time and O(1) space complexity. This algorithm works with both positive and negative numbers by using the arithmetic progression sum formula. Algorithm Approach The solution uses the mathematical property that consecutive integers form an arithmetic progression with common difference 1. We can verify if numbers are consecutive by comparing the actual sum with the expected sum of an arithmetic progression. Step-by-Step Process Find the minimum element (first term of AP) Calculate expected sum using AP formula: n/2 × (2a + (n-1)d) where d=1 ...

Read More

Check if array contains contiguous integers with duplicates allowed in Python

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 610 Views

A sequence of numbers is said to be contiguous if the numbers can be arranged in such a way that each number follows the previous one without any gaps. For example, [11, 12, 13, 14] is a contiguous sequence because there are no missing numbers between the smallest and largest values. In this article, we need to check if an array contains such contiguous integers, even when duplicates are present. For instance, [11, 12, 13, 13, 14] should still be considered contiguous because the unique numbers are [11, 12, 13, 14], which form a proper sequence without breaks. ...

Read More

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

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

This problem asks us to determine if any permutation of the digits of number n equals some power of number m. For example, given n = 7182 and m = 12, we find that 1728 (a permutation of 7182) equals 12³. Approach We'll generate all powers of m within our range, then check if any power has the same digit frequency as n using sets for comparison ? Helper Function to Check Digit Permutation First, we create a function to check if two numbers have the same digits ? def check_power(n, m): temp_arr_1 = [] ...

Read More

Check if array can be sorted with one swap in Python

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 438 Views

Sorting is a task used to organize numbers in increasing order. Usually, we use sorting algorithms to do this, but in most cases, the array is almost sorted, with only 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. Problem Definition Given an array with distinct integers, we need to find if it can become completely sorted by swapping only one pair of elements. We are not performing the swap, just checking if ...

Read More

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

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 205 Views

In this article we are going to check whether the given array can be divided into two sub-arrays in such a way that the absolute difference between the sums of these two sub-arrays 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-arrays 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 ...

Read More

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

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 279 Views

In this article, we are given a square of size n×n with exactly one cell colored. The task is to determine whether this square can be divided into two equal parts by making a single straight cut, ensuring that the colored cell lies entirely within one of the parts. Two equal parts means both splits must contain the same number of cells, which is only possible if the square side length is even. We need to check if the position of the colored cell allows such a split without being intersected by the cut. Understanding the Problem ...

Read More

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

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 306 Views

This task involves checking if any permutation of a given number's digits can form a new number that is both divisible by 3 and palindromic. A number is divisible by 3 if the sum of its digits is divisible by 3, and a number is palindromic if it reads the same forwards and backwards. Understanding the Problem For a number to be divisible by 3, the sum of its digits must be divisible by 3. For it to be palindromic, the number must read the same when reversed. We need to check if any permutation of the input ...

Read More

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

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 1K+ Views

A number is divisible by 8 if its last three digits form a number divisible by 8. For large numbers, checking all permutations is impractical, but we can use this divisibility rule to efficiently check if any permutation is divisible by 8. Divisibility Rule for 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. We can solve this problem by checking if we can form any three-digit combination from the given digits that is divisible by 8. Examples ...

Read More

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

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 497 Views

In mathematics, divisibility helps us 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 will learn how to check if any large number is divisible by 19 in Python using the modulus operator. Why Python Handles Large Numbers Well Checking divisibility with small numbers is straightforward using standard arithmetic operations. However, when dealing with very large numbers (containing 20, 30, or even 100 digits), many programming languages encounter issues due to integer size limits. ...

Read More

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

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

Checking if a large number is divisible by 17 can be efficiently done using a repeated subtraction method. Instead of performing direct division, we extract the last digit and subtract it multiplied by 5 from the remaining number until we get a manageable two-digit number. The algorithm works because of the mathematical property that a number is divisible by 17 if and only if the number formed by removing the last digit and subtracting 5 times the last digit is also divisible by 17. Algorithm Steps To solve this problem, we follow these steps: While ...

Read More
Showing 5761–5770 of 61,297 articles
« Prev 1 575 576 577 578 579 6130 Next »
Advertisements