Sum of first K natural numbers missing in given Array Last Updated : 08 Apr, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of size N and a number K, the task is to find the sum of the first K natural numbers that are not present in the given array. Examples: Input: arr[] = {2, 3, 4}, K = 3Output: 12Explanation: First 3 Missing numbers are: [1, 5, 6] Input: arr[] = {-2, -3, 4}, K = 2Output: 3Explanation: Missing numbers are: [1 2] Approach: The problem can be solved based on the following idea: Store the positive numbers in the set and calculate the sums of the numbers present between two numbers of set using the formula for sum of all numbers from L to R (say X) = (R-1)*R/2 - (L+1)*L/2. Check for all the intervals between two numbers and calculate their sum till K elements are not considered. Follow the steps mentioned below to solve the problem: Create a sorted set from array elements to remove all duplicates.Then for each positive numbers from starting in set:Find count of numbers in between 2 positive numbers in the setIf the count is smaller than K, find the sum of numbers in between those 2 numbers in the set and reduce K by countIf the count is greater than K, find the sum of only K numbers from the previous positive number and reduce K to 0Return the sum Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> 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<int>& nums, int k) { set<int> 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<int> 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<Integer> s = new HashSet<Integer>(); 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<Integer> 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<int> s = new HashSet<int>(); 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<int>.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 <script> // JavaScript program for the above approach // Function to find the sum between 0 to n const sumN = (n) => { return n * (n + 1) / 2; } // Function to calculate the subsequence sum const printKMissingSum = (nums, k) => { let s = new Set(); for (let itm in nums) s.add(nums[itm]); s = Array.from(s); let a, b, prev, cnt; // Create one variable ans let ans = 0; // Loop to calculate the sum for (let itr = 0; itr < s.length && k > 0; itr++) { b = s[itr]; if (b < 0) { a = 0; prev = 0; continue; } a = (itr == 0 ? 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 let arr = [-2, -3, -4]; let K = 2; document.write(printKMissingSum(arr, K)); // This code is contributed by rakeshsahni </script> Output3 Time Complexity: O(N * logN)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Sum of first K natural numbers missing in given Array C code_r Follow Improve Article Tags : Mathematical Geeks Premier League DSA Arrays Geeks-Premier-League-2022 cpp-set +1 More Practice Tags : ArraysMathematical Similar Reads Find first k natural numbers missing in given array Given an array of size n and a number k, we need to print first k natural numbers that are not there in the given array. Examples: Input : [2 3 4] k = 3 Output : [1 5 6] Input : [-2 -3 4] k = 2 Output : [1 2]Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. Sort th 9 min read Find if given number is sum of first n natural numbers Given a number s (1 <= s <= 1000000000). If this number is the sum of first n natural number then print n, otherwise print -1. Examples: Input: s = 10Output: n = 4Explanation: 1 + 2 + 3 + 4 = 10 Input: s = 17Output: n = -1Explanation: 17 can't be expressed as a sum of first n natural numbers a 8 min read Missing in a Sorted Array of Natural Numbers Given a sorted array arr[] of n-1 integers, these integers are in the range of 1 to n. There are no duplicates in the array. One of the integers is missing in the array. Write an efficient code to find the missing integer. Examples: Input : arr[] = [1, 2, 3, 4, 6, 7, 8]Output : 5Explanation: The mis 12 min read Count of Missing Numbers in a sorted array Given a sorted array arr[], the task is to calculate the number of missing numbers between the first and last element of the sorted array. Examples: Input: arr[] = { 1, 4, 5, 8 } Output: 4 Explanation: The missing integers in the array are {2, 3, 6, 7}. Therefore, the count is 4. Input: arr[] = {5, 9 min read Sum of first N natural numbers which are not powers of K Given two integers n and k , the task is to find the sum of all the numbers within the range [1, n] excluding the numbers which are positive powers of k i.e. the numbers k, k2, k3 and so on.Examples: Input: n = 10, k = 3 Output: 43 1 + 2 + 4 + 5 + 6 + 7 + 8 + 10 = 43 3 and 9 are excluded as they are 9 min read Sum of first K numbers which are not divisible by N Given two numbers N and K, the task is to find the sum of first K numbers which are not divisible by N.Examples: Input: N = 5, K = 10 Output: 63 Explanation: Sum of { 1, 2, 3, 4, 6, 7, 8, 9, 11, 12 } is 63. Input: N = 3, k = 13 Output: 127 Explanation: Sum of { 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 9 min read Find K missing numbers from given Array in range [1, M] such that total average is X Given an array arr[] of integers of size N where each element can be in the range [1, M], and two integers X and K. The task is to find K possible numbers in range [1, M] such that the average of all the (N + K) numbers is equal to X. If there are multiple valid answers, any one is acceptable. Examp 6 min read Kth Missing Positive Number in a Sorted Array Given a sorted array of distinct positive integers arr[] and integer k, the task is to find the kth positive number that is missing from arr[].Examples : Input: arr[] = [2, 3, 4, 7, 11], k = 5Output: 9Explanation: Missing are 1, 5, 6, 8, 9, 10, ... and 5th missing number is 9.Input: arr[] = [1, 2, 3 10 min read Sum of all the elements in an array divisible by a given number K Given an array containing N elements and a number K. The task is to find the sum of all such elements which are divisible by K. Examples: Input : arr[] = {15, 16, 10, 9, 6, 7, 17} K = 3 Output : 30 Explanation: As 15, 9, 6 are divisible by 3. So, sum of elements divisible by K = 15 + 9 + 6 = 30. Inp 13 min read k-th missing element in an unsorted array Given an unsorted sequence a[], the task is to find the K-th missing contiguous element in the increasing sequence of the array elements i.e. consider the array in sorted order and find the kth missing number. If no k-th missing element is there output -1. Note: Only elements exists in the range of 6 min read Like