📌  相关文章
📜  缺少给定数组中的偶数和奇数元素

📅  最后修改于: 2021-04-22 08:31:56             🧑  作者: Mango

给定两个整数数组even []奇数[] ,它们分别包含连续的偶数和奇数元素,每个数组中都缺少一个元素。任务是找到缺少的元素。

例子:

方法:even []数组中的最小和最大偶数元素存储在变量minEvenmaxEven中。众所周知,前N个偶数之和N *(N +1) 。计算从2minEvensum1的偶数之和,从2maxEvensum2的偶数之和。现在,需要的甚至阵列的总和将reqSum = SUM2 – SUM1 + minEven,从这个reqSum减去即使[]数组总和会给我们缺少的偶数。
同样,也可以找到丢失的奇数,因为我们知道前N个奇数总和N 2

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the missing numbers
void findMissingNums(int even[], int sizeEven, int odd[], int sizeOdd)
{
  
    // To store the minimum and the maximum
    // odd and even elements from the arrays
    int minEven = INT_MAX;
    int maxEven = INT_MIN;
    int minOdd = INT_MAX;
    int maxOdd = INT_MIN;
  
    // To store the sum of the array elements
    int sumEvenArr = 0, sumOddArr = 0;
  
    // Get the minimum and the maximum
    // even elements from the array
    for (int i = 0; i < sizeEven; i++) {
        minEven = min(minEven, even[i]);
        maxEven = max(maxEven, even[i]);
        sumEvenArr += even[i];
    }
  
    // Get the minimum and the maximum
    // odd elements from the array
    for (int i = 0; i < sizeOdd; i++) {
        minOdd = min(minOdd, odd[i]);
        maxOdd = max(maxOdd, odd[i]);
        sumOddArr += odd[i];
    }
  
    // To store the total terms in the series
    // and the required sum of the array
    int totalTerms = 0, reqSum = 0;
  
    // Total terms from 2 to minEven
    totalTerms = minEven / 2;
  
    // Sum of all even numbers from 2 to minEven
    int evenSumMin = totalTerms * (totalTerms + 1);
  
    // Total terms from 2 to maxEven
    totalTerms = maxEven / 2;
  
    // Sum of all even numbers from 2 to maxEven
    int evenSumMax = totalTerms * (totalTerms + 1);
  
    // Required sum for the even array
    reqSum = evenSumMax - evenSumMin + minEven;
  
    // Missing even number
    cout << "Even = " << reqSum - sumEvenArr << "\n";
  
    // Total terms from 1 to minOdd
    totalTerms = (minOdd / 2) + 1;
  
    // Sum of all odd numbers from 1 to minOdd
    int oddSumMin = totalTerms * totalTerms;
  
    // Total terms from 1 to maxOdd
    totalTerms = (maxOdd / 2) + 1;
  
    // Sum of all odd numbers from 1 to maxOdd
    int oddSumMax = totalTerms * totalTerms;
  
    // Required sum for the odd array
    reqSum = oddSumMax - oddSumMin + minOdd;
  
    // Missing odd number
    cout << "Odd = " << reqSum - sumOddArr;
}
  
// Driver code
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;
}


