Home » Python » Competitive Coding Questions

Python Power Challenge | Competitive Coding Questions

Python Competitive Coding Questions | Power Challenge: This practice is based on Power Challenge in Python programming language.
Submitted by Prerana Jain, on April 24, 2020

Question:

A power function is that positive number that can be expressed as x^x i.e x raises to the power of x, where x is any positive number. You will be given an integer array A and you need to print if the elements of array A are Power Number or not.

Input:

The first line contains N, a positive integer. The next line contains N spaces – separated integers.

    3
    1 3 4 

Output:

For every integer print "Yes" if it’s a power number else print "No". these outputs must be separated by spaces.

    Yes  No  Yes

Constraints:

    1 <= N  <= 100
    1 <= A[i] <= 10^16

Explanation/Solution:

In this question, we have to check that the given numbers in an array are power numbers or not. We will find it with the help of a power function in python. So we can do this by comparing each element with the power number so first we take input from the user and create the array of size N. then make another array that stores the power number from 1 to 14. The key focus of this question is on constraints. In this question, the constraints are given till 10^16 numbers.

To solve this question, we are using Python3.

Code:

# input N
N = int(input())

# input N array elements
Arr = list(map(int, input().split()))

# create another blank/empty array
Brr = []
for i in range(15):
    # Append the power numbers in blank  array
    Brr.append(pow(i, i))
for i in range(N):
    # compare the give array with 
    # power number array  one by one
    if(Arr[i]  in  Brr):
        # if it matches print Yes other wise NO
        print('Yes', end=' ')  
    else:
        print('No', end=' ')   

Output

RUN 1:
5
1 4 3 16 256
Yes Yes No No Yes

RUN 2:
5
2 4 8 16 32
No Yes No No No





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.