We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Functional Programming
  3. Functional Structures
  4. Lists and GCD

Lists and GCD

Problem
Submissions
Leaderboard
Discussions
Editorial

We call an integer a prime number (or simply a prime) if its only positive divisors are and .

Fundamental theorem of arithmetic states: Every positive integer can be uniquely expressed as a product of power of primes, i.e.,

where,

  • is the prime, i.e.,

Greatest common divisor of two positive integers

For two positive integers, and , whose prime factorization is represented as



We calculate the greatest common divisor, , as


Greater common divisor of a list of numbers

Greatest common factor of a list of positive integers, , is represented as


Finite representation of prime factorization

Since primes are infinite, it is not possible to store factors in the form provided above. To that end, we will only consider those prime factors () whose power is greater than zero (). That is:

, where

  • ; for rest

And we will represent them as following:

For example:


Challenge

You are given the elements of list, , in the representation provided above. Find its greatest common divisor, i.e., .

Input Format

First line contains an integer, , which is the size of list, .
Then follows lines, where line represents the factors of element of ,

Output Format

Print one line representing the greatest common divisor of () in the above representation.

Constraints

  • All other integers lie in
  • Total number of prime factors of an element

Notes

  • Test cases are designed such that will always be greater than .

Sample Input #00

2
7 2
2 2 7 1

Sample Output #00

7 1

Sample Input #01

4
2 2 3 2 5 3
3 2 5 3 11 1
2 2 3 3 5 4 7 6 19 18
3 10 5 15

Sample Output #01

3 2 5 3

Explanation

Test case #00: . Therefore .
Test case #01: .

Author

abhiranjan

Difficulty

Easy

Max Score

20

Submitted By

1734

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website