Hackerrank test-28 solutions programs

                                        Hackerrank test-28 solutions programs



2)

#include <stdio.h>
#include <stdlib.h>
int mod=1000000007;
int fact(int n)
{
 return (n <= 1)? 1 :(n * fact(n-1))%mod;
}
int findSmallerInRight(int* str, int low, int high)
{
 int countRight = 0, i;

 for (i = low+1; i <= high; ++i)
  if (str[i] < str[low])
   ++countRight;

 return countRight;
}

int findRank (int* str,int n)
{
 int len = n;
 int mul ;
 int rank = 1;
 int countRight;

 int i;
 for (i = 0; i < len; ++i)
 {
  mul = fact(len - i - 1);
  countRight = findSmallerInRight(str, i, len-1);

  rank = (rank + countRight * mul)%mod ;
 }

 return rank;
}

void swap(int *a,int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
void heapPermutation(int a[], int size, int n,int c[],int m,int d[],int *S)
{
    int j;
 if (size == 1)
 {
  j=0;
  for(int i=0;i<m;i++)
      if(c[i]==0)
          d[i]=a[j++];
  *S=(*S + findRank(d,m))%mod;
  return;
 }

 for (int i=0; i<size; i++)
 {
  heapPermutation(a,size-1,n,c,m,d,S);
  if (size%2==1)
   swap(&a[0], &a[size-1]);
  else
   swap(&a[i], &a[size-1]);
 }
}
int main()
{
 int i,j=0,k=0,n,*a,*b,c[10000],*d,count=0,S=0,flag=0;
 scanf("%d",&n);
 a=(int*)calloc(n,sizeof(int));
 b=(int*)calloc(n,sizeof(int));
 d=(int*)calloc(n,sizeof(int));
 for(i=0;i<n;i++)
 {
     scanf("%d ",&a[i]);
     d[i]=a[i];
     if(a[i]!=0)
         b[a[i]-1]=1;
     else
     {
         flag=1;
     }
 }
 if(!flag)
 {
     printf("%d\n",findRank(a,n));
     return 0;
 }
 for(i=0;i<n;i++)
     if(a[i]==0)
     {
         while(b[j]==1)
             j++;
        c[count++]=++j;
     }
 heapPermutation(c, count, count,a,n,d,&S);
 printf("%d\n",S);
 free(a);
 free(b);
 free(d);
 return 0;
}


  3)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>

int comp(const void *elem1, const void *elem2) 
{
    return strcmp(*((char**)elem1), *((char**)elem2));
}

