-2

Given three sorted arrays say A,B and C. The range of values of A and B are <10^5,while for C the range is upto 10^10 but all C elements are perfect squares.. Count all the pairs of A and B such that there product is equal to any element of C. I tried doing it but the complexity goes to o(n^2) and i can't reduce it,Any suggestion on how to proceed? Eg:A: [1, 3, 9, 14] B:[4, 12, 49] C: [36, 49, 121] Answer:3 1 from A and 49 from B similarly 3*12 and 4*9

Coder doit
  • 25
  • 5
  • Why do you worry about O(n^2)? You have 10^10 C elements, so your environment can handle that. All pairs of A and B are also just 10^10 given that A and B are no more than 10^5 themselves. So it does no seem more difficult than computing the C values themselves. – Andrei May 18 '17 at 19:44
  • array A, B and C can have 10^5 elements each so o(n^2) won't run within 1 sec – Coder doit May 18 '17 at 19:52
  • @Andrei I think it is the value of the elements which goes up to 10^5 and 10^10, not the number of elements. The number of elements, at least in the sample input is 4,3 and 3. The reason for worrying about O(n^2) is simple, the question requires to be faster. Could you elaborate your statement on that foundation? – Yunnosch May 18 '17 at 20:10
  • range of values of elements of A and B 0 – Coder doit May 18 '17 at 20:12
  • @Yunnosch, thank you. I've misread the question. Coder doit, how many values do you have approximately in each array, in the real problem? – Andrei May 18 '17 at 20:17
  • Is the root of C elements known or is it OK to calculate it? Would it help to determine the prime factors of A and B and first list the possible squares they can make? A square is only possible, if the prime factors of A and B together make pairs. E.g. 3 from A can only be combined with non-existing 3 from B to make a square or any B with an odd power of 3 among the prime factors, i.e. the existing 3*2*2=12, but not with 3*3*2*2. That way you can eliminate many entries in A and B. – Yunnosch May 18 '17 at 20:19
  • @Yunnosch yeah but to check if such pair is possbile we need to loop through A and B which is o(n^2). – Coder doit May 18 '17 at 20:28

1 Answers1

1

The sentence "Find all the pairs A and B" is a good indicator that it is not possible to go under O(n^2). For instance, lets pick: A = [2 2 2], B[2 2 2], C = [4 4 4], it is easily seen that we have n^2 pairs summing up to 4.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • what did you change? I think what is said in the answer is still valid. – NiVeR May 18 '17 at 19:36
  • Perhaps the sequences are *strictly* increasing/decreasing? – NiVeR May 18 '17 at 19:38
  • finding pairs limits you to o(n^2) (as no matter what you need to print all the pairs) but counting the pairs might save time if proper algo is implemented. Eg: Arr:[1,2,3,4,5] find all the numbers greater than 2 in the above sorted array Ans:3,4,5 time complexity:o(n)......but if it was asked to count the no, of numbers greater than 2 then Ans:3 time complexity:o(1) – Coder doit May 18 '17 at 19:43
  • can u explain again? – Coder doit May 18 '17 at 19:56