Monday, 12 March 2018

C function to check whether a given number is prime or not

int prime(unsigned long long p)
{
    unsigned long long int i;
     if (p <= 1)  return 0;
    if (p <= 3)  return 1;
    if (p%2 == 0 || p%3 == 0) return 0;long int l=sqrt(p);
    for ( i=5; i<=l;i=i+6)
        if (p%i == 0 || p%(i+2)==0)
           return 0;

    return 1;
}

Saturday, 3 March 2018

Space Less Hacker Rank Solution in C++

https://www.hackerrank.com/contests/ccollesium/challenges/space-less

solution:
#include <bits/stdc++.h>
using namespace std;
int main(void)
{

   int t;
   cin>>t;
   while(t--)
   {
        int num,c=0;
        string k;
        cin>>num;
     cin>>k;
        char st[1000000]={0};
        int n = 0;
        for (int i = 1; i <= num; i++)
            n += sprintf (&st[n], "%d", i); // sprintf returns the number of characters written
        string s(st);                       // converting a character array to string
        //the data typt for f must be size_t since we used find
        for(size_t f = 0 ;(f = s.find( k, f )) < n; f++) // we can use string::npos instead of n to loop until the end of the string".
            c++;
        cout<<c<<endl;  
   }
    return 0;
}

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;
}