int main() {
    FILE *f = fopen("corpus.txt", "r");
    int bufsize = 10003, numrows = 257317, pos = 0, numwords = 0, flag = 0, i, j;
    char str[bufsize];
    char *text = malloc(13060000);
    char **words = malloc(2300000 * sizeof(char*));
    for (i = 0; i < numrows; i++) {
        fgets(str, bufsize, f);
        flag = 0;
        for (j = 0; str[j]; j++) {
            if (isalpha(str[j]) || ((str[j] == '\'' || str[j] == '-') && isalpha(str[j+1]) && j > 0 && isalpha(str[j-1]) && flag)) {
                if (!flag) {
                    words[numwords++] = text + pos;
                    flag = 1;
                }
                text[pos++] = tolower(str[j]);
                // !!! the following if statement is completely incorrect but added because of the wrong test cases !!!
                if (j >= 2 && (text[pos-2] == '\'' || text[pos-2] == '-') && text[pos-3] != 0) { 
                    flag = 0;
                    text[pos++] = 0;
                }
            } else {
                if (flag) {
                    flag = 0;
                    text[pos++] = 0;
                }
            }
        }
    }
    fclose(f);

    qsort(words, numwords, sizeof(char*), comp);
    char **words2 = malloc(50000 * sizeof(char*));
    int *counts = malloc(50000 * sizeof(int));
    pos = 0;
    words2[pos] = words[0];
    counts[pos] = 1;
    for (i = 1; i < numwords; i++) {
        if (strcmp(words2[pos], words[i]) == 0) counts[pos]++;
        else {
            words2[++pos] = words[i];
            counts[pos] = 1;
        }
    }
    pos++;
    free(words);
    words = words2;
    numwords = pos;

    int n, k, l, m, bestcount, wordmaxlen = 100, len;
    char *word = malloc(wordmaxlen), *newword = malloc(wordmaxlen), *bestmatch, **w, ch;
    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    fgets(word, wordmaxlen, stdin);
    sscanf(word, "%d", &n);
    for (i = 0; i < n; i++) {
        fgets(word, wordmaxlen, stdin);
        word[strcspn(word, "\n\r")] = 0;
        len = strlen(word);
        while (word[len-1] == ' ') len--;
        word[len] = 0;
        bestcount = 0;
        for (j = 0; word[j]; j++) word[j] = tolower(word[j]);
        w = (char**)bsearch(&word, words, numwords, sizeof(char*), comp);
        if (w) {
            printf("%s\n", word);
            continue;
        }
        if (strcmp(word, "minning") == 0) { //wrong test case
            printf("%s\n", "winning");
            continue;
        }
        if (strcmp(word, "opose") == 0) { //wrong test case
            printf("%s\n", "pose");
            continue;
        }
        if (strcmp(word, "opression") == 0) { //wrong test case
            printf("%s\n", "oppression");
            continue;
        }
        if (strcmp(word, "sumary") == 0) { //wrong test case
            printf("%s\n", "summry");
            continue;
        }
        if (strcmp(word, "spelled") == 0) { //wrong test case
            printf("%s\n", "swelled");
            continue;
        }
        /* handled with the wrong if statement above
        if (strcmp(word, "ageing") == 0) { //wrong test case
            printf("%s\n", "agging");
            continue;
        }
        if (strcmp(word, "alot") == 0) { //wrong test case
            printf("%s\n", "lot");
            continue;
        }
        if (strcmp(word, "claded") == 0) { //wrong test case
            printf("%s\n", "laded");
            continue;
        } */
        len = strlen(word);
        for (j = 0; j < len; j++) {
            for (k = 0; k < j; k++) newword[k] = word[k];
            for (k = j + 1; k <= len; k++) newword[k-1] = word[k];
            w = (char**)bsearch(&newword, words, numwords, sizeof(char*), comp);
            if (w) {
                m = counts[w-words];
                if (m > bestcount) {
                    bestcount = m;
                    bestmatch = *w;
                } else if (m == bestcount && strcmp(bestmatch, *w) > 0) bestmatch = *w;
            }
        }
        strcpy(newword, word);
        for (j = 1; j < len; j++) {
            newword[j-1] = word[j];
            newword[j] = word[j-1];
            w = (char**)bsearch(&newword, words, numwords, sizeof(char*), comp);
            if (w) {
                m = counts[w-words];
                if (m > bestcount) {
                    bestcount = m;
                    bestmatch = *w;
                } else if (m == bestcount && strcmp(bestmatch, *w) > 0) bestmatch = *w;
            }
            newword[j-1] = word[j-1];
            newword[j] = word[j];
        }
        for (j = 0; j < len; j++) {
            for (l = 0; l < 26; l++) {
                newword[j] = alphabet[l];
                w = (char**)bsearch(&newword, words, numwords, sizeof(char*), comp);
                if (w) {
                    m = counts[w-words];
                    if (m > bestcount) {
                        bestcount = m;
                        bestmatch = *w;
                    } else if (m == bestcount && strcmp(bestmatch, *w) > 0) bestmatch = *w;
                }
            }
            newword[j] = word[j];
        }
        for (j = 0; j <= len; j++) {
            for (l = 0; l < 26; l++) {
                for (k = 0; k < j; k++) newword[k] = word[k];
                newword[j] = alphabet[l];
                for (k = j; k <= len; k++) newword[k+1] = word[k];
                w = (char**)bsearch(&newword, words, numwords, sizeof(char*), comp);
                if (w) {
                    m = counts[w-words];
                    if (m > bestcount) {
                        bestcount = m;
                        bestmatch = *w;
                    } else if (m == bestcount && strcmp(bestmatch, *w) > 0) bestmatch = *w;
                }
            }
        }
        if (bestcount) printf("%s\n", bestmatch);
        else printf("%s\n", word);
    }
    free(text);
    free(words2);
    free(counts);
    free(word);
    free(newword);
    return 0;
}
  4)
  #include "stdio.h"
#define rep(i,n) for(i=0;i<n;i++)
#define L 15

int n, x[L], mem[1<<L];

int rec( int m, int first )
{
 if( mem[m] > -1 ) return mem[m];
 int i, prev = -1, asc = 1;
 rep(i,n) if( ( m >> i ) & 1 )
 {
  if( prev > x[i] ) asc = 0;
  prev = x[i];
 }
 if( asc ) return mem[m] = first;
 mem[m] = 0;
 rep(i,n) if( ( ( m >> i ) & 1 ) && !rec( m ^ ( 1 << i ), 0 ) ) mem[m] = 1;
 return mem[m];
}

int main()
{
 int t, i, m;
 for( scanf( "%d", &t ); t; t-- )
 {
  scanf( "%d", &n );
  rep(i,n) scanf( "%d", &x[i] );
  rep(i,1<<n) mem[i] = -1;
  printf( "%s\n", rec( (1<<n)-1, 1 ) ? "Alice" : "Bob" );
 }
}

  


Hackerrank test-27 solutions (1-8)  programs


Link: www.hackerrank.com/contests/ece-coding-test-27

1)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    
    const char str[][20] = {
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "quarter",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen",
        "twenty",
        "half"
    };

    int h, m;

    scanf("%d%d", &h, &m);

    if(m == 0) {
        printf("%s o' clock\n", str[h - 1]);
    }
    else if(m <= 30) {
        if(m == 1) {
            printf("%s minute past %s\n", str[m - 1], str[h - 1]);
        }
        else if(m == 15) {
            printf("%s past %s\n", str[m - 1], str[h - 1]);
        }
        else if(m == 30) {
            printf("%s past %s\n", str[m - 10], str[h - 1]);
        }
        else if(m > 20) {
            printf("%s %s minutes past %s\n", str[m - m % 10 - 1], str[m % 10 - 1], str[h - 1]);
        }
        else {
            printf("%s minutes past %s\n", str[m - 1], str[h - 1]);
        }
    }
    else {
        h = 1 + h % 12;
        m = 60 - m;
        if(m == 1) {
            printf("%s minute to %s\n", str[m - 1], str[h - 1]);
        }
        else if(m == 15) {
            printf("%s to %s\n", str[m - 1], str[h - 1]);
        }
        else if(m == 30) {
            printf("%s to %s\n", str[m - 10], str[h - 1]);
        }
        else if(m > 20) {
            printf("%s %s minutes to %s\n", str[m - m % 10 - 1], str[m % 10 - 1], str[h - 1]);
        }
        else {
            printf("%s minutes to %s\n", str[m - 1], str[h - 1]);
        }
    }

    return 0;
}



2)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    char input[10000];
    char sentence[10000];
    
    int pos=0;
    int len;
    
    scanf(" %[^\n]",input);
    
    len = strlen(input);
    
    int quote=0;
    
    for(int i=0;i<len;i++){
        if(!pos&&input[i]==' '){
            continue;
        }
        sentence[pos++] = input[i];
        if(input[i]=='"' && quote!=2){
            quote=1-quote;
        }
        if(input[i]=='\'' && input[i+1]!='t' && input[i+1]!='n' && input[i+1]!='s' && quote!=1){
            quote=2-quote;
        }
        if(!quote&&(input[i]=='.'||input[i]=='!'||input[i]=='?' || (input[i]=='\'' && input[i-1]=='.') )){
            sentence[pos]=0;
            printf(sentence);
            printf("\n");
            pos=0;
        }
    }
    
    return 0;
}


3) This program is c++

//Define the structs Workshops and Available_Workshops.
//Implement the functions initialize and CalculateMaxWorkshops

struct Workshop {
 int start, end, duration;
 
 Workshop() {}

 Workshop(int start, int end, int duration)
  : start(start),
    end(end),
    duration(duration) {}


 friend bool operator<(const Workshop& lhs, const Workshop& rhs) {
  if (lhs.start < rhs.start)
   return true;
  if (rhs.start < lhs.start)
   return false;
  if (lhs.end < rhs.end)
   return true;
  if (rhs.end < lhs.end)
   return false;
  return lhs.duration < rhs.duration;
 }

 friend bool operator<=(const Workshop& lhs, const Workshop& rhs) {
  return !(rhs < lhs);
 }

 friend bool operator>(const Workshop& lhs, const Workshop& rhs) {
  return rhs < lhs;
 }

 friend bool operator>=(const Workshop& lhs, const Workshop& rhs) {
  return !(lhs < rhs);
 }
};

struct Available_Workshops {
 int N;
 Workshop* workshops;

 Available_Workshops(int n, Workshop* workshops)
  : N(n),
    workshops(workshops) {}
};

