1 #!/usr/bin/perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-203/#TASK1
 3 #
 4 # Task 1: Special Quadruplets
 5 #
 6 # You are given an array of integers.
 7 #
 8 # Write a script to find out the total special quadruplets for the given array.
 9 #
10 ## Special Quadruplets are such that satisfies the following 2 rules.
11 ## 1) nums[a] + nums[b] + nums[c] == nums[d]
12 ## 2) a < b < c < d
13 #
14 #
15 ## Example 1
16 ##
17 ## Input: @nums = (1,2,3,6)
18 ## Output: 1
19 ##
20 ## Since the only special quadruplets found is $nums[0] + $nums[1] + $nums[2] == $nums[3].
21 #
22 ## Example 2
23 ##
24 ## Input: @nums = (1,1,1,3,5)
25 ## Output: 4
26 ##
27 ## $nums[0] + $nums[1] + $nums[2] == $nums[3]
28 ## $nums[0] + $nums[1] + $nums[3] == $nums[4]
29 ## $nums[0] + $nums[2] + $nums[3] == $nums[4]
30 ## $nums[1] + $nums[2] + $nums[3] == $nums[4]
31 #
32 ## Example 3
33 ##
34 ## Input: @nums = (3,3,6,4,5)
35 ## Output: 0
36 #
37 ############################################################
38 ##
39 ## discussion
40 ##
41 ############################################################
42 #
43 # this is pretty straight forward, just walk the array with 4 variables
44 # and in each step check the condition
45 
46 use strict;
47 use warnings;
48 use feature 'say';
49 
50 my @examples = (
51    [1,2,3,6],
52    [1,1,1,3,5],
53    [3,3,6,4,5]
54 );
55 
56 foreach my $nums (@examples) {
57    say "Found " . get_quadruples(@$nums) . " for (" . join(", ", @$nums) . ")";
58 }
59 
60 sub get_quadruples {
61    my @nums = @_;
62    my $count = 0;
63    foreach my $A (0..$#nums) {
64       foreach my $B ($A+1..$#nums) {
65          foreach my $C ($B+1..$#nums) {
66             foreach my $D ($C+1..$#nums) {
67                $count++ if $nums[$A]+$nums[$B]+$nums[$C] == $nums[$D];
68             }
69          }
70       }
71    }
72    return $count;
73 }