How to choose one element from each array., Which A(i)+B(i)+C(i)+D(i)+E(i)+F(i) must equal to 10.

__________________________________________________
i A B C D E F
__________________________________________________
1 3 2 3 3 2 4
__________________________________________________

2 6 9 4 24 5 7
__________________________________________________

3 11 28 5 81 10 12
__________________________________________________

4 18 65 6 192 17 19
__________________________________________________

5 27 126 7 375 26 28
__________________________________________________

6 38 217 8 648 37 39
__________________________________________________

Dani AI

Generated

The thread needs one clarification up front: there are two different problems that look similar but require very different solutions.

If the goal is "find an index i such that A[i]+B[i]+C[i]+D[i]+E[i]+F[i] == 10" (same i across all arrays) then the simplest approach is to verify all arrays have the same length, iterate a single index from 0 to length-1, compute the sum for that index, and record or print the index (use i+1 if you want 1‑based numbering). That is the interpretation and were steering toward. Check array bounds, and remember C++ arrays/vectors are zero-based.

If the goal is "pick one element from each array (any index in each) so the six chosen values sum to 10" then you need a combinatorial search. For small arrays you can brute force with nested loops; for variable sizes use recursion/backtracking with pruning. The example below shows a clean backtracking template that returns the combinations (it keeps indices so you can report which element from each array was chosen):

#include <vector>
#include <iostream>

void search(const std::vector<std::vector<int>>& lists,
            int idx, int sum, int target,
            std::vector<int>& choiceIndices,
            std::vector<std::vector<int>>& solutions)
{
    if (idx == (int)lists.size()) {
        if (sum == target) solutions.push_back(choiceIndices);
        return;
    }
    for (int j = 0; j < (int)lists[idx].size(); ++j) {
        int v = lists[idx][j];
        // prune only if values are nonnegative:
        if (sum + v > target) continue;
        choiceIndices.push_back(j); // store index chosen from this list
        search(lists, idx + 1, sum + v, target, choiceIndices, solutions);
        choiceIndices.pop_back();
    }
}

Performance notes: the worst case is exponential. If values are nonnegative, sort lists and prune aggressively. For many values or large targets consider dynamic programming (build achievable-sum sets with backpointers) or meet-in-the-middle techniques. If you only want one matching index (same-i case), the linear scan is O(n) and trivial.

Practical debugging tips: print array sizes before looping, test with a tiny hardcoded example, and decide whether you need indices or values in results. This will avoid off-by-one and scope mistakes that commonly trip up early implementations.

Recommended Answers

All 5 Replies

Sorry, I don't understand your question. Do you mean How do you select the same element from a series of arrays and add them together?

post what you have done so have or what steps you are thinking to solve the problem...
then only we will be able to help you...

like this, but I want to choose one elemet from each array, which the sum of A(i)+B(i)+C(i)
+D(i)+E(i)+F(i)=10. Which mean the value of i.

int total = 0;
for(int i = 0;i<10;i++)
{
total = a + b + c + d + e;
if(total==10)
{
printf("total is 10");
}
total=0;
}

you want the value of i??
so just save it to another variable inside if loop!!!

Try something like:

int total = 0;
for (int i=0;i<10;i++) {
total = a[i]+b[i]+c[i]+d[i]+e[i];
// print result
printf("total = "+total+"\n");
// print i to see which iteration of the loop
printf("i = "+i+"\n");
}
/* there is no need to reset total as it will be recalculated on the next iteration of the loop */

This should at least give you a clearer understanding of what is going on with your code, if not answer your question?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.