Saturday, 28 February 2015

Solutions of Warmup Challenges in C language

Challenge: Solve me first

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


int solveMeFirst(int a, int b) {
    return a+b;
  
}
int main() {
  int num1,num2;
  scanf("%d %d",&num1,&num2);
  int sum; 
  sum = solveMeFirst(num1,num2);
  printf("%d",sum);
  return 0;
}

Challenge: Lonely Integer

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
int lonelyinteger(int a_size, int* a) {
    
   int i,j,flag=0;

    for(i=0;i<a_size;i++)
        {
           for(j=0;j<a_size;j++)
            {
            if(a[i]==a[j]&&i!=j)
               { flag=1;
                continue;
               }
            }
         if(flag==1)
         {  
             flag=0;
             continue;
         }
         else
             return a[i];
        }
return 0;

}
int main() {
    int res;
    
    int _a_size, _a_i;
    scanf("%d", &_a_size);
    int _a[_a_size];
    for(_a_i = 0; _a_i < _a_size; _a_i++) { 
        int _a_item;
        scanf("%d", &_a_item);
        
        _a[_a_i] = _a_item;
    }
    
    res = lonelyinteger(_a_size, _a);
    printf("%d", res);
    
    return 0;
}

Challenge: Solve me second

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


int solveMeSecond(int a, int b) {
    
   
  return a+b;
}
int main() {
  int t,i;
  scanf("%d",&t);
  int num1,num2;
  int sum; 
  for ( i = 0;i < t; i++ ) {
    scanf("%d %d",&num1,&num2);
    sum = solveMeSecond(num1,num2);
    printf("%d\n",sum);
  }
  return 0;
}

Challenge: Flipping bits

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

int main() {

   int i,j,k,l;
unsigned long int n;
unsigned long int res;
scanf("%d",&k);
while((k--)>0)
{
int a[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
i=-1;
res=0;
scanf("%lu",&n);
while(n!=0)
{
if(n%2==0)
{
 a[++i]=0;
 n=n/2;
 
}
else
{
a[++i]=1;
n=n/2;
}

}
for(j=31;j>=0;j--)
{
if(a[j]==0)
{
a[j]=1;
}
else
a[j]=0;
}
for(j=0;j<=31;j++)
{
res+=(pow(2,j)*a[j]);
}
printf("%lu\n",res);

}

    return 0;
}

Challenge: Utopian Tree

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

int main() {

    int flag,n,i,a,j,h; // flag = 0 means double and flag=1 means add 1
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {   
             flag=0;
            h=1;
             scanf("%d",&a);
             for(j=0;j<a;j++)
                 {
                   if(flag==0)
                       {
                         h=2*h;
                         flag=1;
                       }
                   else
                       {
                        h=h+1;
                        flag=0;
                       }
                 }
            printf("%d\n",h);
        }
    
    return 0;
}

Challenge: Maximizing XOR

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
/*
 * Complete the function below.
 */
int maxXor(int l, int r) {

    int i,j,t,res=0;
    for(i=l;i<=r;i++)
        for(j=l;j<=r;j++)
            {
            t=i^j;
            if(t>res)
                res=t;
    }
    return res;

}
int main() {
    int res;
    int l;
    scanf("%d", &l);
    
    int r;
    scanf("%d", &r);
    
    res = maxXor(l, r);
    printf("%d", res);
    
    return 0;
}

Challenge: Alternating Characters 

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

int main() {

    int k,i,l,j,res=0;
    char arr[100010],r,p;
    scanf("%d",&k);
    for(i=1;i<=k+1;i++)
        {   
        int flag=0;
        if(i==1)
        flag=1;
            gets(arr);
            l=strlen(arr);
            for(j=0;j<l-1;j++)
                {
                    r=arr[j];
                    p=arr[j+1];
                    if(r==p)
                        res++;
                    
                }
                
            if(flag==1){
            }
            else{
            printf("%d\n",res);
            res=0;}
        }
    return 0;
}

Challenge: The Love-Letter Mystery

#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 */    
    int n,i,j,l,k,m,a,b,o,t;
    char s[10000];
    scanf("%d",&n);
    gets(s);
    for(i=0;i<n;i++)
        {
            o=0;
            gets(s);
            l=strlen(s);
            if(l%2!=0)
                {
                    m=(l+1)/2;
                    for(j=m-2,k=m;j>=0,k<l;j--,k++)
                        {
                            a=(int)s[j];
                            b=(int)s[k];
                            if(a>b)
                            {t=a-b; o+=t;}
                                
                            else
                            {t=b-a; o+=t;} 
                        }
                }
            else
                {
                    m=l/2;
                    for(j=m-1,k=m;j>=0,k<l;j--,k++)
                        {
                            a=(int)s[j];
                            b=(int)s[k];
                            if(a>b)
                            {t=a-b; o+=t;}
                                
                            else
                            {t=b-a; o+=t;} 
                        }
                    
                }
          printf("%d\n",o);
        }
    return 0;
}

Challenge: Max Min

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

#define MAX 100000
#define MAX_VAL 1000000001

int candies[MAX];
long int myfunction(long long int,int,int*);

/** The code to read from STDIN and output to STDOUT has been provided by us, for convenience. You may or may not use this. **/
 
int compare(const void *a,const void *b){
const int *da = (const int *)a;
const int *db = (const int *)b;
return (*da>*db) - (*da<*db);
}

int main() {
    
    int K;
    int i,j;
    long int temp;
    long long int N;
    
    scanf("%lld %d",&N,&K);
    for(i = 0;i < N;i++)
        scanf("%ld",candies + i);
    
qsort(candies,N, sizeof(int),compare);
    long int unfairness = myfunction(N,K,candies);
        
   printf("%ld",unfairness);
   
    return 0;
}
 long int myfunction(long long int n, int k, int *a)
{
 //first i have sorted the array in acending oreder then found the smallest difference at k intervals
    int i,j;
    long int res=9999999999,diff;
    
    for(i=0,j=k-1;i<n,j<n;i++,j++)
  
        {
            diff=a[j]-a[i];
              if(res>diff)
                  res=diff;
        }
    return res;
}

Challenge: Game of Thrones - 1


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
 
findPalind(char *arr)
{
     
    int flag = 0,count,flag2=0;
    char arr2[10000];
    int arr3[10000];
    int k=-1;
    int l;
    l= strlen(arr);
    for(int i=0;i<l;i++)
        { 
            count=0;
            flag=0;
            if(k==-1)
                {
                    k++;
                  arr2[k]=arr[i];
                 for(int j=0;j<l;j++)
                {
                    if(arr[i]==arr[j])
                        count++;
                }
                arr3[k]=count;
            }
            else
            {for(int m=0;m<=k;m++)
                {
                    if(arr[i]==arr2[m])
                        {flag=1;break;}
                }
              if(flag==1)
                  continue;
              else
                  {
                    k++;
                  arr2[k]=arr[i];
                 for(int j=0;j<l;j++)
                {
                    if(arr[i]==arr[j])
                        count++;
                }
                arr3[k]=count;
                  }
            }
        
        }
    for(int n=0;n<=k;n++)
        {
        if(arr3[n]%2!=0)
            flag2++;
    }
    
    if (flag2==0 || flag2==1)
        printf("YES\n");
    else
        printf("NO\n");
    
    return 0;
}
int main() {

    char arr[100001];
    scanf("%s",arr);
    findPalind(arr);
    return 0;
}



No comments:

Post a Comment