Given an integer N and a set of all non-negative powers of N as S = {N0, N1, N2, N3, … }, arrange all non-empty subsets of S in increasing order of subset-sum. The task is to find the sum of the greatest and smallest elements of the Kth subset from that ordering.
Examples:
Input: N = 4, K = 3
Output: 5
Explanation:
S = {1, 4, 16, 64, …}.
Therefore, the non-empty subsets arranged in increasing order of their sum = {{1}, {4}, {1, 4}, {16}, {1, 16}, {4, 16}, {1, 4, 16}………}.
So the elements of the subset in Kth(3rd) subset are {1, 4}. So the sum of the greatest and smallest elements of this subset = (4 + 1) = 5.
Input: N = 3, K = 4
Output: 18
Explanation:
S = {1, 3, 9, 27, 81, …}.
Therefore, the non-empty subsets arranged in increasing order of their sum = {{1}, {3}, {1, 3}, {9}, {1, 9}, {3, 9}, {1, 3, 9} ……..}.
So the element in the subset at 4th position is {9}. So the sum of the greatest and smallest elements of this subset = (9 + 9) = 18.
Approach: This approach is based on the concept of Power-set. Follow the steps below to solve the problem:
- Generate the corresponding binary expression of the integer K (i.e., the position of the subset) in inverse order to maintain the increasing sum of elements in the subset.
- Then calculate whether there will be an element in the subset at corresponding positions depending on the bits present in lst[] list at successive positions.
- Iterate over the lst[] and if lst[i] is 0, then ignore that bit, otherwise (Ni)*lst[i] will be in the Kth Subset num[].
- Then the sum is calculated by taking the element of the num[] at position 0 and at the last position.
- Print the resultant sum after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sumofExtremes( int N, int K)
{
list< int > lst;
while (K > 0)
{
lst.push_back(K % 2);
K = K / 2;
}
list< int > num;
int x = 0;
for ( auto element : lst)
{
if (element != 0)
{
int a = pow (N, x);
a = a * (element);
num.push_back(a);
}
x++;
}
int s = num.size();
if (s == 1)
cout << (2 * num.front()) << "\n" ;
else
cout << num.front() + num.back();
}
int main()
{
int N = 4;
int K = 6;
sumofExtremes(N, K);
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static void sumofExtremes( int N, int K)
{
List<Integer> lst = new ArrayList<Integer>();
while (K > 0 )
{
lst.add(K % 2 );
K = K / 2 ;
}
List<Integer> num = new ArrayList<Integer>();
int x = 0 ;
for ( int element : lst)
{
if (element != 0 )
{
int a = ( int )Math.pow(N, x);
a = a * (element);
num.add(a);
}
x++;
}
int s = num.size();
if (s == 1 )
System.out.println( 2 * num.get( 0 ));
else
System.out.println(num.get( 0 ) +
num.get(s - 1 ));
}
public static void main(String[] args)
{
int N = 4 ;
int K = 6 ;
sumofExtremes(N, K);
}
}
|
Python3
def sumofExtremes(N, K):
lst = []
while K > 0 :
lst.append(K % 2 )
K = K / / 2
num = []
for i in range ( 0 , len (lst)):
if (lst[i] ! = 0 ):
a = N * * i
a = a * lst[i]
num.append(a)
s = len (num)
if (s = = 1 ):
print ( 2 * num[ 0 ])
else :
print (num[ 0 ] + num[s - 1 ])
N = 4
K = 6
sumofExtremes(N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void sumofExtremes( int N, int K)
{
List< int > lst = new List< int >();
while (K > 0)
{
lst.Add(K % 2);
K = K / 2;
}
List< int > num = new List< int >();
for ( int i = 0; i < lst.Count; i++)
{
if (lst[i] != 0)
{
int a = ( int )Math.Pow(N, i);
a = a * (lst[i]);
num.Add(a);
}
}
int s = num.Count;
if (s == 1)
Console.WriteLine(2 * num[0]);
else
Console.WriteLine(num[0] + num[s - 1]);
}
static public void Main()
{
int N = 4;
int K = 6;
sumofExtremes(N, K);
}
}
|
Javascript
<script>
function sumofExtremes(N, K)
{
let lst = [];
while (K > 0)
{
lst.push(K % 2);
K = Math.floor(K / 2);
}
let num = [];
let x = 0;
for (let element of lst)
{
if (element != 0)
{
let a = Math.pow(N, x);
a = a * (element);
num.push(a);
}
x++;
}
let s = num.length;
if (s == 1)
document.write((2 * num[0]) + "+" );
else
document.write(num[0] + num[num.length - 1]);
}
let N = 4;
let K = 6;
sumofExtremes(N, K);
</script>
|
Time Complexity: O(log K)
Auxiliary Space: O(N)