aboutsummaryrefslogtreecommitdiff
path: root/challenge-243/jeanluc2020/perl/ch-1.pl
blob: f3386dd399b61ff944d6410567d0546400a57f24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl
# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK1
#
# Task 1: Reverse Pairs
# =====================
#
# You are given an array of integers.
#
# Write a script to return the number of reverse pairs in the given
# array.
#
# A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length
# and b) nums[i] > 2 * nums[j].
#
## Example 1
##
## Input: @nums = (1, 3, 2, 3, 1)
## Output: 2
##
## (1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1
## (3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1
#
## Example 2
##
## Input: @nums = (2, 4, 3, 5, 1)
## Output: 3
##
## (1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1
## (2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1
## (3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1
#
############################################################
##
## discussion
##
############################################################
#
# Walk the array twice (once from 0, once from the first index + 1)
# Check the condition in each case, counting the occurrences where
# the condition holds true.

use strict;
use warnings;

reverse_pairs(1, 3, 2, 3, 1);
reverse_pairs(2, 4, 3, 5, 1);

sub reverse_pairs {
   my @nums = @_;
   my $result = 0;
   print "Input: (" . join(", ", @nums) . ")\n";
   foreach my $i (0..$#nums) {
      foreach my $j ($i+1..$#nums) {
         $result++ if $nums[$i] > 2 * $nums[$j];
      }
   }
   print "Output: $result\n";
}