Missing even and odd elements from the given arrays | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMissingNums ( int even [ ] , int sizeEven , int odd [ ] , int sizeOdd ) { int minEven = INT_MAX ; int maxEven = INT_MIN ; int minOdd = INT_MAX ; int maxOdd = INT_MIN ; int sumEvenArr = 0 , sumOddArr = 0 ; for ( int i = 0 ; i < sizeEven ; i ++ ) { minEven = min ( minEven , even [ i ] ) ; maxEven = max ( maxEven , even [ i ] ) ; sumEvenArr += even [ i ] ; } for ( int i = 0 ; i < sizeOdd ; i ++ ) { minOdd = min ( minOdd , odd [ i ] ) ; maxOdd = max ( maxOdd , odd [ i ] ) ; sumOddArr += odd [ i ] ; } int totalTerms = 0 , reqSum = 0 ; totalTerms = minEven / 2 ; int evenSumMin = totalTerms * ( totalTerms + 1 ) ; totalTerms = maxEven / 2 ; int evenSumMax = totalTerms * ( totalTerms + 1 ) ; reqSum = evenSumMax - evenSumMin + minEven ; cout << " Even ▁ = ▁ " << reqSum - sumEvenArr << " STRNEWLINE " ; totalTerms = ( minOdd / 2 ) + 1 ; int oddSumMin = totalTerms * totalTerms ; totalTerms = ( maxOdd / 2 ) + 1 ; int oddSumMax = totalTerms * totalTerms ; reqSum = oddSumMax - oddSumMin + minOdd ; cout << " Odd ▁ = ▁ " << reqSum - sumOddArr ; } int main ( ) { int even [ ] = { 6 , 4 , 8 , 14 , 10 } ; int sizeEven = sizeof ( even ) / sizeof ( even [ 0 ] ) ; int odd [ ] = { 7 , 5 , 3 , 11 , 13 } ; int sizeOdd = sizeof ( odd ) / sizeof ( odd [ 0 ] ) ; findMissingNums ( even , sizeEven , odd , sizeOdd ) ; return 0 ; }