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

Integers in an array are unique and increasingly sorted. Please write a function/method to find an integer from the array which equals to its index. For example, in the array {-3, -1, 1, 3, 5}, the number 3 equals its index 3.

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

int main(){
    int i,*arr,len;
    scanf("%d",&len);
   
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
   
    for(i=0;i<len;i++)
        scanf("%d",&arr[i]);
   
    //Traversing the array & finding the element which is equal to its index
    for(i=0;i<len;i++){
        if(arr[i]==i){
            printf("The number %d is equal to its index %d\n",arr[i],i);
        }
    }
}

There are n registration numbers in an array, some of them are repeated, write an algorithm to find the repeated numbers in time complexity of O(nlogn) or O(n)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    int i,*arr,len,max=-1,*hash;
    scanf("%d",&len);
   
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
   
    for(i=0;i<len;i++){
        scanf("%d",&arr[i]);
        // To find the largest number in array
        if(max<arr[i])
            max=arr[i];
    }
   
    //Dynamically allocating memory for hash map
    hash = (int*) malloc(max * sizeof(int));
   
    //Initialize hash array with 0
    for(i=0;i<len;i++)
    hash[arr[i]]=0;
   
    //Traversing the array and using hash map, finding the duplicates in the array
    for(i=0;i<len;i++){
        if(hash[arr[i]]>=0)
        hash[arr[i]]++;
        if(hash[arr[i]]>1){
            printf("%d is duplicated\n",arr[i]);
            hash[arr[i]]=-1;
        }
    }