Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to place exactly 2 elements in every 2x2 sub array in nxn int array such that the sum is maximum?

for example, if you have 3x3 array [5 1 4] [1 8 2 ] [1 7 1] you will place in the following way (every 2x2 sub array we place exactly 2 elements denoted as p) to maximize the sum

[p 1 p] [1 p 2] [1 p 1] such that sum = 5+4+8+7=24

if you place like [p 1 p] [p 8 p] [1 p 1] will give sum = 5+4+1+2+7=19 which is not maximum.


What I have tried:

i tried so hard. but cannot seem to figure out a way to do it. brute force will time out.
Posted
Updated 23-Jan-21 10:01am
Comments
Patrice T 23-Jan-21 15:07pm    
Show your brute force code.

1 solution

So you want to first find each unique element. I would do that with a list. Go through them all, and only add to the list if it does not exist. You don't need a 2x2 array to then add those values
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900