Quick actions

cmd+k|ctrl+k

Navigation

Languages

two nums

Snippet info

Language

Cpp

Visibility

public

Author

shilpimanchanda5

Created

2022-01-12T13:26:33.244218Z

Updated

2022-01-12T14:28:57.979049Z

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

void twonum(const vector<int> & rvec, const int & tar)
{
    int temptar = tar; 
    int prevInd = 0;
    int turn = 1;
 
    for(int ind =0; ind < rvec.size(); ind++)
    {
        if(temptar-rvec[ind] == 0 && turn == 0)
        {
            cout<<"the two indices are "<<prevInd <<", "<<ind<<endl;
            return;
        }
        else
        {
            turn = 1;
            temptar = tar;
        }
        
        if(temptar-rvec[ind] > 0 && turn == 1)
        {
            temptar = temptar - rvec[ind];
            prevInd = ind;
            turn--;
        }
    }    
    cout<<"Target doesn't exist"<<endl;
}

int main() {
    int target = 0;
    
    cin>> target;
    
    vector<int> vec{1,6,7,3,6,8,2,9};
    
    twonum(vec, target);
    
    return 0;
}
INFO