Find pairs in two arrays such that when multiplied becomes a perfect square

后端 未结 4 880
不知归路
不知归路 2021-01-23 05:11

Given two integer arrays like this:-

int[] a = { 2, 6, 10, 13, 17,18 };
int[] b = { 3, 7, 8, 9, 11, 15 };

How can I find pai

4条回答
  •  野性不改
    2021-01-23 06:13

    I managed to simplify it using LINQ:

    int[] arr1 = { 2, 6, 10, 13, 17, 18 };
    int[] arr2 = { 3, 7, 8, 9, 11, 15 };
    int count = arr1.Sum(t => (from t1 in arr2 select t*t1 into x let s = (long) Math.Sqrt(x) where x == s*s select x).Count());
    

提交回复
热议问题