Write this solution in C language. You are given an array A of N positive integers and an integer K. Find the largest po

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Write this solution in C language. You are given an array A of N positive integers and an integer K. Find the largest po

Post by answerhappygod »

Write this solution in C language.
You are given an array A of N positive integers and an integerK. Find the largest possible even sum of K elements at differentpositions in A.
For example given A = [4, 9, 8, 2, 6] and K = 3, the largesteven sum of three elements is 18. The three selected elements areA(0] = 4, A[2] = 8 and A[4] = 6.
Write a function:int solution (int All, int N, int K): that, given an array A of Npositive integers and positive integer K, returns the largest evensum of K elements. If there are no such K elements, return -1.
Examples:1. Given A = [4, 9, 8, 2, 6] and K = 3, the function should return18, as explained above.2. Given A = [5, 6, 3, 4, 2] and K = 5, the function should return20. There are five elements and they sum to 20.3. Given A = [7, 7, 7, 7, 7) and K = 1, the function should return-1, as we can pick only one element and there are no evenones.4. Given A = [10000] and K = 2, the function should return -1because K is larger than N.5. Given A = [2, 3, 3, 5, 5], K = 3, the function should return 12,because 5 + 5 + 2 = 12. Write an efficient algorithm for thefollowing assumptions: ? N and k are integers within the range[1..100,000]; each element of array A is an integer within therange [1..10,000].
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply