Find n positive integers that satisfy the given equations | C ++ implementation of the approach ; Function to find n positive integers that satisfy the given conditions ; To store n positive integers ; Place N - 1 one 's ; If can not place ( y - ( n - 1 ) ) as the Nth integer ; Place Nth integer ; To store the sum of squares of N integers ; If it is less than x ; Print the required integers ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }