Maximum possible sum of squares of stack elements satisfying the given properties
Last Updated :
09 Dec, 2021
Given two integers S and N, the task is to find the maximum possible sum of squares of N integers that can be placed in a stack such that the following properties are satisfied:
- The integer at the top of the stack should not be smaller than the element immediately below it.
- All stack elements should be in the range [1, 9].
- The Sum of stack elements should be exactly equal to S.
If it is impossible to obtain such an arrangement, print -1.
Examples:
Input: S = 12, N = 3
Output: 86
Explanation:
Stack arrangement [9, 2, 1] generates the sum 12 (= S), thus, satisfying the properties.
Therefore, maximum possible sum of squares = 9 * 9 + 2 * 2 + 1 * 1= 81 + 4 + 1 = 86
Input: S = 11, N = 1
Output: -1
Approach: Follow the steps below to solve the problem:
- Check if S is valid, i.e. if it lies within the range [N, 9 * N].
- Initialize a variable, say res to store the maximum sum of squares of stack elements.
- The minimum value of an integer in the stack can be 1, so initialize all the stack elements with 1. Hence, deduct N from S.
- Now, check the number of integers having a value of more than 1. For this, add 8 (if it is possible) to the integers starting from the base of the stack and keep on adding it until S > 0.
- In the end, add S % 8 to the current integer and fill all remaining stack elements with 1.
- Finally, add the sum of squares of stack elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxSumOfSquares( int N,
int S)
{
int res = 0;
if (S < N || S > 9 * N)
{
cout << (-1);
return ;
}
S = S - N;
int c = 0;
while (S > 0)
{
c++;
if (S / 8 > 0)
{
res += 9 * 9;
S -= 8;
}
else
{
res += (S + 1) *
(S + 1);
break ;
}
}
res = res + (N - c);
cout << (res);
}
int main()
{
int N = 3;
int S = 12;
maxSumOfSquares(N, S);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void maxSumOfSquares(
int N, int S)
{
int res = 0 ;
if (S < N || S > 9 * N) {
System.out.println(- 1 );
return ;
}
S = S - N;
int c = 0 ;
while (S > 0 ) {
c++;
if (S / 8 > 0 ) {
res += 9 * 9 ;
S -= 8 ;
}
else {
res += (S + 1 )
* (S + 1 );
break ;
}
}
res = res + (N - c);
System.out.println(res);
}
public static void main(String args[])
{
int N = 3 ;
int S = 12 ;
maxSumOfSquares(N, S);
}
}
|
Python3
def maxSumOfSquares(N, S):
res = 0
if (S < N or S > 9 * N):
cout << - 1 ;
return
S = S - N
c = 0
while (S > 0 ):
c + = 1
if (S / / 8 > 0 ):
res + = 9 * 9
S - = 8
else :
res + = (S + 1 ) * (S + 1 )
break
res = res + (N - c)
print (res)
if __name__ = = '__main__' :
N = 3
S = 12
maxSumOfSquares(N, S)
|
C#
using System;
class GFG{
public static void maxSumOfSquares( int N,
int S)
{
int res = 0;
if (S < N || S > 9 * N)
{
Console.WriteLine(-1);
return ;
}
S = S - N;
int c = 0;
while (S > 0)
{
c++;
if (S / 8 > 0)
{
res += 9 * 9;
S -= 8;
}
else
{
res += (S + 1) *
(S + 1);
break ;
}
}
res = res + (N - c);
Console.WriteLine(res);
}
public static void Main(String []args)
{
int N = 3;
int S = 12;
maxSumOfSquares(N, S);
}
}
|
Javascript
<script>
function maxSumOfSquares(N , S) {
var res = 0;
if (S < N || S > 9 * N) {
document.write(-1);
return ;
}
S = S - N;
var c = 0;
while (S > 0) {
c++;
if (parseInt(S / 8) > 0) {
res += 9 * 9;
S -= 8;
}
else {
res += (S + 1) * (S + 1);
break ;
}
}
res = res + (N - c);
document.write(res);
}
var N = 3;
var S = 12;
maxSumOfSquares(N, S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...