+ 1

Write a function that, given an array A of N integers, returns the largest integer K > 0 such that both values K and -K exist in

Write a function that, given an array A of N integers, returns the largest integer K > 0 such that both values K and -K exist in array A. If there is no such integer, the function should return 0. How do I write code for this question?

27th Jul 2021, 10:45 AM
Gouri Shinde
Gouri Shinde - avatar
2 ответов
+ 1
I would take two sets , append positive to one and -(negative) to other , intersect the both . After that check if set length is 1 or more or empty and apply max , return 0 or return the only value from set accordingly.
27th Jul 2021, 12:11 PM
Abhay
Abhay - avatar
+ 3
Steps: 1. Take the list A as input 2. Sort the numbers 3. K = 0 4. Let the first positive integer be a candidate for K, if that number satisfies the condition to be K then it'll be our new K 5. Now again select the 2nd positive integer to be a candidate for K and repeat (4) 6. Continue this till the end of the list 7. Return K
27th Jul 2021, 11:37 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar