Given two integers N and K, the task is to find K numbers(A1, A2, …, AK) such that ∑i=1KAi is equal to N and ∑i=1KAi2 is maximum.
Input: N = 3, K = 2
Output: 1 2
Explanation: The two numbers are 1 and 2 as their sum is equal to N and 12 + 22 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, (a2 + b2) is always less than 1 + (a + b – 1)2.
Below is the implementation of the above approach:
1 1 8
Time Complexity: O(K)
Auxiliary Space: O(1)
- Find two numbers such that difference of their squares equal to N
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count array elements that can be maximized by adding any permutation of first N natural numbers
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Find the first N integers such that the sum of their digits is equal to 10
- Find any K distinct odd integers such that their sum is equal to N
- Find the pair (a, b) with minimum LCM such that their sum is equal to N
- Find two numbers from their sum and XOR
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized
- Pick points from array such that minimum distance is maximized
- Generate an Array such with elements maximized through swapping bits
- Check if Array elements can be maximized upto M by adding all elements from another array
- Program to find if two numbers and their AM and HM are present in an array using STL
- Count of pairs from Array with sum equal to twice their bitwise AND
- Find three integers less than or equal to N such that their LCM is maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.