# Challenge 086

## Task 1 - Pair Difference

You are given an array of integers @N and an integer \$A.

Write a script to find find if there exists a pair of elements in the array whose difference is \$A.

Print 1 if exists otherwise 0.

### Perl

@N & \$A are taken from stdin, \$A is the last argument.

```die "usage: ./ch-1.pl <integers \@N> <integer \\$A>\n"
unless scalar @ARGV >= 3;

my \$A = pop @ARGV;
my @N = @ARGV;
```

We just loop over @N over a loop over @N & find the difference. If it's equal to `\$A` or `-\$A` then we print `1` & exit. The first loop is `shift`'ing the numbers out of array `@N` because we are matching for both `\$A` & `-\$A` so we don't need the number again.

For example, if `@N = [1, 2]` & we don't `shift` in first loop then we'll perform 2 subtraction operations: `1 - 2` & `2 - 1` & we won't have to match for `-\$A` but if we just match for `-\$A` then we can use `shift` & we'll only have to perform 1 subtraction operation `1 - 2`.

We assume subtraction costs more than matching with `-\$A`, that makes this more efficient. But it doesn't matter much.

```while (my \$int = shift @N) {
foreach (@N) {
my \$diff = \$int - \$_;
print "1\n" and exit 0 if (\$diff == \$A or \$diff == -\$A);
}
}
print "0\n";

```

Andinus / 2020-11-15 / Modified: 2021-10-11 Mon 19:56 Emacs 27.2 (Org mode 9.4.4)
{"ft_lang_label":"__label__en","ft_lang_prob":0.7966187,"math_prob":0.954287,"size":1209,"snap":"2022-40-2023-06","text_gpt3_token_len":378,"char_repetition_ratio":0.08547718,"word_repetition_ratio":0.0,"special_character_ratio":0.33333334,"punctuation_ratio":0.1007752,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98767346,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-26T02:13:52Z\",\"WARC-Record-ID\":\"<urn:uuid:7166c2bd-fe95-4f79-ad46-ca7e1b6e50db>\",\"Content-Length\":\"5453\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:545ad76e-3954-4a2b-b10e-89f0fe76b247>\",\"WARC-Concurrent-To\":\"<urn:uuid:83cac06c-01d6-486e-af72-35d0845d239f>\",\"WARC-IP-Address\":\"144.24.113.75\",\"WARC-Target-URI\":\"https://andinus.unfla.me/pwc/challenge-086/\",\"WARC-Payload-Digest\":\"sha1:6PBGX4RZEJ5RDETZEKOJUYHPP5KCGJUC\",\"WARC-Block-Digest\":\"sha1:AZBU7BLHAGGYWUL26K4YUARINKWCQF4S\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030334644.42_warc_CC-MAIN-20220926020051-20220926050051-00645.warc.gz\"}"}