Given an array of numbers and a target value K, we need to check if we can transform the array to have sum K by applying exactly one of three operations to each element: Make the number negative Add its index (1-based) to the number Subtract its index from the number For example, with nums = [1, 2, 3, 7] and k = 8, we can subtract indices from elements 2 and 3 to get [1, 0, 0, 7] with sum 8. Approach Using Dynamic Programming We use memoization to explore all possible combinations ... Read More
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
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
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
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance