I have to write a program to read 10 integers into an array. It will then read in one more integer. My program should then compute and output how many distinct pairs of integers in the array add up to the last number that was input. Note I cannot use the same number twice in a sum, unless it has been input two or more times.

How do I go about doing this?

Dani AI

Generated

— the task is to count unordered index pairs (i < j) whose values sum to the target. Duplicates in the input matter: if the value 2 appears twice it can form a 2+2 pair, so do not remove repeats. ’s suggestion to reduce to unique values would drop valid pairs when numbers repeat.

A simple, robust method is to build a frequency map of values. For each distinct value x compute y = target - x. If x < y add freq[x] * freq[y]. If x == y add freq[x] * (freq[x] - 1) / 2. That counts each unordered (i, j) exactly once. For ten numbers the brute-force double loop over i < j is perfectly fine and easiest to understand; the frequency map is O(N) average and scales better for larger inputs.

Example (C++11+). This shows the frequency idea; it uses long long for the pair count to avoid small overflow when counts grow.

#include <iostream>
#include <vector>
#include <unordered_map>

int main() {
    std::vector<int> a(10);
    for (int i = 0; i < 10; ++i) std::cin >> a[i];
    int target; std::cin >> target;

    std::unordered_map<int, long long> freq;
    for (int v : a) ++freq[v];

    long long pairs = 0;
    for (const auto &kv : freq) {
        int x = kv.first;
        long long fx = kv.second;
        int y = target - x;
        auto it = freq.find(y);
        if (it == freq.end()) continue;
        long long fy = it->second;
        if (x < y) pairs += fx * fy;
        else if (x == y) pairs += fx * (fx - 1) / 2;
    }

    std::cout << pairs << '\n';
}

Notes: sort+two-pointer is an alternative (O(N log N)). Test edge cases: all-equal arrays, negatives, zero, and very large inputs (use 64-bit counters). Compile with C++11 or newer if using the example above.

Recommended Answers

All 3 Replies

I really don't know how to go about doing this, I know that I have to have the program promt you to input 10 numbers, and then I have to get the program to compare all the possible sums of two of the integers and then say if they equal 9.

So far I have:

#include <iostream.h>
#include <stdlib.h>

int main()
{
int num, count;
int List[10],i;

// input 10 integers into an array and compute the average
for (i=0;i<10;i++)
{
cout << "Please enter an integer ";
cin >> List ;


But I don't know where to go from here

Maybe posting this to the (C++) forum will help.
If you want to do it in C#, then I suggest that you load a second array with unique integers from the first array, then sum the second array.

Thank you, I didn't even notice that I posted this in C#, I must of acciently pressed it. I ment to put it under C++. Thank you.

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.