Find ‘N’ number of solutions with the given inequality equations

Find the value of a1, a2, a3, ….an such that the following two conditions are satisfied.
 a_1^2 + a_2^2 + a_3^2 + ....+ a_n^2 \geq X
a_1 + a_2 + a_3 + ....+ a_n \leq Y
Print the value of a1, a2, …, an and “No solution” otherwise.
Note: There maybe a several solutions, print any of them .

Examples:

Input: n = 5, x = 15, y = 15
Output:
11
1
1
1
1
Input: n = 4, x = 324, y = 77
Output: 
74
1
1
1

Approach: Below is the step by step algorithm to solve this problem:

  1. Initialize the number of elements and the value of x and y.
  2. There is no solution of a1…a2 if y is less than n or if x is very larger than n.
  3. Print first solution as y – n + 1 and 1 as the solution of rest of the elements.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long
  
// Function to calculate all the solutions
void findsolution(ll n, ll x, ll y)
{
    // there is no solutions
    if ((y - n + 1) * (y - n + 1) + n - 1 < x || y < n) {
        cout << "No solution";
        return;
    }
  
    // print first element as y-n+1
    cout << y - n + 1;
  
    // print rest n-1 elements as 1
    while (n-- > 1)
        cout << endl
             << 1;
}
  
// Driver code
int main()
{
    // initialize the number of elements
    // and the value of x an y
    ll n, x, y;
    n = 5, x = 15, y = 15;
  
    findsolution(n, x, y);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java implementation of above approach
import java.io.*;
  
class GFG {
     
// Function to calculate all the solutions
static void findsolution(long n, long x, long y)
{
    // there is no solutions
    if ((y - n + 1) * (y - n + 1) + n - 1 < x || y < n) {
        System.out.println( "No solution");
        return;
    }
  
    // print first element as y-n+1
    System.out.println( y - n + 1);
  
    // print rest n-1 elements as 1
    while (n-- > 1)
            System.out.println( "1");
}
  
// Driver code
  
    public static void main (String[] args) {
            // initialize the number of elements
    // and the value of x an y
    long n, x, y;
    n = 5; x = 15; y = 15;
  
    findsolution(n, x, y);
    }
}
// This code is contributed 
// by ajit

chevron_right


Python3

# Python3 implementation of above approach

# Function to calculate all the solutions
def findsolution(n, x, y):

# there is no solutions
if ((y – n + 1) * (y – n + 1) +
n – 1 < x or y < n): print("No solution"); return; # print first element as y-n+1 print(y - n + 1); # print rest n-1 elements as 1 while (n > 1):
print(1);
n -= 1;

# Driver code

# initialize the number of elements
# and the value of x an y
n = 5;
x = 15;
y = 15;

findsolution(n, x, y);

# This code is contributed by mits

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
using System;
  
class GFG
{
      
// Function to calculate all the solutions 
static void findsolution(long n, 
                         long x, long y) 
    // there is no solutions 
    if ((y - n + 1) * (y - n + 1) + 
         n - 1 < x || y < n) 
    
        Console.WriteLine( "No solution"); 
        return
    
  
    // print first element as y-n+1 
    Console.WriteLine( y - n + 1); 
  
    // print rest n-1 elements as 1 
    while (n-- > 1) 
        Console.WriteLine( "1"); 
  
// Driver code 
static public void Main ()
{
    // initialize the number of elements 
    // and the value of x an y 
    long n, x, y; 
    n = 5; x = 15; y = 15; 
      
    findsolution(n, x, y); 
  
// This code is contributed 
// by ajit 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Function to calculate all the solutions
function findsolution($n, $x, $y)
{
    // there is no solutions
    if (($y - $n + 1) * ($y - $n + 1) + 
         $n - 1 < $x || $y < $n)
    {
        echo "No solution";
        return;
    }
  
    // print first element as y-n+1
    echo $y - $n + 1;
  
    // print rest n-1 elements as 1
    while ($n-- > 1)
    echo "\n" . 1;
}
  
// Driver code
  
// initialize the number of elements
// and the value of x an y
$n = 5; $x = 15; $y = 15;
  
findsolution($n, $x, $y);
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


Output:

11
1
1
1
1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.