Java
// Java implementation of the approach
class GFG
{
      
// Function to find the missing numbers
static void findMissingNums(int even[], int sizeEven, 
                            int odd[], int sizeOdd)
{
  
    // To store the minimum and the maximum
    // odd and even elements from the arrays
    int minEven =Integer.MAX_VALUE;
    int maxEven =Integer.MIN_VALUE;
    int minOdd = Integer.MAX_VALUE;
    int maxOdd = Integer.MIN_VALUE;
  
    // To store the sum of the array elements
    int sumEvenArr = 0, sumOddArr = 0;
  
    // Get the minimum and the maximum
    // even elements from the array
    for (int i = 0; i < sizeEven; i++)
    {
        minEven = Math.min(minEven, even[i]);
        maxEven = Math.max(maxEven, even[i]);
        sumEvenArr += even[i];
    }
  
    // Get the minimum and the maximum
    // odd elements from the array
    for (int i = 0; i < sizeOdd; i++) 
    {
        minOdd = Math.min(minOdd, odd[i]);
        maxOdd = Math.max(maxOdd, odd[i]);
        sumOddArr += odd[i];
    }
  
    // To store the total terms in the series
    // and the required sum of the array
    int totalTerms = 0, reqSum = 0;
  
    // Total terms from 2 to minEven
    totalTerms = minEven / 2;
  
    // Sum of all even numbers
    // from 2 to minEven
    int evenSumMin = (totalTerms * 
                     (totalTerms + 1));
  
    // Total terms from 2 to maxEven
    totalTerms = maxEven / 2;
  
    // Sum of all even numbers from 
    // 2 to maxEven
    int evenSumMax = (totalTerms * 
                     (totalTerms + 1));
  
    // Required sum for the even array
    reqSum = evenSumMax - evenSumMin + minEven;
  
    // Missing even number
    System.out.println("Even = " + 
                      (reqSum - sumEvenArr));
  
    // Total terms from 1 to minOdd
    totalTerms = (minOdd / 2) + 1;
  
    // Sum of all odd numbers 
    // from 1 to minOdd
    int oddSumMin = totalTerms * totalTerms;
  
    // Total terms from 1 to maxOdd
    totalTerms = (maxOdd / 2) + 1;
  
    // Sum of all odd numbers 
    // from 1 to maxOdd
    int oddSumMax = totalTerms * totalTerms;
  
    // Required sum for the odd array
    reqSum = oddSumMax - oddSumMin + minOdd;
  
    // Missing odd number
    System.out.println("Odd = " + 
                      (reqSum - sumOddArr));
}
  
// Driver code
public static void main(String[] args)
{
    int even[] = { 6, 4, 8, 14, 10 };
    int sizeEven = even.length;
    int odd[] = { 7, 5, 3, 11, 13 };
    int sizeOdd = odd.length;
    findMissingNums(even, sizeEven, 
                     odd, sizeOdd);
}
}
  
// This code is contributed 
// by Code_Mech.


Python3
# Python3 implementation of the approach 
import sys
  
