Given two integers N and D, Find a set of N integers such that 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; ` `} ` |

*chevron_right*

*filter_none*

## 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.*/` |

*chevron_right*

*filter_none*

## 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' ` |

*chevron_right*

*filter_none*

## 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.*/` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

Output :

1 2 8

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find distinct integers for a triplet with given product
- Sum of series formed by difference between product and sum of N natural numbers
- Count of subsequences whose product is a difference of square of two integers
- Sum of product of all integers upto N with their count of divisors
- Find a pair with maximum product in array of Integers
- Maximum GCD of N integers with given product
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Absolute difference between sum and product of roots of a quartic equation
- Find an index such that difference between product of elements before and after it is minimum
- Find sum in range L to R in given sequence of integers
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Find sum of product of every number and its frequency in given range
- Check whether product of integers from a to b is positive , negative or zero
- Maximum possible GCD for a pair of integers with product N
- Program for dot product and cross product of two vectors
- Find sum of product of number in given series
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference

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.