How to create a function in dart of ArrayCouples

Viewed 1218

Have the function ArrayCouples(arr) take the arr parameter being passed which will be an array of an even number of positive integers, and determine if each pair of integers, [k, k+1], [k+2, k+3], etc. in the array has a corresponding reversed pair somewhere else in the array. For example: if arr is [4, 5, 1, 4, 5, 4, 4, 1] then the program should output the string yes because the first pair 4, 5 has the reversed pair 5, 4 in the array, and the next pair, 1, 4 has the reversed pair 4, 1 in the array as well. But if the array doesn't contain all pairs with their reversed pairs, then your program should output a string of the integer pairs that are incorrect, in the order that they appear in the array. For example: if arr is [6, 2, 2, 6, 5, 14, 14, 1] then the program should output the string 5,14,14,1 with only a comma separating the integers.

import 'dart:io';

int ArrayCouples(List arr) {

  
  return arr[0];

}
   
void main() {
  print(ArrayCouples(stdin.readLineSync()));
}

Example:

Input: [2,1,1,2,3,3]
Output: 3,3

So how to create a function like this.

1 Answers

Here's a solution that may work for you, I'll add explanations as comments:

String arrayCouples(List<int> values) {
  // Considers the input having an even number of elements
  assert(values.length % 2 == 0);
  
  // Stores the invalid pairs so we can return them on end
  final List<List<int>> invalidPairs = [];

  // Iterates over even indexes until the last index
  // This will allow us to access a pair using i0 and i0 + 1
  for (int i0 = 0; i0 < values.length; i0 += 2) {
    final int i1 = i0 + 1;

    // Creates the base pair which we'll check on the list
    final List<int> base = [values[i0], values[i1]];
    
    // Stores if the base pair is found in the list
    bool found = false;

    // Set the initial index to iterate backwards
    // If the base pair is the last element on the list,
    // start checking from the last but one pair. If not,
    // start checking from the last pair.
    final int j = (values.length - 1) - ((i0 == values.length - 2) ? 2 : 0);
    
    // Iterates backwards over even indexes until the first index
    // This will allow us to access a pair using j1 and j1 - 1
    for (int j1 = j; j1 - 1 >= 0; j1 -= 2) {
      final int j0 = j1 - 1;

      // Access the current pair
      final List<int> pair = [values[j1], values[j0]];

      // Make the comparison check
      if (base[0] == pair[0] && base[1] == pair[1]) {
        found = true;
        break;
      }
    }

    // If the reversed search doesn't find anything,
    // add the base pair to the invalid pairs
    if (!found) {
      invalidPairs.add(base);
    }
  }

  return invalidPairs.isEmpty 
    // If there are no invalid pairs, return "yes"
    ? "yes"
    // Otherwise, expand all values inside the invalid pairs 
    // to a single list and join them using a comma
    : invalidPairs.expand((pair) => pair).join(",");
}

The output for each one of

print(arrayCouples([4, 5, 1, 4, 5, 4, 4, 1]) == "yes");
print(arrayCouples([6, 2, 2, 6, 5, 14, 14, 1]) == "5,14,14,1");
print(arrayCouples([2, 1, 1, 2, 3, 3]) == "3,3");
print(arrayCouples([2, 1, 1, 2]) == "yes");

is true.

Related