Find n positive integers that satisfy the given equations
Last Updated :
08 Mar, 2022
Given three integers N, X, and Y. The task is to find N positive integers that satisfy the given equations.
- a12 + a22 + …. + an2 ? X
- a1 + a2 + …. + an ? Y
If no such sequence of integers is possible then print -1.
Examples:
Input: N = 3, X = 254, Y = 18
Output: 1 1 16
12 + 12 + 162 = 1 + 1 + 256 = 258 which is ? X
1 + 1 + 16 = 18 which is ? Y
Input: N = 2, X = 3, Y = 2
Output: -1
No such sequence exists.
Approach: It is easy to see that in order to maximize the sum of squares, one should make all numbers except the first one equal to 1 and maximize the first number. Keeping this in mind we only need to check whether the given value of y is large enough to satisfy a restriction that all n numbers are positive. If y is not too small, then all we need is to ensure that X ? 1 + 1 + … + (y – (n – 1))2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findIntegers( int n, int x, int y)
{
vector< int > ans;
for ( int i = 0; i < n - 1; i++)
ans.push_back(1);
if (y - (n - 1) <= 0) {
cout << "-1" ;
return ;
}
ans.push_back(y - (n - 1));
int store = 0;
for ( int i = 0; i < n; i++)
store += ans[i] * ans[i];
if (store < x) {
cout << "-1" ;
return ;
}
for ( int i = 0; i < n; i++)
cout << ans[i] << " " ;
}
int main()
{
int n = 3, x = 254, y = 18;
findIntegers(n, x, y);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findIntegers( int n, int x, int y)
{
ArrayList<Integer> ans = new ArrayList<Integer>();
for ( int i = 0 ; i < n - 1 ; i++)
ans.add( 1 );
if (y - (n - 1 ) <= 0 )
{
System.out.print( "-1" );
return ;
}
ans.add(y - (n - 1 ));
int store = 0 ;
for ( int i = 0 ; i < n; i++)
store += ans.get(i) * ans.get(i);
if (store < x)
{
System.out.print( "-1" );
return ;
}
for ( int i = 0 ; i < n; i++)
System.out.print(ans.get(i)+ " " );
}
public static void main (String[] args)
{
int n = 3 , x = 254 , y = 18 ;
findIntegers(n, x, y);
}
}
|
Python3
def findIntegers(n, x, y):
ans = []
for i in range (n - 1 ):
ans.append( 1 )
if (y - (n - 1 ) < = 0 ):
print ( "-1" , end = "")
return
ans.append(y - (n - 1 ))
store = 0
for i in range (n):
store + = ans[i] * ans[i]
if (store < x):
print ( "-1" , end = "")
return ;
for i in range (n):
print (ans[i], end = " " )
n, x, y = 3 , 254 , 18
findIntegers(n, x, y)
|
C#
using System;
using System.Collections;
class GFG
{
static void findIntegers( int n, int x, int y)
{
ArrayList ans = new ArrayList();
for ( int i = 0; i < n - 1; i++)
ans.Add(1);
if (y - (n - 1) <= 0)
{
Console.Write( "-1" );
return ;
}
ans.Add(y - (n - 1));
int store = 0;
for ( int i = 0; i < n; i++)
store += ( int )ans[i] *( int )ans[i];
if (store < x)
{
Console.Write( "-1" );
return ;
}
for ( int i = 0; i < n; i++)
Console.Write(( int )ans[i]+ " " );
}
static void Main()
{
int n = 3, x = 254, y = 18;
findIntegers(n, x, y);
}
}
|
PHP
<?php
function findIntegers( $n , $x , $y )
{
$ans = array ();
for ( $i = 0; $i < $n - 1; $i ++)
array_push ( $ans ,1) ;
if ( $y - ( $n - 1) <= 0)
{
echo "-1" ;
return ;
}
array_push ( $ans , $y - ( $n - 1));
$store = 0;
for ( $i = 0; $i < $n ; $i ++)
$store += $ans [ $i ] * $ans [ $i ];
if ( $store < $x )
{
echo "-1" ;
return ;
}
for ( $i = 0; $i < $n ; $i ++)
echo $ans [ $i ], " " ;
}
$n = 3; $x = 254; $y = 18;
findIntegers( $n , $x , $y );
?>
|
Javascript
<script>
function findIntegers(n, x, y)
{
let ans = [];
for (let i = 0; i < n - 1; i++)
ans.push(1);
if (y - (n - 1) <= 0)
{
document.write( "-1" );
return ;
}
ans.push(y - (n - 1));
let store = 0;
for (let i = 0; i < n; i++)
store += ans[i] * ans[i];
if (store < x)
{
document.write( "-1" );
return ;
}
for (let i = 0; i < n; i++)
document.write(ans[(i)]+ " " );
}
let n = 3, x = 254, y = 18;
findIntegers(n, x, y);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...