📜  使用两个方程式找到重复和缺失的数字

📅  最后修改于: 2021-04-22 02:16:00             🧑  作者: Mango

给定一个大小为N的数组arr [] ,范围[1,N]中的每个整数只出现一次,除了A出现两次B丢失了。任务是找到数字AB。
例子:

方法:根据前N个自然数的总和,

根据前N个自然数的平方和

将等式2中等式1的(A – B)值
SumSq – SumSqN =(A + B)*(Sum – SumN)
A + B =(SumSq – SumSqN)/(Sum – SumN)…(等式3)
求解方程式1和方程式3将得出,

下面是上述方法的实现:

C++
//C++ implementation of the approach
 
#include 
#include
#include 
 
using namespace std;
 
    // Function to print the required numbers
 void findNumbers(int arr[], int n)
    {
 
        // Sum of first n natural numbers
        int sumN = (n * (n + 1)) / 2;
 
        // Sum of squares of first n natural numbers
        int sumSqN = (n * (n + 1) * (2 * n + 1)) / 6;
 
        // To store the sum and sum of squares
        // of the array elements
        int sum = 0, sumSq = 0, i;
 
        for (i = 0; i < n; i++) {
            sum += arr[i];
            sumSq = sumSq + (pow(arr[i], 2));
        }
 
        int B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2;
        int A = sum - sumN + B;
         cout << "A = " ;
         cout << A << endl;
         cout << "B = " ;
         cout << B << endl;
    }
 
    // Driver code
int main() {
        int arr[] = { 1, 2, 2, 3, 4 };
        int n = sizeof(arr)/sizeof(arr[0]);
        findNumbers(arr, n);
    return 0;
}


Java
// Java implementation of the approach
public class GFG {
 
    // Function to print the required numbers
    static void findNumbers(int arr[], int n)
    {
 
        // Sum of first n natural numbers
        int sumN = (n * (n + 1)) / 2;
 
        // Sum of squares of first n natural numbers
        int sumSqN = (n * (n + 1) * (2 * n + 1)) / 6;
 
        // To store the sum and sum of squares
        // of the array elements
        int sum = 0, sumSq = 0, i;
 
        for (i = 0; i < n; i++) {
            sum += arr[i];
            sumSq += Math.pow(arr[i], 2);
        }
 
        int B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2;
        int A = sum - sumN + B;
        System.out.println("A = " + A + "\nB = " + B);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 2, 3, 4 };
        int n = arr.length;
 
        findNumbers(arr, n);
    }
}


Python3
# Python3 implementation of the approach
 
import math
# Function to print the required numbers
def findNumbers(arr, n):
     
 
        # Sum of first n natural numbers
        sumN = (n * (n + 1)) / 2;
 
        # Sum of squares of first n natural numbers
        sumSqN = (n * (n + 1) * (2 * n + 1)) / 6;
 
        # To store the sum and sum of squares
        # of the array elements
        sum = 0;
        sumSq = 0;
 
        for i in range(0,n):
            sum = sum + arr[i];
            sumSq = sumSq + (math.pow(arr[i], 2));
         
 
        B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2;
        A = sum - sumN + B;
        print("A = ",int(A)) ;
        print("B = ",int(B));
     
 
# Driver code
 
arr = [ 1, 2, 2, 3, 4 ];
n = len(arr);
findNumbers(arr, n);
 
#This code is contributed by Shivi_Aggarwal


C#
// C# implementation of the approach
using System;
public class GFG {
 
    // Function to print the required numbers
    static void findNumbers(int []arr, int n)
    {
 
        // Sum of first n natural numbers
        int sumN = (n * (n + 1)) / 2;
 
        // Sum of squares of first n natural numbers
        int sumSqN = (n * (n + 1) * (2 * n + 1)) / 6;
 
        // To store the sum and sum of squares
        // of the array elements
        int sum = 0, sumSq = 0, i;
 
        for (i = 0; i < n; i++) {
            sum += arr[i];
            sumSq += (int)Math.Pow(arr[i], 2);
        }
 
        int B = (((sumSq - sumSqN) / (sum - sumN)) + sumN - sum) / 2;
        int A = sum - sumN + B;
        Console.WriteLine("A = " + A + "\nB = " + B);
    }
 
    // Driver code
    public static void Main()
    {
        int []arr = { 1, 2, 2, 3, 4 };
        int n = arr.Length;
 
        findNumbers(arr, n);
    }
}
// This code is contributed by PrinciRaj1992


PHP


Javascript


输出:
A = 2
B = 5