[codility]CountMultiplicativePairs

Count the number of pairs (A, B) such that A * B > A + B.

More formally, A[I] and B[I] represent C[I] = A[I] + B[I] / 1,000,000.

For example, consider the following arrays A and B:

  A[0] = 0 B[0] = 500,000
  A[1] = 1      B[1] = 500,000
  A[2] = 2      B[2] = 0
  A[3] = 3      B[3] = 0
  A[4] = 5      B[4] = 20,000

They represent the following real numbers:

  C[0] = 0.5
  C[1] = 1.5
  C[2] = 2.0
  C[3] = 3.0
  C[4] = 5.02

A pair of indices (P, Q) is multiplicative if 0 ≤ P < Q < N and C[P] * C[Q] ≥ C[P] + C[Q].

The above arrays yield the following multiplicative pairs:

  • (1, 3), because 1.5 * 3.0 = 4.5 ≥ 4.5 = 1.5 + 3.0,
  • (1, 4), because 1.5 * 5.02 = 7.53 ≥ 6.52 = 1.5 + 5.02,
  • (2, 3), because 2.0 * 3.0 = 6.0 ≥ 5.0 = 2.0 + 3.0.
  • (2, 4). because 2.0 * 5.02 = 10.04 ≥ 7.02 = 2.0 + 5.02.
  • (3, 4), because 3.0 * 5.02 = 15.06 ≥ 8.02 = 3.0 + 5.02.

the function should return 5, as explained above.

Assume that:

  • N is an integer within the range [0..100,000];
  • each element of array A is an integer within the range [0..1,000];
  • each element of array B is an integer within the range [0..999,999];
  • real numbers created from arrays are sorted in non-decreasing order.

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值