Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
maximizing the sum of product of two array
You are given two integer arrays A and B each of size N .
Let us define interaction of arrays A and B to be the sum
of A[i] * B[i] for each i from 1 to N .
You want to maximize the value of interaction of the
arrays. You are allowed to make at most K (possibly zero)
operations of following kind.
In a single operation, you can increase or decrease any
of the elements of array A or array B by 1.
Find out the maximum value of interaction of the arrays
that you can get.
Input
First line contains two space separated integers N . k
Second line contains N space separated integers
denoting array A .
Third line contains N space separated integers
denoting array B .
Output
output a single integer denoting the answer of the
problem.
Constraints
1 ≤ N ≤ 10^5<br />
-10^5 ≤ |A[i]|, |B[i]| ≤ 10^5<br />
0 ≤ K ≤ 10^4

sample Input 0
2 2
1 1
-1 -1

sample Output 0
0

explanation 0
on usual multiplication we get 1*(-1)+1*(-1)=-2
after maximizing array B b[0]=0 b[1]=0
then the sum is 1*0+1*0= 0
sample output 1
2 2
-1 2
0 2

sample Output 8
explationation 1
a[]={-1,2}<br />
b[]={0,2}

on maximizing for k=2
b[1]=4
then -1*0+2*4=8

What I have tried:

just the bruteforce technique but is giving me wrong answer
Posted
Updated 15-Oct-16 10:56am
v2
Comments
Patrice T 14-Oct-16 0:52am    
Show code
Garth J Lancaster 14-Oct-16 0:56am    
have you heard of duck typing ? ie, if it sounds like, and if it smells like homework, then it probably IS homework

.. we dont do homework - if you updated your question with your code we might be able to spot anything erroneous/obvious, and, we'd likely also say 'step through your program with the debugger line by line looking at the variable contents to see their values change and see if the values make sense according to a paper walk through'

sorry - I know it sounds harsh from where you're sitting
[no name] 14-Oct-16 6:47am    
If you do not understand your homework assignment, ask your teacher.

1 solution

Quote:
just the bruteforce technique but is giving me wrong answer
Just can guess that you have a mistake somewhere. but without code ...

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.
 
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