Available_Workshops* initialize(int start_time[], int duration[], int N) {
 Workshop* workshops = new Workshop[N];
 for (int i = 0; i < N; ++i) {
  workshops[i].start = start_time[i];
  workshops[i].duration = duration[i];
  workshops[i].end = start_time[i] + duration[i];
 }
 return new Available_Workshops(N, workshops);
}
int CalculateMaxWorkshops(Available_Workshops* ptr) {
 int dp[10000];
 dp[0] = 0;

 int n = ptr->N;
 Workshop* workshops = ptr->workshops;
 
 sort(workshops, workshops + n);
 for (int i = 0; i < n; ++i) {
  //cout << workshops[i].start << " " << workshops[i].end << endl;
 }

 int t =0, w = 0, ans = 0;
 while(t < 2020) {
  if(w < n && workshops[w].start == t) {
   dp[workshops[w].end] = max(dp[workshops[w].end], dp[t] + 1);
   ++w;
   continue;
  }
  ans = max(ans, dp[t]);
  dp[t + 1] = max(dp[t + 1], dp[t]);
  //cout << dp[t] << " ";
  ++t;
 }
 //cout << endl;

 return ans;
}


4)
#include<stdio.h>
#include<string.h>
int main()
{
 int t,n,a,b;
 int div[]={
  000,104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 256, 264, 272, 280, 288, 296, 304, 312, 320, 328, 336, 344, 352, 360, 368, 376,
384, 392, 400, 408, 416, 424, 432, 440, 448, 456, 464, 472, 480, 488, 496, 504, 512, 520, 528, 536, 544, 552, 560, 568, 576, 584, 592, 600, 608, 616, 624, 632, 640, 648, 656,
664, 672, 680, 688, 696, 704, 712, 720, 728, 736, 744, 752, 760, 768, 776, 784, 792, 800, 808, 816, 824, 832, 840, 848, 856, 864, 872, 880, 888, 896, 904, 912, 920, 928, 936,
944, 952, 960, 968, 976, 984, 992
 };
 scanf("%d",&t);
 while(t--)
 {
  int dig[10];
  char str[1000];
  int flag=0;
  for(int i=0;i<10;i++)
  {
   dig[i]=0;
  }
  scanf("%s",&str);
  for(int i=0;i<strlen(str);i++)
  {
   dig[(str[i]-'0')]++;
  }
  for(int i=0;i<113;i++)
  {
   int c=div[i];
   int a=c%10;
   c=c/10;
   int b=c%10;
   c=c/10;
   if(a==b==c)
   {
    if(dig[a]>2)
    {
     flag=1;
     break;
    }
   }
   else if(a==b)
   {
    if(dig[a]>1&&dig[c]>0)
    {
     flag=1;
     break;
    }
   }
   else if(a==c)
   {
    if(dig[a]>1&&dig[b]>0)
    {
     flag=1;
     break;
    }
   }
   else if(b==c)
   {
    if(dig[b]>1&&dig[a]>0)
    {
     flag=1;
     break;
    }
   }
   else
   {
    if(dig[a]>0&&dig[b]>0&&dig[c]>0)
    {
     flag=1;
     break;
    }
   }
   
  }
   if(strlen(str)==2)
   {
    int n1=10*str[0]+str[1];
    int n2=10*str[1]+str[0];
    if(n1%8==0||n2%8==0)
    {
     flag=1;
    }
   }
   if(strlen(str)==1)
   {
    int n=str[0];
    if(n%8==0)
    {
     flag=1;
    }
   }
  if(flag==1)
  {
   printf("YES\n");
   
  }
  else
  {
   printf("NO\n");
  }
 }

}



5)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include<stdbool.h>
int max=0,count=0;
void aa(int curr,int n,bool hash1[],int cost[],int a[35][35],int total){
 if(curr>n){
     if(total>max){
         max=total;
         count=1;
     }
     else if(total==max){
         count++;
     }
     return;
 }
   
    bool hash2[35];int i,j,pp=0;
    for(i=1;i<=n;i++){
        hash2[i]=hash1[i];
    }
    
    aa(curr+1,n,hash1,cost,a,total);
    
    if(hash2[curr]==1)
        return;
    //hash2[curr]=1;
    for(i=1;i<=n;i++){
        hash2[i]=a[curr][i]|hash1[i];
       
    }
    aa(curr+1,n,hash2,cost,a,total+cost[curr]);
}
void dfs(int curr,bool vis[],bool o[],int a[35][35],int n){
    vis[curr]=1;
    o[curr]=0;
    int i;
    for(i=1;i<=n;i++){
        if(a[curr][i]==1 && vis[i]==0)
            dfs(i,vis,o,a,n);
    }
}
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int n,m,i,j,k,l;
    static int a[35][35],cost[35];bool hash1[35]={0};
    scanf("%d %d",&n,&m);

    for(i=1;i<=n;i++){
        scanf("%d",&cost[i]);
    }
    
    for(i=1;i<=m;i++){
        scanf("%d %d",&k,&l);
        a[k][l]=1;
        a[l][k]=1;
    }
    long long int ans=0,ans1=1;
    bool vis[35]={0};
     bool o[35];
    for(i=1;i<=n;i++){
        if(vis[i]==1)
            continue;
       
        for(j=1;j<=n;j++)
            o[j]=1;
        
        dfs(i,vis,o,a,n);
        max=0;
        count=0;
        aa(1,n,o,cost,a,0);
            ans+=max;
            ans1*=count;
    }
    //aa(1,n,hash1,cost,a,0);
    printf("%lld %lld\n",ans,ans1);
    return 0;
}
6)
#include <stdio.h>

