Friday, April 1, 2016

Finding the positions of a number in a array using 2d vector

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int a[100];
    int n;//size of array
    cin>>n;
    int i;
    int d;
    vector<int> v[10000];//2d vector
    for(i=0; i<n; i++)
    {
        cin>>d;
        a[i]=d;
        v[d].push_back(i);
    }
    while(1)
    {

        int c;//number to find
        cin>>c;
        if(v[c].size()==0)
        {
            cout<<"khali"<<endl;//no such elements
        }
        for(i=0; i<v[c].size(); i++)
        {

            cout<<v[c][i]<<" ";//printing the positions of that number
        }

        cout<<endl;

    }
}

No comments:

Post a Comment