Pairs of integers in an Array whose sum is equal to a given number in Kotlin

To find all pairs of integers in an array whose sum is equal to a given number, you can follow these steps

  • Create an empty hash set to store the visited elements of the array.
  • Loop through the array, and for each element num, calculate the difference diff between the given number and the current element (diff = target - num).
  • Check if diff is present in the hash set. If it is, then you have found a pair whose sum is equal to the target number. Add this pair to a list of pairs.
  • If diff is not present in the hash set, add the current element num to the hash set.
  • After looping through the entire array, return the list of pairs.
  • Here is an example Kotlin function that implements this algorithm:

    Kotlin
    fun findPairsWithSum(arr: IntArray, target: Int): List<Pair<Int, Int>> {     val set = mutableSetOf<Int>()     val pairs = mutableListOf<Pair<Int, Int>>()      for (num in arr) {         val diff = target - num         if (set.contains(diff)) {             pairs.add(Pair(num, diff))         }         set.add(num)     }      return pairs } fun main() {     val arr = intArrayOf(2, 5, 8, 3, 9, 1, 4, 6)     val target = 10     val pairs = findPairsWithSum(arr, target)     if (pairs.isEmpty()) {         println("No pairs found")     } else {         println("Pairs with sum $target:")         for (pair in pairs) {             println("${pair.first}, ${pair.second}")         }     } }

    This function takes an integer array arr and a target sum target as input and returns a list of pairs of integers whose sum is equal to target. The function first creates an empty mutable set set to store the visited elements of the array, and an empty mutable list pairs to store the pairs of integers whose sum is equal to target.

    The function then loops through the array using a for-each loop, and for each element num, it calculates the difference diff between the target sum and the current element. It then checks if diff is present in the hash set using the contains method. If it is, it adds a pair (num, diff) to the pairs list. If diff is not present in the hash set, the function adds the current element num to the hash set using the add method.

    Finally, the function returns the list of pairs pairs.

    main function first defines an input array arr and a target sum value target. It then calls the findPairsWithSum function to find all pairs of integers in arr whose sum is equal to target. If no pairs are found, it prints a message saying so. Otherwise, it prints the pairs found one by one using a for loop and the println function.

    Output : Pairs with sum 10: [8, 2] [1, 9] [6, 4]

    The time complexity of this function is O(n), where n is the length of the input array, because the function loops through the array once.

    The space complexity of the function is O(n), because in the worst case scenario the hash set may contain all the elements of the array.

    Leave a Comment