Check If Queue Elements Are Pairwise Consecutive in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:36:12

243 Views

Suppose we have a queue full of numbers. We have to check whether the consecutive elements in the queue are pairwise consecutive or not.So, if the input is like que = [3, 4, 6, 7, 8, 9], then the output will be True.To solve this, we will follow these steps −q := define a queue and insert all elements from given list into qtemp := a new listwhile q is not empty, doinsert front element of queue into temp and delete front element from queuetemp2 := a new listwhile temp is not empty, doinsert last element of temp into temp2delete ... Read More

Check Divisibility of Product and Sum of First N Natural Numbers in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:34:12

233 Views

Suppose we have a number n. We have to check whether the product of (1*2*...*n) is divisible by (1+2+...+n) or notSo, if the input is like num = 5, then the output will be True as (1*2*3*4*5) = 120 and (1+2+3+4+5) = 15, and 120 is divisible by 15.To solve this, we will follow these steps −if num + 1 is prime, thenreturn falsereturn trueExampleLet us see the following implementation to get better understanding − Live Demodef isPrime(num):    if num > 1:       for i in range(2, num):          if num % i == 0:             return False       return True    return False def solve(num):    if isPrime(num + 1):       return False    return True num = 3 print(solve(num))Input5OutputTrue

Check Product of Digits at Even and Odd Places in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:33:59

315 Views

Suppose we have a number n. We have to check whether the product of odd placed digits and the even placed digits are same or not.So, if the input is like n = 2364, then the output will be True as product of odd placed numbers are 2 * 6 = 12 and product of even placed numbers are 3 * 4 = 12 which are same.To solve this, we will follow these steps −if num < 10, thenreturn Falseodd_place := 1, even_place := 1while num > 0, dod := last digit of numodd_place := odd_place * dnum := quotient ... Read More

Check If Product of Array of Prime Numbers Is a Perfect Square in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:29:40

185 Views

Suppose we have an array nums with all prime numbers. We have to check whether the product of all numbers present in nums is a perfect square or not.So, if the input is like nums = [3, 3, 7, 7], then the output will be True as product of all elements in nums is 441 which is a perfect square as 21^2 = 441.To solve this, we will follow these steps −m := a map containing all elements in nums and their frequenciesfor each key in nums, doif m[key] is odd, thenreturn Falsereturn TrueExampleLet us see the following implementation to ... Read More

Check Possible Movement from Given to Desired Coordinate in C++

Arnab Chakraborty
Updated on 19-Jan-2021 05:29:20

322 Views

Suppose we have two coordinates (sx, sy), and (tx, ty), we have to check whether we can move from starting point to ending point or not. Here we can move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).So if the inputs are (1, 1) and (4, 5), then the answer will be true, this is because move (1, 1) to (2, 1), then (3, 1), then (4, 1), then (4, 5).To solve this, we will follow these steps −while tx > sx and ty > sy, do −if tx > ty, ... Read More

Check if One Number is One's Complement of Another in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:28:55

610 Views

Suppose we have two numbers x and y. We have to check whether one of these two numbers is 1's complement of the other or not. We all know the 1's complement of a binary number is flipping all bits from 0 to 1 or 1 to 0.So, if the input is like x = 9, y = 6, then the output will be True as the binary representations are x = 1001 and y = 0110 which are complement of each other.To solve this, we will follow these steps −z = x XOR yreturn true when all bits in ... Read More

Check If Number is Palindrome in Octal Using Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:22:02

552 Views

Suppose we have a number which is either in octal or in decimal form. If this is in octal form check whether it is palindrome or not. If the number in decimal, then convert it to octal then check whether it is palindrome or not.So, if the input is like num = 178, then the output will be True as the number is not in octal form (8 is not valid symbol in octal but valid in decimal), then convert it to octal which is 262 and this is palindrome.To solve this, we will follow these steps −base := 8 ... Read More

Check If Number Can Be Displayed Using Seven Segment LED in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:21:46

1K+ Views

Suppose we have a number n, and we have another input c. We have to check whether n can be displayed using 7-segment displays or not. Now here is a constraint. We are only allowed to glow at most c number of LEDs.So, if the input is like n = 315 c = 17, then the output will be True as 315 needs 12 LEDs and we have 17.To solve this, we will follow these steps −seg := a list containing led counts for all digits : [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]s := n as ... Read More

Check if n is Strong Prime in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:21:24

705 Views

Suppose we have a number n. We have to check whether n is a strong prime or not. As we know a number said to be strong prime when it is a prime number that is greater than the average of nearest prime numbers.So, if the input is like num = 37, then the output will be True as nearest prime numbers are 31 and 41, the average is (31+41)/2 = 36. And 37 > 36.To solve this, we will follow these steps −if num is not prime or num is 2, thenreturn Falselast := num - 1, next := ... Read More

Check If n is Divisible by Power of 2 in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:17:07

430 Views

Suppose we have two numbers x and n. We have to check whether x is divisible by 2^n or not without using arithmetic operators.So, if the input is like x = 32 n = 5, then the output will be True as 32 = 2^5.To solve this, we will follow these steps −if x AND (2^n - 1) is 0, thenreturn Truereturn FalseExampleLet us see the following implementation to get better understanding − Live Demodef solve (x, n):    if (x & ((1

Advertisements