daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 51 in 22 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // union method two sorted arrays
  2.  
  3.     fun arraysUnion(firstArray: IntArray, secondArray: IntArray): IntArray {
  4.         val firstArraySize = firstArray.size
  5.         val secondArraySize = secondArray.size
  6.         val resultArray = IntArray(firstArraySize + secondArraySize)
  7.         var firstArrayIndex = 0
  8.         var secondArrayIndex = 0
  9.         while (firstArrayIndex < firstArraySize && secondArrayIndex < secondArraySize) {
  10.             if (firstArray[firstArrayIndex] < secondArray[secondArrayIndex]) {
  11.                 resultArray[firstArrayIndex + secondArrayIndex] = firstArray[firstArrayIndex]
  12.                 firstArrayIndex++
  13.             } else {
  14.                 resultArray[firstArrayIndex + secondArrayIndex] = secondArray[secondArrayIndex]
  15.                 secondArrayIndex++
  16.             }
  17.             if (firstArrayIndex == firstArraySize) {
  18.                 while (secondArrayIndex < secondArraySize) {
  19.                     resultArray[firstArrayIndex + secondArrayIndex] = secondArray[secondArrayIndex]
  20.                     secondArrayIndex++
  21.                 }
  22.                 break
  23.             } else if (secondArrayIndex == secondArraySize) {
  24.                 while (firstArrayIndex < firstArraySize) {
  25.                     resultArray[firstArrayIndex + secondArrayIndex] = firstArray[firstArrayIndex]
  26.                     firstArrayIndex++
  27.                 }
  28.                 break
  29.             }
  30.         }
  31.         return resultArray
  32.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top