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. Algorithms
  3. Bit Manipulation
  4. Iterate It

Iterate It

Problem
Submissions
Leaderboard
Discussions
Editorial

Consider the following pseudocode, run on an array of length :

rep := 0
while A not empty:
    B := []
    for x in A, y in A:
        if x != y: append absolute_value(x - y) to B
    A := B
    rep := rep + 1

Given the values of and array , compute and print the final value of after the pseudocode above terminates; if the loop will never terminate, print -1 instead.

Input Format

The first line contains a single integer, , denoting the length of array .
The second line contains space-separated integers describing the respective values of .

Constraints

Output Format

Print the final value of after the pseudocode terminates; if the loop will never terminate, print -1 instead.

Sample Input 0

3
1 3 4

Sample Output 0

4

Explanation 0

After the first loop, becomes . After the second loop, the array only contains 's and 's. After the third loop, the array only contains 's. After the fourth loop, the array is empty. Because the value of is incremented after each loop, at the time the loop terminates. Thus, we print 4 as our answer.

Author

Xellos0

Difficulty

Expert

Max Score

90

Submitted By

2360

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

Join us

Create a HackerRank account

Be part of a 26 million-strong community of developers

Please signup or login in order to view this challenge

or
Already have an account?Log in