Server Side Programming Articles - Page 1377 of 2650

Check if binary string multiple of 3 using DFA in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:26:28

2K+ Views

Suppose we have an array n that represents a binary representation of any number. We have to check whether its binary representation is divisible by three or not by using Deterministic Finite Automata DFA.So, if the input is like n = [1, 1, 0, 0] (binary of 12), then the output will be True.To solve this, we can construct DFA like below −The approach is simple when a number is divisible by 3 then the remainder will be 0, if not then remainder will be 1 or 2. There are three states for these three remainders. The initial state is ... Read More

Check if binary representation of a number is palindrome in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:22:22

309 Views

Suppose we have a number n. We have to check whether the binary representation of n is palindrome or not.So, if the input is like n = 9, then the output will be True as binary representation of 9 is 1001, which is palindrome.To solve this, we will follow these steps −ans := 0while num > 0, doans := ans * 2if num is odd, thenans := ans XOR 1num := num / 2return ansLet us see the following implementation to get better understanding −Example Live Demodef reverse_binary(num) :    ans = 0    while (num > 0) :     ... Read More

Check if array sum can be made K by three operations on it in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:20:44

144 Views

Suppose we have a list of numbers called nums and also a positive value K. We can perform any of these three operations on nums −Make one number negative, Add index (start from index 1) of the number to the number itselfSubtract index of the number from the number itself.Finally, we have to check whether the given array can be transformed as the sum of the array becomes k, by performing these operations only once on each element.So, if the input is like nums = [1, 2, 3, 7] k = 8, then the output will be True as we ... Read More

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

Arnab Chakraborty
Updated on 30-Dec-2020 13:18:32

211 Views

Suppose we have an array of unsorted numbers called nums. We have to check whether it contains contiguous values or not, it should support negative numbers also.So, if the input is like nums = [-3, 5, 1, -2, -1, 0, 2, 4, 3], then the output will be true as the elements are 3, 4, 5, 6, 7, 8.To solve this, we will follow these steps −size := size of numsinit_term := inffor i in range 0 to size, doif nums[i] < init_term, theninit_term := nums[i]ap_sum := quotient of ((size * (2 * init_term + (size - 1) * 1)) ... Read More

Check if array elements are consecutive in Python

SaiKrishna Tavva
Updated on 20-Jan-2025 18:26:42

6K+ Views

Suppose we have an array of numbers called nums. We have to check whether it contains contiguous values or not. So, if the input is like nums = [6, 8, 3, 5, 4, 7], then the output will be true as the elements are 3, 4, 5, 6, 7, 8. Following is the sample example to check if elements of an array are consecutive. def solve(nums): if len(nums) < 1: return False min_val = min(nums) max_val = max(nums) if max_val - min_val ... Read More

Check if array contains contiguous integers with duplicates allowed in Python

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

489 Views

Check for Contiguous Integers in an Array 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 the duplicates are present. For instance, [11, 12, 13, 13, 14] should still be considered contiguous because the unique numbers are [11, 12, 13, ... Read More

Check if array can be sorted with one swap in Python

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

348 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

126 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 two intervals overlap among a given set of intervals in C++

Arnab Chakraborty
Updated on 30-Dec-2020 13:11:59

933 Views

Suppose, we are given a set of intervals that consists of values (time1, time2) where time1 represents the starting time, and time2 represents the ending time of an event. Our task is to check whether any of these intervals overlap any other interval in this set. If any of the intervals overlap, we return the result as True, otherwise we return False.So, if the input is like [(4, 7), (5, 11), (7, 11), (5, 8)] then the output will be True.To solve this, we will follow these steps −sort the list inputArrfor i in range 1 to size of inputArr, ... 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

197 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

Advertisements