Need an idea about how to solve this algorithm puzzle
So, it is an C++ assignment question and I have been trying since a long time but couldn't get the idea right. This is the question:
Given two arrays of integers which have the same length, A [0..n-1]
and B [0..n-1]. It is necessary to find the first pair of indices i0
and j0, i0 <= j0, such that A [i0] + B [j0] = max A [i] + B [j], where
0 <= i < n, 0 <= j < n, i <= j.
int maxSum(int arrx, int arry, int x){
int i=0, j=0;
int a;
while(i <= j && j < x){
a = arrx[i] + arry[j];
if(a > arrx[i]){
cout << i << " " << j << " ";
i = x;
}else{
j++;
}
}
return 0;
}
Sample of what should be the I/O:
Input:
4
4 -8 6 0
-10 3 1 1
Output:
0 1
c++
add a comment |
So, it is an C++ assignment question and I have been trying since a long time but couldn't get the idea right. This is the question:
Given two arrays of integers which have the same length, A [0..n-1]
and B [0..n-1]. It is necessary to find the first pair of indices i0
and j0, i0 <= j0, such that A [i0] + B [j0] = max A [i] + B [j], where
0 <= i < n, 0 <= j < n, i <= j.
int maxSum(int arrx, int arry, int x){
int i=0, j=0;
int a;
while(i <= j && j < x){
a = arrx[i] + arry[j];
if(a > arrx[i]){
cout << i << " " << j << " ";
i = x;
}else{
j++;
}
}
return 0;
}
Sample of what should be the I/O:
Input:
4
4 -8 6 0
-10 3 1 1
Output:
0 1
c++
And what exactly is your question?
– DYZ
Jan 19 at 21:18
@DYZ It is what is the way to solve such thing?
– Islam A.F.
Jan 19 at 21:20
add a comment |
So, it is an C++ assignment question and I have been trying since a long time but couldn't get the idea right. This is the question:
Given two arrays of integers which have the same length, A [0..n-1]
and B [0..n-1]. It is necessary to find the first pair of indices i0
and j0, i0 <= j0, such that A [i0] + B [j0] = max A [i] + B [j], where
0 <= i < n, 0 <= j < n, i <= j.
int maxSum(int arrx, int arry, int x){
int i=0, j=0;
int a;
while(i <= j && j < x){
a = arrx[i] + arry[j];
if(a > arrx[i]){
cout << i << " " << j << " ";
i = x;
}else{
j++;
}
}
return 0;
}
Sample of what should be the I/O:
Input:
4
4 -8 6 0
-10 3 1 1
Output:
0 1
c++
So, it is an C++ assignment question and I have been trying since a long time but couldn't get the idea right. This is the question:
Given two arrays of integers which have the same length, A [0..n-1]
and B [0..n-1]. It is necessary to find the first pair of indices i0
and j0, i0 <= j0, such that A [i0] + B [j0] = max A [i] + B [j], where
0 <= i < n, 0 <= j < n, i <= j.
int maxSum(int arrx, int arry, int x){
int i=0, j=0;
int a;
while(i <= j && j < x){
a = arrx[i] + arry[j];
if(a > arrx[i]){
cout << i << " " << j << " ";
i = x;
}else{
j++;
}
}
return 0;
}
Sample of what should be the I/O:
Input:
4
4 -8 6 0
-10 3 1 1
Output:
0 1
c++
c++
edited Jan 19 at 21:19
melpomene
60.3k54693
60.3k54693
asked Jan 19 at 21:12
Islam A.F.Islam A.F.
64
64
And what exactly is your question?
– DYZ
Jan 19 at 21:18
@DYZ It is what is the way to solve such thing?
– Islam A.F.
Jan 19 at 21:20
add a comment |
And what exactly is your question?
– DYZ
Jan 19 at 21:18
@DYZ It is what is the way to solve such thing?
– Islam A.F.
Jan 19 at 21:20
And what exactly is your question?
– DYZ
Jan 19 at 21:18
And what exactly is your question?
– DYZ
Jan 19 at 21:18
@DYZ It is what is the way to solve such thing?
– Islam A.F.
Jan 19 at 21:20
@DYZ It is what is the way to solve such thing?
– Islam A.F.
Jan 19 at 21:20
add a comment |
1 Answer
1
active
oldest
votes
if I understand your question then, this should work. The answer is 7 that is given from your input data:
int maxSum(int arrx, int arry, int n) // n is the size (count) of the array
{
int i;
int j;
int a;
int maxVal;
int saveI;
int saveJ;
// first, set maxVal to minimum to make sure you get max val (0 is not necessarily min value)
maxVal = 0;
for (i = 0; i <= (n - 1); i++)
{
if (arrx[i] < maxVal)
maxVal = arrx[i];
if (arrx[j] < maxVal)
maxVal = arrx[j];
}
// now, crawl through the arrays
for (j = 0; j <= (n - 1); j++)
{
for (i = 0; i <= j; i++)
{
a = arrx[i] + arry[j];
if (a > maxVal)
{
maxVal = a;
saveI = i;
saveJ = j;
cout << i << " " << j << " " << a " ";
}
}
}
return maxVal;
}
Thanks a lot for the effort, appreciated. Worked but had to use my own max value of an array function. But may I know what is the purpose of havingsaveI
andsaveJ
?
– Islam A.F.
Jan 19 at 22:03
Just incase you wanted to pass them out or access them after the loops have ended.
– IAmNerd2000
Jan 19 at 22:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54271428%2fneed-an-idea-about-how-to-solve-this-algorithm-puzzle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
if I understand your question then, this should work. The answer is 7 that is given from your input data:
int maxSum(int arrx, int arry, int n) // n is the size (count) of the array
{
int i;
int j;
int a;
int maxVal;
int saveI;
int saveJ;
// first, set maxVal to minimum to make sure you get max val (0 is not necessarily min value)
maxVal = 0;
for (i = 0; i <= (n - 1); i++)
{
if (arrx[i] < maxVal)
maxVal = arrx[i];
if (arrx[j] < maxVal)
maxVal = arrx[j];
}
// now, crawl through the arrays
for (j = 0; j <= (n - 1); j++)
{
for (i = 0; i <= j; i++)
{
a = arrx[i] + arry[j];
if (a > maxVal)
{
maxVal = a;
saveI = i;
saveJ = j;
cout << i << " " << j << " " << a " ";
}
}
}
return maxVal;
}
Thanks a lot for the effort, appreciated. Worked but had to use my own max value of an array function. But may I know what is the purpose of havingsaveI
andsaveJ
?
– Islam A.F.
Jan 19 at 22:03
Just incase you wanted to pass them out or access them after the loops have ended.
– IAmNerd2000
Jan 19 at 22:13
add a comment |
if I understand your question then, this should work. The answer is 7 that is given from your input data:
int maxSum(int arrx, int arry, int n) // n is the size (count) of the array
{
int i;
int j;
int a;
int maxVal;
int saveI;
int saveJ;
// first, set maxVal to minimum to make sure you get max val (0 is not necessarily min value)
maxVal = 0;
for (i = 0; i <= (n - 1); i++)
{
if (arrx[i] < maxVal)
maxVal = arrx[i];
if (arrx[j] < maxVal)
maxVal = arrx[j];
}
// now, crawl through the arrays
for (j = 0; j <= (n - 1); j++)
{
for (i = 0; i <= j; i++)
{
a = arrx[i] + arry[j];
if (a > maxVal)
{
maxVal = a;
saveI = i;
saveJ = j;
cout << i << " " << j << " " << a " ";
}
}
}
return maxVal;
}
Thanks a lot for the effort, appreciated. Worked but had to use my own max value of an array function. But may I know what is the purpose of havingsaveI
andsaveJ
?
– Islam A.F.
Jan 19 at 22:03
Just incase you wanted to pass them out or access them after the loops have ended.
– IAmNerd2000
Jan 19 at 22:13
add a comment |
if I understand your question then, this should work. The answer is 7 that is given from your input data:
int maxSum(int arrx, int arry, int n) // n is the size (count) of the array
{
int i;
int j;
int a;
int maxVal;
int saveI;
int saveJ;
// first, set maxVal to minimum to make sure you get max val (0 is not necessarily min value)
maxVal = 0;
for (i = 0; i <= (n - 1); i++)
{
if (arrx[i] < maxVal)
maxVal = arrx[i];
if (arrx[j] < maxVal)
maxVal = arrx[j];
}
// now, crawl through the arrays
for (j = 0; j <= (n - 1); j++)
{
for (i = 0; i <= j; i++)
{
a = arrx[i] + arry[j];
if (a > maxVal)
{
maxVal = a;
saveI = i;
saveJ = j;
cout << i << " " << j << " " << a " ";
}
}
}
return maxVal;
}
if I understand your question then, this should work. The answer is 7 that is given from your input data:
int maxSum(int arrx, int arry, int n) // n is the size (count) of the array
{
int i;
int j;
int a;
int maxVal;
int saveI;
int saveJ;
// first, set maxVal to minimum to make sure you get max val (0 is not necessarily min value)
maxVal = 0;
for (i = 0; i <= (n - 1); i++)
{
if (arrx[i] < maxVal)
maxVal = arrx[i];
if (arrx[j] < maxVal)
maxVal = arrx[j];
}
// now, crawl through the arrays
for (j = 0; j <= (n - 1); j++)
{
for (i = 0; i <= j; i++)
{
a = arrx[i] + arry[j];
if (a > maxVal)
{
maxVal = a;
saveI = i;
saveJ = j;
cout << i << " " << j << " " << a " ";
}
}
}
return maxVal;
}
answered Jan 19 at 21:42
IAmNerd2000IAmNerd2000
45419
45419
Thanks a lot for the effort, appreciated. Worked but had to use my own max value of an array function. But may I know what is the purpose of havingsaveI
andsaveJ
?
– Islam A.F.
Jan 19 at 22:03
Just incase you wanted to pass them out or access them after the loops have ended.
– IAmNerd2000
Jan 19 at 22:13
add a comment |
Thanks a lot for the effort, appreciated. Worked but had to use my own max value of an array function. But may I know what is the purpose of havingsaveI
andsaveJ
?
– Islam A.F.
Jan 19 at 22:03
Just incase you wanted to pass them out or access them after the loops have ended.
– IAmNerd2000
Jan 19 at 22:13
Thanks a lot for the effort, appreciated. Worked but had to use my own max value of an array function. But may I know what is the purpose of having
saveI
and saveJ
?– Islam A.F.
Jan 19 at 22:03
Thanks a lot for the effort, appreciated. Worked but had to use my own max value of an array function. But may I know what is the purpose of having
saveI
and saveJ
?– Islam A.F.
Jan 19 at 22:03
Just incase you wanted to pass them out or access them after the loops have ended.
– IAmNerd2000
Jan 19 at 22:13
Just incase you wanted to pass them out or access them after the loops have ended.
– IAmNerd2000
Jan 19 at 22:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54271428%2fneed-an-idea-about-how-to-solve-this-algorithm-puzzle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
And what exactly is your question?
– DYZ
Jan 19 at 21:18
@DYZ It is what is the way to solve such thing?
– Islam A.F.
Jan 19 at 21:20