Wednesday 27 September 2017

To reverse the order of two words ..... if I enter "hello world" , I should get "world hello "

#include<stdio.h>
void reverse(char *begin, char *end);

void reverseWords(char *s)
{
  char *word_begin = s;
  char *temp = s;

  while( *temp )
  {
    temp++;
    if (*temp == '\0')
    {
      reverse(word_begin, temp-1);
    }
    else if(*temp == ' ')
    {
      reverse(word_begin, temp-1);
      word_begin = temp+1;
    }
  }
  reverse(s, temp-1);
}

void reverse(char *begin, char *end)
{
  char temp;
  while (begin < end)
  {
    temp = *begin;
    *begin++ = *end;
    *end-- = temp;
  }
}

int main()
{
  char *s;
  gets(s);
  char *temp = s;
  reverseWords(s);
  printf("%s", s);
  getchar();
  return 0;
}

Write a Program to Read two numbers and check whether they are Co-Primes or not Co-Prime: In number theory, two integers a and b are said to be relatively prime, mutually prime, or coprime (also spelled co-prime)[1] if the only positive integer that divides both of them is 1 Input: 2 3 Output: Co-Prime

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

int prime(int number){
    if(number==1)
    return 0;
    int dummy = (int) sqrt(number), i;
for (i = 2; i <= dummy; i++) {
if (number % i == 0) {
return 0;
}
}
return 1;
}
int main(){
    int num1,num2;
    scanf("%d %d",&num1,&num2);

    //Any even number have 2 as common divisor
    if(num1%2==0 && num2%2==0){
        printf("Not a Co-prime");
    }

    //1 is co-prime with every number
    else if(num1==1 || num2==1){
        printf("Co-prime Number");
    }

    //Any two successive numbers/ integers are always co-prime
    else if(abs(num1-num2)==1){
        printf("Co-prime Number");
    }

    //Every prime number is co-prime to each other
    else if(prime(num1)&&prime(num2)){
        printf("Co-prime Number");
    }

    else{
        printf("Not a Co-prime");
    }
}

Tuesday 26 September 2017

Given an array with both positive and negative numbers. Find two elements such that their sum is closest to zero

#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,*arr,len,total=99999,sum,num1,num2;
scanf("%d",&len);
//Dynamically allocating memory for array
arr = (int*) malloc(len * sizeof(int));
for(i=0;i<len;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<len;i++){
for(j=0;j<len;j++){
if(i==j)
break;
if(abs(arr[i]+arr[j])<abs(total)){
total = arr[i]+arr[j];
num1=arr[i];
num2=arr[j];
}
}
}
printf("sum = %d\nNumbers are %d %d",total,num1,num2);
}

Given an array with both positive and negative numbers. Find two elements such that their sum is closest to zero

#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,*arr,len,total=99999,sum,num1,num2;
scanf("%d",&len);
//Dynamically allocating memory for array
arr = (int*) malloc(len * sizeof(int));
for(i=0;i<len;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<len;i++){
for(j=0;j<len;j++){
if(i==j)
break;
if(abs(arr[i]+arr[j])<abs(total)){
total = arr[i]+arr[j];
num1=arr[i];
num2=arr[j];
}
}
}
printf("sum = %d\nNumbers are %d %d",total,num1,num2);
}

Given 2 arrays of integers a1,a2 check if a1 is a subset of a2

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

int main(){
    int i,j,len1,len2;
   
    scanf("%d",&len1);
    int arr1[len1];
    for(i=0;i<len1;i++){
        scanf("%d",&arr1[i]);
    }
   
    scanf("%d",&len2);
    int arr2[len2];
    for(i=0;i<len2;i++){
        scanf("%d",&arr2[i]);
    }
    int flag=0;
    for(i=0;i<len1;i++){
        flag=0;
        for(j=0;j<len2;j++){
            if(arr1[i]==arr2[j]){
                flag=1;
                break;
            }
        }
        if(flag==0){
            printf("arr1 is not a subset of arr2");
            break;
        }
    }
    if(flag==1)
        printf("arr1 is subset of arr2");
}

Monday 25 September 2017

Given an array of n numbers, give an algorithm to find the first element in the array which is repeated

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


int main(){
    int i,*arr,len,hash[10000]={0};
    scanf("%d",&len);
    
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
    
    for(i=0;i<len;i++){
        scanf("%d",&arr[i]);
        hash[arr[i]]++;
    }
    
    for(i=0;i<len;i++){
        if(hash[arr[i]]>1){
            printf("%d is the first the element in the array which is repeated",arr[i]);
            break;
        }
    }

}

Given an array filled with integers that appear exactly twice, with the exception of one integer that appears once, find the unique integer. ex:findUnique([1, 2, 6, 9, 9, 1, 3, 6, 2]) returns: 3 ex: findUnique([12, 45, 32, 65, 32, 65, 45]) returns: 12

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


int main(){
    int i,*arr,len,temp,j;
    scanf("%d",&len);
   
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
   
    for(i=0;i<len;i++){
        scanf("%d",&arr[i]);
    }
   
    //Sort the array
    for (i=0;i<len;i++)
    {
        for (j=0;j<len-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
     
    //Traversing the array, finding the unique integer as mentioned in the question the number must
    //appear exactly twice except one unique number
    for(i=0;i<len-1;i+=2){
        if(arr[i]!=arr[i+1]){
            printf("%d is the unique number\n",arr[i]);
            break;
        }
    }
}