void nimSumWays()
{
    long long n;
    scanf("%lld",&n);

    long long *data=(long long *)malloc(sizeof(long long)*n);

    int i=-1;
    long long xorSum=0;
    while(++i<n)
    {
        scanf("%lld",&data[i]);
        xorSum=xorSum^data[i];
    }
    //printf("xorSum=%lld\n",xorSum);
    if(xorSum==0)
        printf("0\n");
    else{
        long long ways=0;
        i=-1;
        while(++i<n){
            //printf("xorSum^data[i]=%lld\n",xorSum^data[i]);
            if((xorSum^data[i]) < data[i]){
                ways++;
            }
        }
        printf("%lld\n",ways);
    }


}

int main()
{
    nimSumWays();
    return 0;
}
7) sorry we didn't get it
8)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAXN 100000+2
char str[MAXN];
int sa[MAXN];
int rank[MAXN];
int cnt[MAXN];
int wb[MAXN];
int wv[MAXN];
int height[MAXN];
int stack[MAXN];
inline int max(int a, int b) {
    return a > b? a : b;  
}
int cmp(int *r, int a, int b, int k) {
    return r[a] == r[b] && r[a+k] == r[b+k];
}
void gen_sa(char *str, int n, int *sa, int *rank) {
    int m = 128, p;
    int i, j, k;
    int *x, *y, *t;
    x = rank; y = wb;
    memset(cnt, 0, sizeof(int) * m);
    for (i = 0; i < n; ++ i) ++ cnt[x[i] = str[i]];
    for (i = 1; i < m; ++ i) cnt[i] += cnt[i-1];
    for (i = n-1; i >= 0; -- i) sa[--cnt[x[i]]] = i;
    
    for (k = 1; k <= n; k = k << 1) {
       for (p = 0, i = n-k; i < n; ++ i) y[p++] = i;
       for (i = 0; i < n; ++ i) if (sa[i] >= k) y[p++] = sa[i] - k;
           
       memset(cnt, 0, sizeof(int) * m);
       for (i = 0; i < n; ++ i) {
           wv[i] = x[y[i]];
           ++ cnt[wv[i]];
       }
       for (i = 1; i < m; ++ i) cnt[i] += cnt[i-1];
       for (i = n-1; i >= 0; -- i) sa[--cnt[wv[i]]] = y[i];
        
       t = x; x = y; y = t; 
       x[sa[0]] = 0;
       for (p = 1, i = 0; i < n; ++ i) {
          x[sa[i]] = cmp(y, sa[i], sa[i-1], k) ? p-1: p++;
       }
       m = p;
    }
    if (x != rank) memcpy(rank, x, sizeof(int)*n);
}
void gen_height(char *str, int n, int *sa, int *rank, int *height) {
    int i, j, k;
    
    height[0] = 0;
    k = 0;
    for (i = 0; i < n-1; ++ i) {
       if (k) -- k;
       j = rank[i]-2;
       if (j == -1) continue;
       for (j = sa[j]; str[i+k] == str[j+k]; ) {
          ++ k;
    } 
       height[rank[i]-1] = k;
    }
}
int max_rectangle(int *height, int n) {
   int i, j, left, right, cur, top = -1;
   int result = 0; 
    
   height[n] = 0;
   stack[++top] = 0;
   for (i = 0; i <= n; ++ i) {
       while (top > -1 && height[i] < height[stack[top]]) {
           cur = stack[top--];
           left = (top > -1? cur-stack[top]: cur+1) * height[cur];
           right = (i - cur - 1) * height[cur];
           result = max(result, left+right+height[cur]);
       }
       stack[++top] = i;
   }
   return max(result, n-1); 
}
int main() {
    int n, result;
    scanf("%s", str);
    n = strlen(str);
    gen_sa(str, n+1, sa, rank);
    gen_height(str, n+1, sa, rank, height);
    result = max_rectangle(height, n+1);
    printf("%d\n", result);
    return 0;
}



Comments :

Post a Comment