Open In App

Min sum of set of positive integers so that sum of no two elements is K

Improve
Improve
Like Article
Like
Save
Share
Report

Given two positive integers N and K. Create a set of size N containing distinct positive integers such that the sum of no two integers is K, the task is to find the minimum possible sum of the set that meets the above criteria. Since the answer may be large, print it modulo 10^9+7.

Note: It is guaranteed that it is always possible to make a set that meets the given conditions.

Examples:

Input: N = 2, K = 3
Output: 4
Explanation: Set can be {1, 3} with sum = 4, which is the minimum possible sum of the elements of set.

Input: N = 1, K = 1
Output: 1
Explanation: Set {1} can be chosen since there is no pair with K=1 so, 1 is the minimum possible sum of the elements of set.

Input: N = 4, K = 10
Output: 10
Explanation: Set {1, 2, 3, 4} can be chosen. There are no 2 elements with sum = k, so the minimum possible sum of the elements of set will be 1 + 2 + 3 + 4 = 10.

Approach: To solve the problem follow the below idea:

  • The approach uses a greedy strategy to generate the sequence of N positive integers, starting with 1. The algorithm keeps track of the count of generated integers and the sum of those integers. It also maintains an unordered set of integers that should not be included in the sequence, such that the sum of no two integers in the sequence is equal to K.
  • In each iteration, the algorithm generates the next integer in the sequence and check if it is greater than or equal to K, the algorithm adds the integer to the sum and increments the count of generated integers.
  • If the next integer is less than K, check if it is present in the unordered set. If not, the algorithm adds the integer to the sum, increments the count of generated integers, and insert K minus the integer, to the unordered set. This step ensures that no two integers in the sequence sum up to a multiple of K.
  • The algorithm continues the above process until N integers are generated, and it returns the minimum possible sum of those integers.

Below are the steps for the above approach:

  • Initialize a variable say sum = 1, cnt = 1, and num = 1.
  • Create an unordered_set s and insert the values 1 and k-1 into the set.
  • Run a loop till cnt < n.
  • In each iteration of the while loop, increment the value of num by 1.
  • Check if num ? K, update the sum += num, and increment the value of cnt by 1.
  • If the value of num < K, check if the value of num is not present in the set, update sum += num, increment the value of cnt by 1, and if num < K, insert the value K-num in the set.
  • When the while loop ends, return the final value of the sum.

Below is the implementation for the above approach:

C++14




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
 
int minSum(int& n, int& k)
{
    int sum = 1, cnt = 1, num = 1;
    unordered_set<int> s;
    s.insert(1);
    s.insert(k - 1);
 
    while (cnt < n) {
 
        num++;
 
        if (num >= k) {
            sum = (sum + num) % mod;
            cnt++;
            continue;
        }
 
        if (s.find(num) == s.end()) {
            sum = (sum + num) % mod;
 
            if (num < k)
                s.insert(k - num);
            cnt++;
        }
    }
 
    return sum;
}
 
// Driver's code
int main()
{
    int N = 4, K = 10;
 
    // Function Call
    cout << minSum(N, K);
    return 0;
}


Java




// Java code for the above approach:
import java.util.*;
 
class GFG {
    static final int MOD = 1000000007;
 
    static int minSum(int n, int k)
    {
        int sum = 1, cnt = 1, num = 1;
        Set<Integer> s = new HashSet<>();
        s.add(1);
        s.add(k - 1);
 
        while (cnt < n) {
            num++;
 
            if (num >= k) {
                sum = (sum + num) % MOD;
                cnt++;
                continue;
            }
 
            if (!s.contains(num)) {
                sum = (sum + num) % MOD;
 
                if (num < k)
                    s.add(k - num);
                cnt++;
            }
        }
        return sum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4, K = 10;
 
        // Function Call
        System.out.println(minSum(N, K));
    }
}
// This code is contributed by Prasad Kandekar(prasad264)


C#




// C# code for the above approach:
using System;
using System.Collections.Generic;
 
public class GFG{
 
    static int MOD = 1000000007;
 
    static int minSum(int n, int k)
    {
        int sum = 1, cnt = 1, num = 1;
        HashSet<int> s = new HashSet<int>();
        s.Add(1);
        s.Add(k - 1);
 
        while (cnt < n) {
            num++;
 
            if (num >= k) {
                sum = (sum + num) % MOD;
                cnt++;
                continue;
            }
 
            if (!s.Contains(num)) {
                sum = (sum + num) % MOD;
 
                if (num < k)
                    s.Add(k - num);
                cnt++;
            }
        }
        return sum;
    }
 
    // Driver Code
    static public void Main (){
 
        int N = 4, K = 10;
 
        // Function Call
        Console.WriteLine(minSum(N, K));
    }
}


Python3




# Python3 code for the above approach:
 
MOD = 10**9 + 7
 
def minSum(n: int, k: int) -> int:
    sum = 1
    cnt = 1
    num = 1
    s = set()
    s.add(1)
    s.add(k - 1)
 
    while cnt < n:
        num += 1
        if num >= k:
            sum = (sum + num) % MOD
            cnt += 1
            continue
 
        if num not in s:
            sum = (sum + num) % MOD
            if num < k:
                s.add(k - num)
            cnt += 1
 
    return sum
 
 
# Driver's code
N = 4
K = 10
print(minSum(N, K))


Javascript




// JavaScript code for the above approach:
 
// JS code for the above approach:
 
const MOD = 1000000007;
 
function minSum(n, k) {
  let sum = 1;
  let cnt = 1;
  let num = 1;
  const s = new Set();
  s.add(1);
  s.add(k - 1);
 
  while (cnt < n) {
    num++;
 
    if (num >= k) {
      sum = (sum + num) % MOD;
      cnt++;
      continue;
    }
 
    if (!s.has(num)) {
      sum = (sum + num) % MOD;
 
      if (num < k) {
        s.add(k - num);
      }
 
      cnt++;
    }
  }
 
  return sum;
}
 
// Driver's code
const N = 4;
const K = 10;
console.log(minSum(N, K));


Output

10

Time Complexity: O(N)
Auxiliary Space: O(N)



Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads