📌  相关文章
📜  在由前 M 个自然数组成的大小为 N 的数组中查找重复元素

📅  最后修改于: 2022-05-13 01:56:09.373000             🧑  作者: Mango

在由前 M 个自然数组成的大小为 N 的数组中查找重复元素

给定一个大小为N的数组arr[] ,其中包含从1 到 M的数字排列,以及一个重复的元素(一次或多次),任务是找到重复的元素。

例子:

天真的方法:天真的方法是对数组进行排序并检查相邻元素是否相等。

时间复杂度: O(NlogN)
辅助空间: O(1)

解决方法:按照以下步骤解决问题:

  1. 初始化两个变量Msum分别存储数组的最大元素和总和。
  2. 遍历数组arr并执行以下操作:
    1. 将当前元素加到sum
    2. 将当前元素与M进行比较以计算最大元素。
  3. 使用以下公式将从1 到 M的排列总和存储在变量sum1中:
Sum of elements from 1 to X= X*(X+1)/2
  1. 将答案计算为sumsum1之间的差除以额外字符的数量,即(sum-sum1)/(NM)

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate the repeating character in a given
// permutation
int repeatingElement(int arr[], int N)
{
    // variables to store maximum element and sum of the
    // array respectively.
    int M = 0, sum = 0;
    for (int i = 0; i < N; i++) {
 
        // calculate sum of array
        sum += arr[i];
 
        // calculate maximum element in the array
        M = max(M, arr[i]);
    }
 
    // calculating sum of permutation
    int sum1 = M * (M + 1) / 2;
 
    // calculate required answer
    int ans = (sum - sum1) / (N - M);
    return ans;
}
// Driver code
int main()
{
    // Input
    int arr[] = { 2, 6, 4, 3, 1, 5, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << repeatingElement(arr, N) << endl;
    return 0;
}


Java
// Java Program for the above approach
import java.io.*;
 
class GFG
{
   
  // Function to calculate the repeating character in a given
  // permutation
  public static int repeatingElement(int arr[], int N)
  {
     
    // variables to store maximum element and sum of the
    // array respectively.
    int M = 0, sum = 0;
    for (int i = 0; i < N; i++) {
 
      // calculate sum of array
      sum += arr[i];
 
      // calculate maximum element in the array
      M = Math.max(M, arr[i]);
    }
 
    // calculating sum of permutation
    int sum1 = M * (M + 1) / 2;
 
    // calculate required answer
    int ans = (sum - sum1) / (N - M);
    return ans;
  }
 
  // Driver code
  public static void main (String[] args)
  {
     
    // Input
    int arr[] = { 2, 6, 4, 3, 1, 5, 2 };
    int N = arr.length;
 
    // Function call
    System.out.println(repeatingElement(arr, N));
  }
}
 
// This code is contributed by lokeshpotta20


Python3
# Python 3 program for the above approach
 
# Function to calculate the repeating character in a given
# permutation
def repeatingElement(arr, N):
   
    # variables to store maximum element and sum of the
    # array respectively.
    M = 0
    sum = 0
    for i in range(N):
       
        # calculate sum of array
        sum += arr[i]
 
        # calculate maximum element in the array
        M = max(M, arr[i])
 
    # calculating sum of permutation
    sum1 = M * (M + 1) // 2
 
    # calculate required answer
    ans = (sum - sum1) // (N - M)
    return ans
 
# Driver code
if __name__ == '__main__':
   
    # Input
    arr = [2, 6, 4, 3, 1, 5, 2]
    N = len(arr)
 
    # Function call
    print(repeatingElement(arr, N))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C++ program for the above approach
using System;
 
// Function to calculate the repeating character in a given
// permutation
public class GFG
{
    public static int repeatingElement(int[] arr, int N)
    {
       
        // variables to store maximum element and sum of the
        // array respectively.
        int M = 0, sum = 0;
        for (int i = 0; i < N; i++) {
 
            // calculate sum of array
            sum += arr[i];
 
            // calculate maximum element in the array
            M = Math.Max(M, arr[i]);
        }
 
        // calculating sum of permutation
        int sum1 = M * (M + 1) / 2;
 
        // calculate required answer
        int ans = (sum - sum1) / (N - M);
        return ans;
    }
   
    // Driver code
    public static void Main()
    {
        // Input
        int[] arr = { 2, 6, 4, 3, 1, 5, 2 };
        int N = 7;
 
        // Function call
        Console.WriteLine(repeatingElement(arr, N));
    }
}
 
// This code is contributed by Sohom Das


Javascript
// JavaScript program for the above approach
 
        // Function to calculate the repeating character in a given
        // permutation
        function repeatingElement(arr, N)
        {
         
            // variables to store maximum element and sum of the
            // array respectively.
            let M = 0, sum = 0;
            for (let i = 0; i < N; i++) {
 
                // calculate sum of array
                sum += arr[i];
 
                // calculate maximum element in the array
                M = Math.max(M, arr[i]);
            }
 
            // calculating sum of permutation
            let sum1 = parseInt(M * (M + 1) / 2);
 
            // calculate required answer
            let ans = parseInt((sum - sum1) / (N - M));
            return ans;
        }
        // Driver code
 
        // Input
        let arr = [2, 6, 4, 3, 1, 5, 2];
        let N = arr.length;
 
        // Function call
        document.write(repeatingElement(arr, N));
 
  // This code is contributed by Potta Lokesh
    



输出
2

时间复杂度: O(N)
辅助空间: O(1)