📜  给定数组中缺失的前 K 个自然数的总和

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

给定数组中缺失的前 K 个自然数的总和

给定一个大小为N的数组arr[]和一个数字K ,任务是找到给定数组中不存在的前K个自然数的总和。

例子:

方法:可以根据以下思路解决问题:

请按照以下步骤解决问题:

  • 从数组元素创建一个排序集以删除所有重复项。
  • 然后对于从集合开始的每个正数:
    • 查找集合中 2 个正数之间的数字计数
    • 如果计数小于K ,则求集合中这两个数字之间的数字之和,然后将K减少 count
    • 如果计数大于K ,则从前一个正数中仅找到K个数字的总和,并将 K 减少到 0
  • 返回总和

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the sum between 0 to n
long long sumN(long long n)
{
    return n * (n + 1) / 2;
}
 
// Function to calculate the subsequence sum
long long printKMissingSum(vector& nums,
                           int k)
{
    set s(nums.begin(), nums.end());
    int a, b, prev, cnt;
 
    // Create one variable ans
    long long ans = 0;
 
    // Loop to calculate the sum
    for (auto itr = s.begin();
         itr != s.end() && k > 0; itr++) {
        b = *itr;
        if (b < 0) {
            a = 0;
            prev = 0;
            continue;
        }
 
        a = (itr == s.begin() ? 0 : prev);
        cnt = b - 1 - a;
 
        if (cnt <= k) {
            ans += sumN(b - 1) - sumN(a);
            k -= cnt;
        }
        else {
            ans += sumN(a + k) - sumN(a);
            k -= k;
        }
        prev = b;
    }
    if (k > 0) {
        ans += sumN(prev + k) - sumN(prev);
        k -= k;
    }
    return ans;
}
 
// Driver code
int main()
{
    vector arr = { -2, -3, -4 };
    int K = 2;
 
    cout << printKMissingSum(arr, K);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
    // Function to find the sum between 0 to n
    public static long sumN(long n)
    {
        return n * (n + 1) / 2;
    }
 
    // Function to calculate the subsequence sum
    public static long printKMissingSum(int nums[], int k)
    {
        HashSet s = new HashSet();
        for (int i : nums)
            s.add(i);
        int a = 0, b = 0, prev = 0, cnt = 0;
 
        // Create one variable ans
        long ans = 0;
 
        // Loop to calculate the sum
        Iterator it = s.iterator();
        int tempk = k;
        while (it.hasNext() && k > 0) {
            b = it.next();
            if (b < 0) {
                a = 0;
                prev = 0;
                continue;
            }
 
            a = (k == tempk ? 0 : prev);
            cnt = b - 1 - a;
 
            if (cnt <= k) {
                ans += sumN(b - 1) - sumN(a);
                k -= cnt;
            }
            else {
                ans += sumN(a + k) - sumN(a);
                k -= k;
            }
            prev = b;
        }
        if (k > 0) {
            ans += sumN(prev + k) - sumN(prev);
            k -= k;
        }
        return ans;
    }
    public static void main(String[] args)
    {
        int arr[] = { -2, -3, -4 };
        int K = 2;
 
        System.out.print(printKMissingSum(arr, K));
    }
}
 
// This code is contributed by Rohit Pradhan


Python3
# Python program for the above approach
 
# Function to find the sum between 0 to n
def sumN(n):
    return (n * (n + 1)) / 2
 
# Function to calculate the subsequence sum
def printKMissingSum(nums, k):
    s = set(nums)
    s = list(s)
    ans = 0
    itr = 0
    while itr < len(s) and k > 0:
        b = s[itr]
        if b < 0:
            a = 0
            prev = 0
            break
 
        a = 0 if itr == 0 else prev
        cnt = b - 1 - a
 
        if cnt <= k:
            ans += sumN(b - 1) - sumN(a)
            k -= cnt
        else:
            ans += sumN(a + k) - sumN(a)
            k -= k
        prev = b
 
        itr += 1
    if k > 0:
        ans += sumN(prev + k) - sumN(a)
        k -= k
    return int(ans)
 
# Driver code
nums = [-2, -3, -4]
k = 2
print(printKMissingSum(nums, k))
 
# This code is contributed by amnindersingh1414.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Function to find the sum between 0 to n
  static long sumN(long n)
  {
    return n * (n + 1) / 2;
  }
 
  // Function to calculate the subsequence sum
  static long printKMissingSum(int[] nums, int k)
  {
    HashSet s = new HashSet();
    foreach (var i in nums)
      s.Add(i);
    int a = 0, b = 0, prev = 0, cnt = 0;
 
    // Create one variable ans
    long ans = 0;
 
    // Loop to calculate the sum
    HashSet.Enumerator it = s.GetEnumerator();
    int tempk = k;
    while (it.MoveNext() && k > 0) {
      b = it.Current;
      if (b < 0) {
        a = 0;
        prev = 0;
        continue;
      }
 
      a = (k == tempk ? 0 : prev);
      cnt = b - 1 - a;
 
      if (cnt <= k) {
        ans += sumN(b - 1) - sumN(a);
        k -= cnt;
      }
      else {
        ans += sumN(a + k) - sumN(a);
        k -= k;
      }
      prev = b;
    }
    if (k > 0) {
      ans += sumN(prev + k) - sumN(prev);
      k -= k;
    }
    return ans;
  }
 
  static public void Main ()
  {
    int[] arr = { -2, -3, -4 };
    int K = 2;
 
    Console.Write(printKMissingSum(arr, K));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript



输出
3

时间复杂度 O(N * logN)
辅助空间 O(N)