Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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++

// 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;
}

                    

Java

// 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

                    

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#

// 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

                    

PHP

<?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)

                    

Javascript

<script>
 
// Javascript  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)
    {
        document.write( "No solution");
        return;
    }
 
    // print first element as y-n+1
    document.write( y - n + 1);
 
    // print rest n-1 elements as 1
    while (n-- > 1)
    document.write( "<br>" + 1);
}
 
// Driver code
 
// initialize the number of elements
// and the value of x an y
let n = 5;
let x = 15;
let y = 15;
 
findsolution(n, x, y);
 
 
// This code is contributed
// by bobby
 
</script>

                    

Output: 
11
1
1
1
1

 

Time Complexity: O(n)

Auxiliary Space: O(1)



Last Updated : 21 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads