Given two integers **N** and **K**, the task is to find **K** numbers(**A _{1}, A_{2}, …, A_{K}**) such that

**∑**is equal to

_{i=1}^{K}A_{i}**N**and

**∑**is maximum.

_{i=1}^{K}A_{i}^{2}**Examples:**

Input:N = 3, K = 2

Output:1 2

Explanation:The two numbers are 1 and 2 as their sum is equal to N and 1^{2}+ 2^{2}is maximum.

Input:N = 10, K = 3

Output:1 8 1

**Approach:** The idea is to take number **1**, **K – 1** times and number **N – K + 1** once. The sum of these numbers is equal to **N** and sum of squares of these numbers is always maximum. For any two non-negative numbers **a** and **b**, **(a ^{2} + b^{2})** is always less than

**1 + (a + b – 1)**.

^{2}Below is the implementation of the above approach:

## C++

`// C++ program to find K numbers ` `// with sum equal to N and the ` `// sum of their squares maximized ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that prints the ` `// required K numbers ` `void` `printKNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Print 1, K-1 times ` ` ` `for` `(` `int` `i = 0; i < K - 1; i++) ` ` ` `cout << 1 << ` `" "` `; ` ` ` ` ` `// Print (N-K+1) ` ` ` `cout << (N - K + 1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 10, K = 3; ` ` ` ` ` `printKNumbers(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find K numbers ` `// with sum equal to N and the ` `// sum of their squares maximized ` `class` `GFG{ ` ` ` `// Function that prints the ` `// required K numbers ` `static` `void` `printKNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Print 1, K-1 times ` ` ` `for` `(` `int` `i = ` `0` `; i < K - ` `1` `; i++) ` ` ` `System.out.print(` `1` `+ ` `" "` `); ` ` ` ` ` `// Print (N - K + 1) ` ` ` `System.out.print(N - K + ` `1` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `10` `, K = ` `3` `; ` ` ` `printKNumbers(N, K); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find K numbers ` `# with a sum equal to N and the ` `# sum of their squares maximized ` ` ` `# Function that prints the ` `# required K numbers ` `def` `printKNumbers(N, K): ` ` ` ` ` `# Print 1, K-1 times ` ` ` `for` `i ` `in` `range` `(K ` `-` `1` `): ` ` ` `print` `(` `1` `, end ` `=` `' '` `) ` ` ` ` ` `# Print (N-K+1) ` ` ` `print` `(N ` `-` `K ` `+` `1` `) ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` ` ` `(N, K) ` `=` `(` `10` `, ` `3` `) ` ` ` ` ` `printKNumbers(N, K) ` ` ` `# This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find K numbers ` `// with sum equal to N and the ` `// sum of their squares maximized ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that prints the ` `// required K numbers ` `static` `void` `printKNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Print 1, K-1 times ` ` ` `for` `(` `int` `i = 0; i < K - 1; i++) ` ` ` `Console.Write(1 + ` `" "` `); ` ` ` ` ` `// Print (N - K + 1) ` ` ` `Console.Write(N - K + 1); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 10, K = 3; ` ` ` ` ` `printKNumbers(N, K); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

**Output:**

1 1 8

**Time Complexity:** *O(K)*

**Auxiliary Space:** *O(1)*

## Recommended Posts:

- Find two numbers such that difference of their squares equal to N
- Find all factorial numbers less than or equal to n
- Find N distinct numbers whose bitwise Or is equal to K
- Find maximum product of digits among numbers less than or equal to N
- Find two numbers whose difference of fourth power is equal to N
- Count of pairs of integers whose difference of squares is equal to N
- Subsets of size K with product equal to difference of two perfect squares
- Sum of squares of first n natural numbers
- Sum of squares of Fibonacci numbers
- Sum of squares of first n natural numbers
- Print squares of first n natural numbers without using *, / and -
- Number of perfect squares between two given numbers
- Average of Squares of Natural Numbers
- Sum of alternating sign Squares of first N natural numbers
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Find a point that lies inside exactly K given squares
- Puzzle | Program to find number of squares in a chessboard
- Find the side of the squares which are inclined diagonally and lined in a row

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.