Friday, February 1, 2019

Search For Number Yielding Factorial

  • Problem Description

    Perform Linear search on given set of integers for a number which yields a factorial given by the user.

    First Line of Input is the number of elements
    Second Line of Input is the Number of Elements
    Third Line of Input is the Factorial Value

    Output:
    Print the number which yields the searched factorial values.
  • CODING ARENA
  • a=int(input())

    def factorial(n):
        if(n <= 1):
            return 1
        else:
            return(n*factorial(n-1))

    b=input().split()
    key=int(input())
    flaf=0
    for i in range(a):
        ans=factorial(int(b[i]))
        if(ans==key):
            print(b[i])
            flaf=1
            break
    if(flaf==0):
        print("Not found")
                      

                
  • Test Case 1

    Input (stdin)
    4
    
    5 2 3 1
    
    120
    
    
    Expected Output
    5
  • Test Case 2

    Input (stdin)
    4
    
    2 3 4 5
    
    111
    
    
    Expected Output
    Not found

No comments:

Post a Comment