Unique Birthday Gift - INFOSYS - SAMPLE QUESTION - 2

 Unique Birthday Gift - INFOSYS - SAMPLE QUESTION - 2 (SOLUTION )



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



int main() {
int i ;
int c = 0;

int n , k ;

cin>> n ; cin >> k ;

vector<pair<int,int>>v ;
int j ;
if ( n == 1 || k == 1 )
{
int temp ;
temp = n>k?n:k;
cout << temp<< endl ;
}else {
for (i = 1; i <= k+1; i++) {
for (j = 1; j <= n; j++) {
pair<int, int> p = make_pair(i, j);
v.push_back(p);
}

}

for (auto it : v) {

if ( it.second % it.first == 0 )
{

c++ ;
}

}
}
if (c) {
cout << c << endl;
}
return 0 ;
}



Comments

  1. Dude once verify this one :
    "For some input ur approach is giving wrong result."


    #include
    using namespace std;

    int rec(int i , int n , int k){
    if ( i > n ) return 0;
    if ( k == 0) return 1;
    int x = rec(i, n , k-1);
    int s = 0;
    for(int j = 2 ; j*i <= n ; j++){
    s+= rec(j*i , n , k-1);
    }
    return x + s;
    }

    int main (){
    int n , k;
    cin>>n>>k;
    cout<<rec(1 , n , k)<<endl;
    return 0;
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Infosys - Monster RPG GAME Sample Question Solution

Hackerrank -- C++ - Variable Sized Array Solution