Thursday, November 28, 2024
Google search engine
HomeData Modelling & AIFind N integers with given difference between product and sum

Find N integers with given difference between product and sum

Given two integers N and D, Find a set of N integers such that the difference between their product and sum is equal to D.

Examples:  

Input : N = 2, D = 1
Output : 2 3
Explanation: 
product = 2*3 = 6,
Sum = 2 + 3 = 5.
Hence, 6 - 5 = 1(D).

Input : N = 3, D = 5.
Output : 1 2 8
Explanation :
Product = 1*2*8 = 16
Sum = 1+2+8 = 11.
Hence, 16-11 = 5(D).

A tricky solution is to keep the difference D to choose N numbers as N-2 ‘1’s, one ‘2’ and one remaining number as ‘N+D’. 
Sum = (N-2)*(1) + 2 + (N+D) = 2*N + D. 
Product = 1*2*(N+D) = 2*N+2*D 
Difference = (2*N+2*D) – (2*N+D) = D. 

C++




// CPP code to generate numbers
// with difference between
// product and sum is D
#include <iostream>
using namespace std;
 
// Function to implement calculation
void findNumbers(int n, int d)
{
    for (int i = 0; i < n - 2; i++)
        cout << "1"  << " ";
 
    cout << "2" << " ";
    cout << n + d << endl;
}
 
// Driver code
int main()
{
    int N = 3, D = 5;
    findNumbers(N, D);
    return 0;
}


Java




// Java code to generate numbers
// with difference between
// product and sum is D
import java.io.*;
 
class GFG {
     
    // Function to implement calculation
    static void findNumbers(int n, int d)
    {
        for (int i = 0; i < n - 2; i++)
            System.out.print("1" + " ");
     
        System.out.print("2" + " ");
        System.out.println(n + d);
    }
     
    // Driver code
    public static void main(String args[])
    {
        int N = 3, D = 5;
        findNumbers(N, D);
    }
}
 
/* This code is contributed by Nikita Tiwari.*/


Python3




# Python3 code to generate numbers with
# difference between product and sum is D
 
# Function to implement calculation
def pattern(n, d) :
     
    for i in range(0, n - 2) :
        print("1", end=" ")
         
    print("2", end=" ")
    print(n + d)
 
# Driver code
N = 3
D = 5
pattern(N, D)
 
 
# This code is contributed by 'Akanshgupta'


C#




// C# code to generate numbers
// with difference between
// product and sum is D
using System;
 
class GFG {
     
    // Function to implement calculation
    static void findNumbers(int n, int d)
    {
        for (int i = 0; i < n - 2; i++)
        Console.Write("1" + " ");
     
        Console.Write("2" + " ");
        Console.Write(n + d);
    }
     
    // Driver code
    public static void Main()
    {
        int N = 3, D = 5;
        findNumbers(N, D);
    }
}
 
/* This code is contributed by vt_m.*/


PHP




<?php
// PHP code to generate numbers
// with difference between
// product and sum is D
 
// Function to implement
// calculation
function findNumbers($n, $d)
{
    for ($i = 0; $i < $n - 2; $i++)
        echo "1" ," ";
 
    echo "2" , " ";
    echo $n + $d ,"\n";
}
 
    // Driver Code
    $N = 3;
    $D = 5;
    findNumbers($N, $D);
 
// This code is contributed by ajit
?>


Javascript




<script>
 
// JavaScript program to generate numbers
// with difference between
// product and sum is D
 
// Function to implement calculation
    function findNumbers(n, d)
    {
        for (let i = 0; i < n - 2; i++)
            document.write("1" + " ");
       
        document.write("2" + " ");
       document.write(n + d);
    }
       
 
// Driver code
 
        let N = 3, D = 5;
        findNumbers(N, D);
            
</script>


Output : 

 1 2 8

Time complexity : O(n) 
Auxiliary Space : O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments