'why this code is giving this message during compilation "could not convert 'trip' from 'std::vector' to 'std::vector >"?

You are given an array/list ARR consisting of N integers. Your task is to find all the distinct triplets present in the array which adds up to a given number K.

An array is said to have a triplet {ARR[i], ARR[j], ARR[k]} with sum = 'K' if there exists three indices i, j and k such that i!=j, j!=k and i!=j and ARR[i] + ARR[j] + ARR[k] = 'K'.

vector<vector<int>> findTriplets(vector<int>arr, int n, int K) {
    vector<int>trip;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n;k++){
                if(arr[i]+arr[j]+arr[k]==K){
                    trip.push_back(arr[i]);
                    trip.push_back(arr[j]);
                    trip.push_back(arr[k]);
                }
            }
        }
    }
    return trip;
}


Solution 1:[1]

The problem is that the return type of your function findTriplets is a 2D vector while what you're actually returning(trip) is a 1D vector.

To solve this, you just have to match the type of what you're actually returning with the return type of the function as shown below:

#include <iostream>
#include <vector>
using namespace std;

//also note that we don't have to pass the size of the vector as second argument because we can use size() member function of vector
vector<vector<int>> findTriplets(vector<int>arr, int K) {
        
    vector<vector<int>> tempVec;
    vector<int> trip;
    int n = arr.size();//find n using arr.size()

    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n;k++){
                if(arr[i]+arr[j]+arr[k]==K){
                    trip.push_back(arr.at(i));
                    trip.push_back(arr.at(j));
                    trip.push_back(arr.at(k));
                        
                    //add trip to tempVec
                    tempVec.push_back(trip);
                        
                    //clear trip 
                    trip.clear();
                }
            }
        }
    }

    return tempVec;
}

int main()
{
    std::vector<int> vec = {12, 3, 4, 1, 6, 9,8,7};
    std::vector<std::vector<int>> result = findTriplets(vec, 24);
    std::cout << "triplets are:" << std::endl;
    for(const std::vector<int> &vec: result)
    {
        for(const int &element: vec)
        {
            std::cout << element << " ";
        }
        std::cout<<endl;
    }
    return 0;
}

The output of the above program is:

triplets are:
12 3 9 
12 4 8 
9 8 7 

Which can be verified here.

Solution 2:[2]

vector<vector<int>> findTriplets(vector<int>arr, int n, int K) {
    // Write your code here.
    vector< vector<int>> ans;
    vector<int> temp;
    for(int i =0; i<n-2; i++){
        for(int j=i+1; j<n-1; j++){
            for(int k= j+1; k<n; k++){
                if(arr[i]+arr[j]+arr[k]==K){
                    temp.push_back(arr[i]);
                    temp.push_back(arr[j]);
                    temp.push_back(arr[k]);
                    ans.push_back(temp); 
                    return ans;
                }
            }
        }
    }
}

its helps you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remy Lebeau
Solution 2 Jain Arpit