📜  第K个子集的最大值和最小值之和,按子集总和递增

📅  最后修改于: 2021-05-17 03:31:46             🧑  作者: Mango

给定一个整数N和的N的所有非负功率为S = {N 0,N 1,N 2,N 3,…}组,安排在增加子集和的顺序S的所有非空子集。任务是从该顺序中找到第K个子集的最大和最小元素之和。

例子:

方法:此方法基于功率设定的概念。请按照以下步骤解决问题:

  1. 以相反的顺序生成整数K的对应二进制表达式(即子集的位置),以保持子集中元素的和不断增加。
  2. 然后,根据lst []列表中连续位置上出现的位,计算子集中相应位置是否存在元素。
  3. 迭代lst [] ,如果lst [i]0 ,则忽略该位,否则( N i )* lst [i] 在第K个子num []中
  4. 然后,通过在位置0和最后位置取num []的元素来计算总和。
  5. 完成上述步骤后,打印结果总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find the sum of greatest
// and smallest element of Kth Subset
void sumofExtremes(int N, int K)
{
  
    // Stores the binary equivalent
    // of k in inverted order
    list lst;
  
    while (K > 0)
    {
        lst.push_back(K % 2);
        K = K / 2;
    }
  
    // Stores the kth subset
    list num;
  
    int x = 0;
  
    // Iterate over the list
    for(auto element : lst)
    {
          
        // If the element is non-zero
        if (element != 0) 
        {
            int a = pow(N, x);
            a = a * (element);
            num.push_back(a);
        }
        x++;
    }
      
    // Update S to length of num
    int s = num.size();
  
    // If length of the subset is 1
    if (s == 1)
  
        // Print twice of that element
        cout << (2 * num.front()) << "\n";
  
    // Print the sum of first and
    // last element
    else
        cout << num.front() + num.back();
}
  
// Driver Code
int main()
{
      
    // Given number N
    int N = 4;
  
    // Given position K
    int K = 6;
  
    sumofExtremes(N, K);
}
  
// This code is contributed by akhilsaini


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
  
class GFG{
  
// Function to find the sum of greatest
// and smallest element of Kth Subset
public static void sumofExtremes(int N, int K)
{
      
    // Stores the binary equivalent
    // of k in inverted order
    List lst = new ArrayList();
  
    while (K > 0) 
    {
        lst.add(K % 2);
        K = K / 2;
    }
  
    // Stores the kth subset
    List num = new ArrayList();
  
    int x = 0;
  
    // Iterate over the list
    for(int element : lst)
    {
          
        // If the element is non-zero
        if (element != 0) 
        {
            int a = (int)Math.pow(N, x);
            a = a * (element);
            num.add(a);
        }
        x++;
    }
      
    // Update S to length of num
    int s = num.size();
  
    // If length of the subset is 1
    if (s == 1)
  
        // Print twice of that element
        System.out.println(2 * num.get(0));
  
    // Print the sum of first and
    // last element
    else
        System.out.println(num.get(0) + 
                           num.get(s - 1));
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given number N
    int N = 4;
  
    // Given position K
    int K = 6;
  
    // Function call
    sumofExtremes(N, K);
}
}
  
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
  
# Function to find the sum of greatest
# and smallest element of Kth Subset
def sumofExtremes(N, K):
  
    # Stores the binary equivalent
    # of k in inverted order
    lst = []
  
    while K > 0:
        lst.append(K % 2)
        K = K//2
  
    # Stores the kth subset
    num = []
      
    # Iterate over the list
    for i in range(0, len(lst)):
          
        # If the element is non-zero
        if(lst[i] != 0):
            a = N**i
            a = a * lst[i]
            num.append(a)
      
    # Update S to length of num
    s = len(num)
  
    # If length of the subset is 1
    if(s == 1):
  
        # Print twice of that element
        print(2 * num[0])
  
  
    # Print the sum of first and
    # last element
    else:
        print(num[0] + num[s - 1])
  
  
# Driver Code
  
# Given number N
N = 4
  
# Given position K
K = 6
  
# Function Call
sumofExtremes(N, K)


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to find the sum of greatest
// and smallest element of Kth Subset
public static void sumofExtremes(int N, int K)
{
      
    // Stores the binary equivalent
    // of k in inverted order
    List lst = new List();
  
    while (K > 0) 
    {
        lst.Add(K % 2);
        K = K / 2;
    }
  
    // Stores the kth subset
    List num = new List();
  
    // Iterate over the list
    for(int i = 0; i < lst.Count; i++)
    {
          
        // If the element is non-zero
        if (lst[i] != 0) 
        {
            int a = (int)Math.Pow(N, i);
            a = a * (lst[i]);
            num.Add(a);
        }
    }
      
    // Update S to length of num
    int s = num.Count;
  
    // If length of the subset is 1
    if (s == 1)
  
        // Print twice of that element
        Console.WriteLine(2 * num[0]);
  
    // Print the sum of first and
    // last element
    else
        Console.WriteLine(num[0] + num[s - 1]);
}
  
// Driver Code
static public void Main()
{
      
    // Given number N
    int N = 4;
  
    // Given position K
    int K = 6;
  
    // Function call
    sumofExtremes(N, K);
}
}
  
// This code is contributed by akhilsaini


输出:
20



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