Saturday, 3 March 2018

Printing all occurrence (indexes) of a particular character in a string in c++

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

int main() {
string st="mississipi";
vector<char>s;
copy(st.begin(),st.end(),back_inserter(s)); // converting string to vector
s.push_back('\0');       //  converting string to vector
vector<char>::iterator it=s.begin();
int i=-1;
while(1){
    
it=find(s.begin()+i+1,s.end(),'s'); //once we find first 's' reduce the search range
if(it == s.end())
        break;
i=it-s.begin();  // storing index of the occurrence of 's' in string 
cout<<i;

}
return 0;



}

Wednesday, 28 February 2018

For Loop in STL C++

Different ways of writing for loop to loop through vectors in c++

vector<int> v;

for(int &t:v)
   cout<<t;

for(int i=0;i<v.size();i++)
     cout<<v[i];

vector<int>::iterator it;
for(it=v.begin();it != v.end();it++)
     cout<<*it;

Friday, 23 February 2018

Dictionaries and Maps Hacker Rank Solution in C/C++

https://www.hackerrank.com/challenges/linkedin-practice-dictionaries-and-maps/problem

C++ code that passes all test cases

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

int main()
 {
map<string,long>pb;
int n;
string name;
cin>>n;
for(int i=0;i<n;i++)
{
        long num;
    cin>>name>>num;
    pb[name]=num;
}
while(cin>>name)
{
    if(pb[name])
            cout<<name<<"="<<pb[name]<<endl;
        else
            cout<<"Not found"<<endl;
}
}

C Code that passes only 2 test cases and timed out for other test cases:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
             
               int data;
               char name[1000];
               struct node *next;
};

int main()
{
        int n;
        scanf("%d",&n);
        struct node *head[26]={NULL},*c;
        for(int i=0;i<n;i++)
        {
            struct node * newnode=(struct node *)malloc(sizeof(struct node));
            int val;
            char temp[1000];
            scanf("%s%d",temp,&val);
            newnode->data=val; strcpy(newnode->name,temp);
               newnode->next = NULL;
               if(head[temp[0]-'a'] == NULL)
                    head[temp[0]-'a'] = newnode;
               else
               {
                    for(c=head[temp[0]-'a'];c->next != NULL;c=c->next);
                    c->next=newnode;
               }
         
        }
         for(int i=0;i<n;i++)
        {
            char temp[1000];int flag=1;
            scanf("%s",temp);
            if(head[temp[0]-'a'] == NULL)
                 printf("Not found\n");
            else
               {
                              for(c=head[temp[0]-'a'];c!=NULL;c=c->next)
                              {
                                  if(strcmp(temp,c->name) == 0)
                                  {
                                      printf("%s=%d\n",c->name,c->data);
                                      flag=0;break;
                                  }
                              }
                              if(flag)
                                printf("Not found\n");
                           
               }
        }
        return 0;
}


  

Sunday, 18 February 2018

Anagram Hacker Rank Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int anagram(char* s){
    // Complete this function
    int l=strlen(s),c=0;
    if(l%2 != 0)
        return -1;
    int h1[26]={0},h2[26]={0},i;
    for(i=0;i<l/2;i++)
        h1[s[i]-'a']++;
    for(i=l/2;i<l;i++)
        h2[s[i]-'a']++;
    for(i=0;i<26;i++)
    {
        h1[i]=h1[i]-h2[i];
        if(h1[i]>0)
            c=c+abs(h1[i]);
      
    }
    return c;
}
int main() {
    int q;
    scanf("%i", &q);
    for(int a0 = 0; a0 < q; a0++){
        char* s = (char *)malloc(512000 * sizeof(char));
        scanf("%s", s);
        int result = anagram(s);
        printf("%d\n", result);
    }
    return 0;
}

Saturday, 17 February 2018

Smaller than the element code in c++

https://www.codechef.com/CODR2018/problems/KRYP6/


#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;
set <long long> st;
set <long long>::iterator ind;
int main() {
    long long n,i=0;
    scanf("%lld",&n);
    long long *a=(long long *)malloc(sizeof(long long)*n);
    for(i=0;i<n;i++)
        scanf("%lld",&a[i]);
    for(i=0;i<n;i++)
    {
        ind = st.lower_bound(a[i]);
   
if (ind == st.begin())
    printf("-1\n");
else {
    ind--;
    printf("%lld\n", *ind);
}
st.insert(a[i]);
    }
 
return 0;
}

Tuesday, 6 February 2018

Climbing the Leaderboard Hacker Rank Solution in C

https://www.hackerrank.com/challenges/climbing-the-leaderboard/copy-from/61917526

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main() {
    int n,i,j,t,scores_i,k;
    scanf("%i", &n);
 
    int *scores = malloc(sizeof(int) * n);
    scanf("%i",&scores[0]);
    for (scores_i = 1,k=1; k < n;k++ )
    {
       scanf("%i",&t);
       if(t !=scores[scores_i-1])
       {
           scores[scores_i]=t; //scoring only distinct scores so that index gives the rank
           scores_i++;
       }
     }
    n=scores_i;
    int m,rank;
    j=n-1;//will store last index or the last rank
    scanf("%i", &m);
    int *alice = malloc(sizeof(int) * m);
    for (int alice_i = 0; alice_i < m; alice_i++) {
       scanf("%i",&alice[alice_i]);
    }
    for(i=0;i<m;i++)
    {
        while(j>=0 && alice[i]>scores[j])
            j--;
        if(j==-1)
            rank=1;
        else if(alice[i] == scores[j])
            rank=j+1;
        else if(alice[i] < scores[j])
            rank=j+2;
        printf("%d\n",rank);
    }
    return 0;
}