Missing even and odd elements from the given arrays | Function to find the missing numbers ; To store the minimum and the maximum odd and even elements from the arrays ; To store the sum of the array elements ; Get the minimum and the maximum even elements from the array ; Get the minimum and the maximum odd elements from the array ; To store the total terms in the series and the required sum of the array ; Total terms from 2 to minEven ; Sum of all even numbers from 2 to minEven ; Total terms from 2 to maxEven ; Sum of all even numbers from 2 to maxEven ; Required sum for the even array ; Missing even number ; Total terms from 1 to minOdd ; Sum of all odd numbers from 1 to minOdd ; Total terms from 1 to maxOdd ; Sum of all odd numbers from 1 to maxOdd ; Required sum for the odd array ; Missing odd number ; Driver code
function findMissingNums ( even , sizeEven , odd , sizeOdd ) { let minEven = Number . MAX_VALUE ; let maxEven = Number . MIN_VALUE ; let minOdd = Number . MAX_VALUE ; let maxOdd = Number . MIN_VALUE ; let sumEvenArr = 0 , sumOddArr = 0 ; for ( let i = 0 ; i < sizeEven ; i ++ ) { minEven = Math . min ( minEven , even [ i ] ) ; maxEven = Math . max ( maxEven , even [ i ] ) ; sumEvenArr += even [ i ] ; } for ( let i = 0 ; i < sizeOdd ; i ++ ) { minOdd = Math . min ( minOdd , odd [ i ] ) ; maxOdd = Math . max ( maxOdd , odd [ i ] ) ; sumOddArr += odd [ i ] ; } let totalTerms = 0 , reqSum = 0 ; totalTerms = parseInt ( minEven / 2 , 10 ) ; let evenSumMin = ( totalTerms * ( totalTerms + 1 ) ) ; totalTerms = parseInt ( maxEven / 2 , 10 ) ; let evenSumMax = ( totalTerms * ( totalTerms + 1 ) ) ; reqSum = evenSumMax - evenSumMin + minEven ; document . write ( " " + ( reqSum - sumEvenArr ) + " " ) ; totalTerms = parseInt ( minOdd / 2 , 10 ) + 1 ; let oddSumMin = totalTerms * totalTerms ; totalTerms = parseInt ( maxOdd / 2 , 10 ) + 1 ; let oddSumMax = totalTerms * totalTerms ; reqSum = oddSumMax - oddSumMin + minOdd ; document . write ( " " + ( reqSum - sumOddArr ) ) ; } let even = [ 6 , 4 , 8 , 14 , 10 ] ; let sizeEven = even . length ; let odd = [ 7 , 5 , 3 , 11 , 13 ] ; let sizeOdd = odd . length ; findMissingNums ( even , sizeEven , odd , sizeOdd ) ;