# Function to find the missing numbers 
def findMissingNums(even, sizeEven,
                      odd, sizeOdd): 
  
    # To store the minimum and the
    # maximum odd and even elements
    # from the arrays 
    minEven = sys.maxsize ; 
    maxEven = -(sys.maxsize - 1); 
    minOdd = sys.maxsize ; 
    maxOdd = -(sys.maxsize - 1); 
      
    # To store the sum of the 
    # array elements
    sumEvenArr = 0;
    sumOddArr = 0; 
  
    # Get the minimum and the maximum 
    # even elements from the array 
    for i in range(sizeEven) :
        minEven = min(minEven, even[i]); 
        maxEven = max(maxEven, even[i]); 
        sumEvenArr += even[i]; 
  
    # Get the minimum and the maximum 
    # odd elements from the array 
    for i in range(sizeOdd) :
        minOdd = min(minOdd, odd[i]); 
        maxOdd = max(maxOdd, odd[i]); 
        sumOddArr += odd[i]; 
      
    # To store the total terms in 
    # the series and the required
    # sum of the array 
    totalTerms = 0;
    reqSum = 0; 
  
    # Total terms from 2 to minEven 
    totalTerms = minEven // 2; 
  
    # Sum of all even numbers from
    # 2 to minEven 
    evenSumMin = (totalTerms * 
                 (totalTerms + 1)); 
  
    # Total terms from 2 to maxEven 
    totalTerms = maxEven // 2; 
  
    # Sum of all even numbers from
    # 2 to maxEven 
    evenSumMax = (totalTerms * 
                 (totalTerms + 1)); 
  
    # Required sum for the even array 
    reqSum = (evenSumMax - 
              evenSumMin + minEven); 
  
    # Missing even number 
    print("Even =", reqSum - 
                    sumEvenArr); 
  
    # Total terms from 1 to minOdd 
    totalTerms = (minOdd // 2) + 1; 
  
    # Sum of all odd numbers from 
    # 1 to minOdd 
    oddSumMin = totalTerms * totalTerms; 
  
    # Total terms from 1 to maxOdd 
    totalTerms = (maxOdd // 2) + 1; 
  
    # Sum of all odd numbers from
    # 1 to maxOdd 
    oddSumMax = totalTerms * totalTerms; 
  
    # Required sum for the odd array 
    reqSum = (oddSumMax - 
              oddSumMin + minOdd); 
  
    # Missing odd number 
    print("Odd =", reqSum - sumOddArr); 
  
# Driver code 
if __name__ == "__main__" : 
      
    even = [ 6, 4, 8, 14, 10 ]; 
    sizeEven = len(even)
    odd = [ 7, 5, 3, 11, 13 ]; 
    sizeOdd = len(odd) ;
    findMissingNums(even, sizeEven, 
                    odd, sizeOdd); 
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to find the missing numbers
static void findMissingNums(int []even, int sizeEven, 
                            int []odd, int sizeOdd)
{
  
    // To store the minimum and the maximum
    // odd and even elements from the arrays
    int minEven =int.MaxValue;
    int maxEven =int.MinValue;
    int minOdd = int.MaxValue;
    int maxOdd = int.MinValue;
  
    // To store the sum of the array elements
    int sumEvenArr = 0, sumOddArr = 0;
  
    // Get the minimum and the maximum
    // even elements from the array
    for (int i = 0; i < sizeEven; i++)
    {
        minEven = Math.Min(minEven, even[i]);
        maxEven = Math.Max(maxEven, even[i]);
        sumEvenArr += even[i];
    }
  
    // Get the minimum and the maximum
    // odd elements from the array
    for (int i = 0; i < sizeOdd; i++) 
    {
        minOdd = Math.Min(minOdd, odd[i]);
        maxOdd = Math.Max(maxOdd, odd[i]);
        sumOddArr += odd[i];
    }
  
    // To store the total terms in the series
    // and the required sum of the array
    int totalTerms = 0, reqSum = 0;
  
    // Total terms from 2 to minEven
    totalTerms = minEven / 2;
  
    // Sum of all even numbers
    // from 2 to minEven
    int evenSumMin = (totalTerms * 
                    (totalTerms + 1));
  
    // Total terms from 2 to maxEven
    totalTerms = maxEven / 2;
  
    // Sum of all even numbers from 
    // 2 to maxEven
    int evenSumMax = (totalTerms * 
                    (totalTerms + 1));
  
    // Required sum for the even array
    reqSum = evenSumMax - evenSumMin + minEven;
  
    // Missing even number
    Console.WriteLine("Even = " + 
                    (reqSum - sumEvenArr));
  
    // Total terms from 1 to minOdd
    totalTerms = (minOdd / 2) + 1;
  
    // Sum of all odd numbers 
    // from 1 to minOdd
    int oddSumMin = totalTerms * totalTerms;
  
    // Total terms from 1 to maxOdd
    totalTerms = (maxOdd / 2) + 1;
  
    // Sum of all odd numbers 
    // from 1 to maxOdd
    int oddSumMax = totalTerms * totalTerms;
  
    // Required sum for the odd array
    reqSum = oddSumMax - oddSumMin + minOdd;
  
    // Missing odd number
    Console.WriteLine("Odd = " + 
                    (reqSum - sumOddArr));
}
  
// Driver code
static void Main()
{
    int []even = { 6, 4, 8, 14, 10 };
    int sizeEven = even.Length;
    int []odd = { 7, 5, 3, 11, 13 };
    int sizeOdd = odd.Length;
    findMissingNums(even, sizeEven, 
                    odd, sizeOdd);
}
}
  
// This code is contributed 
// by chandan_jnu


PHP


输出:
Even = 12
Odd = 9