Problem 49
Median :
Solution in C
float median(int size,int *inputList)
{
int start_index=0;
int end_index=size-1;
float res=-1;
if(size%2!=0)
{
int med_1=size/2;
int med_2=(size/2)+1;
res=((float)quick_select(inputList,start_index,end_index,med_1));
}
else
{
//enter your code here
res=((float)quick_select(inputList,start_index,end_index,med_1)+(float)quick_select(inputList,st
art_index,end_index,med_2))/2;
}
return res;
}
Problem 50
Matrix Sum :
Solution in C
int matrixSum(int rows,int columns, int **matrix)
{
int i,j,sum=0;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
sum+=matrix[i][j];
}
}
return sum;
}
Problem 51
Count Element :
Solution in C
int countElement(int size,int num,int *inputList)
{
int i,count=0;
for(i=0;i<size;i++)
{
if(inputList[i]>2*num)
{
count+=1;
}
}
return count;
}
Problem 52
Delete Non Repeat :
Solution in C
void deleteNonRepeat(int size,int *inputList)
{
int count=0,i,j;
int counter[size];
for(i=0;i<size;i++)
{
counter[i]=1;
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(inputList[i]==inputList[j])
{
counter[i]++;
counter[j]++;
}
}
}
for(i=0;i<size;i++)
if(counter[i]>1)
count++;
j=0;
for(i=0;i<size;i++)
if(counter[i]>1)
printf("%d",inputList[i]);
}
Problem 53
Multiply Number :
Solution in C
int multiplyNumber(int numA,int numB,int numC)
{
int result,min,max,mid;
max=(numA>numB)?((numA>numC)?numA:numc):((numB>numC)?numB:numC);
min=(numA<numB)?((numA<numC)?numA:numc):((numB<numC)?numB:numC);
mid=(numA+numB+numC)-(min+max);
result=(max* mid);
return result;
}
Problem 54
Pyramid of alphabets
a
bcd
efghi
jklmnop
Solution in C
#include<stdio.h>
void printPattern(int n)
{
int i,j;
char ch='a';
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("%c",ch++);
printf("\n");
}
}
Problem 55
You have to encrypt a non-empty string phrase. The encryption adds a ‘cyclic shift’ to each letter where the value of this ‘cyclic shift’ is decided by the position of the letter from the end of its word. The shift value for each letter of a word is its index value (starting from 0) from the right-most character of the word.
EXAMPLE:
The shift values in ‘yum feed’ will be
yum: m->0, u->1, y->2
feed: d->0, e->1, e->2, f->3
which gives the encryption avmigfd. Here, adding the shift with value 0 to letter ‘m’ gives ‘m’ + 0 = m;
values 1 to ‘u’ gives ‘u’ + 1 = v and values 2 to ‘y’ gives ‘y’ + 2 = a and so on.
Note that the shift wraps around on reaching the end of the alphabets, i.e., the shift values for ‘y’ as shown above is ‘a’.
INPUT:
The input to the function/method consists of a string.
OUTPUT:
Return the encrypted string
NOTE:
Assume that the input string contains single space separating set of words
Solution in C
#include<stdio.h>
char* encryption(char* str);
int main()
{
char str[]="zebra tiger";
printf("%s",encryption(str));
return 0;
}
char* encryption(char* str)
{
//your CODE
int len,index,value;
for(len=0;str[len]!=‟\0‟;len++)
{
if(str[index]=‟\0‟)
{
value=0;
continue;
}
if(str[index]+value<=122)
str[index]=str[index]+value++;
else
str[index]=str[index]+value++ -26;
}
return str;
}
Problem 56
The LeastRecentlyUsed(LRU) cache algorithm exists the element from the cache(when it’s full) that was leastrecentlyused. After an element is requested from the cache, it should be added to the cache(if not already there) and considered the most recently used element in the cache. Initially, the cache is empty. The input to the function LruCountMiss shall consist of an integer
max_cache_size, an array pages and its length len. The function should return an integer for the number of cache misses using the LRU cache algorithm. Assume that the array pages always has pages numbered from 1 to 50.
TEST CASE1:
Input: 3 16 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0
Expected Return Value: 11
TESTCASE 2:
Input:2 9 2 3 1 3 2 1 4 3 2
Expected Return Value: 8
Solution in C
#include <stdio.h>
int main()
{
int max_cache_size,pages_len,i,j,cache[100],pages[100],k,pageincache=0,misscount=0;
scanf("%d %d",&max_cache_size,&pages_len);
for(i=0;i< max_cache_size;i++)
cache[i]=-1;
for(i=0;i<pages_len;i++)
{
pageincache=0;
scanf("%d",&pages[i]);
for(j=0;j<max_cache_size;j++)
{
if(pages[i]==cache[j])
{
pageincache=1;
for(k=j;k<max_cache_size;k++)
{
cache[k]=cache[k+1];
}
cache[max_cache_size-1]=pages[i];
}
}
if(pageincache==0)
{
misscount++;
for(k=0;k<max_cache_size;k++)
{
cache[k]=cache[k+1];
}
cache[max_cache_size-1]=pages[i];
}
}
printf("%d",misscount);
}
Problem 57
Half sort Array:
Solution in C
#include<stdio.h>
#include<limits.h>
int main()
{
int arr[]={10,12,25,6,13,8,19};
int index,size,max,maxpos,min,minpos,temp,scope;
size=sizeof(arr)/sizeof(arr[0]);
for(index= 0 ; index < size; printf("%2d ",arr[index++]));
if(index%2==0)
{
min = INT_MAX;
for(index =1; index<size; index++)
{
if(arr[index] < min)
{
min = arr[index];
minpos=index;
}
}
temp = arr[index];
arr[index] = arr[minpos];
arr[minpos]=temp;
}
else
{
max = INT_MIN;
for(index = 0 ; index<size; index++)
{
if(arr[index] > max)
{
max = arr[index];
maxpos=index;
}
}
temp = arr[index];
arr[index] = arr[maxpos];
arr[maxpos]=temp;
}
for(printf("\n"),index= 0 ; index < size;printf("%2d ",arr[index++]));
return 0;
}
Problem 58
Given a string str, write a program to eliminate all the vowels from the given string. The list of vowels in the English alphabet is : {a,e,i,o,u,A,E,l,0.U}. The Input to the function eliminateVowelString shall consist of a string str (containing only English letters) and returns a pointer to a string which does not contain vowels.
EXAMPLE:
Input =”abcdefghijklmnopqrstuvwxyz”
0utput=”bcdfghjklmnpqrstvwxyz”
USEFUL COMMANDS:
strlen() is used to calculate the length of the string. The statement –
int len = strlen(str); Returns the length of the string str
TESTCASE 1:
Input: “bacdefghijklmnopgrstu”
Expected Return Value: “bcdfghjklmnpqrst”
TESTCASE 2:
Input: “bacdcfgh”
Expected Return Value: “bcdlgh”
Solution in C
char * removeVowel(char *str)
{
int trav,,hold=0;
for(trav=0;str[trav]!='\0';trav++)
{
if(str[trav]=='a'|| str[trav]=='e'|| str[trav]=='i'|| str[trav]=='o'|| str[trav]=='u'||
str[trav]=='A'|| str[trav]=='E'|| str[trav]=='I'|| str[trav]=='O'|| str[trav]=='U')
{
}
else
{
str[hold]=str[trav];
hold++;
}
}
str[hold]='\0';
printf("%s",str);
return 0;
}
Problem 59
The function sameelementcount(int *arr,intlen)accepts an integer array arr of length len as a input and returns the number of elements in an arr which are even numbers and equal to the element to its right.
Solution in C
//WRITE DOWN YOUR CODE HERE
int sameelementcount(int *arr, int len)
{
int i,count=0;
for(i=0;i<len-1;i++)
{
if((arr[i]%2==0)&&(arr[i]==arr[i++]))
count++;
}
return count;
}
Problem 60
Print the following Pattern
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Solution in C
//WRITE DOWN YOUR CODE HERE
void printPattern(int n)
{
int i,j;
for(i=1;i<=n;i++,printf(“\n”))
{
for(j=1;j<=i;j++)
{
printf("%d”,j);
}
for(j--;j>=1;j--)
{
printf("%d”,j);
}
}
}
Or,
void printPattern(int n)
{
int i,j,num=1;
for(i=1;i<=n;i++)
{
num=num*10+1;
printf(“%d\n”, num*num);
}
}
Problem 61
Return the difference between two given times in seconds.
TESTCASE
TestCase1:
Input:
Time:1:58:42, Time:2:1:45
Expected Return values:
183
Testcase 2
Input:
Time:3:49:57, Time:2:45:57
Expected Return Values
3600
Solution in C
#include<stdio.h>
struct Time
{
int h;
int m;
int s;
};
typedef struct Time TIME;
toSeconds(TIME * gt)
{
int in_seconds;
in_seconds = gt->h * 3600 + gt->m * 60 + gt->s;
return in_seconds;
}
int abs(int val)
{
if (val< 0)
return -val;
else
return val;
}
diff_in_times(TIME *t1, TIME *t2)
{
//WRITE DOWN YOUR CODE HERE
int t5,t6,res,result;
t5= toSeconds(t1);
t6= toSeconds(t2);
res= t5-t6;
result=abs(res);
return result;
}
int main()
{
TIME t1 = {1,58,42}, t2 = {2,59,45};
printf("%d", diff_in_times(&t1, &t2));
return 0;
}
Problem 62
The function findMaxElement(int *arr1,int len1,int *arr2,int len2) accepts two integer arrays arr1, arr2 of length len1, len2 respectively. It is supposed to return the largest element in both the input arrays. Another function sortArray(int *arr,intlen) sorts the input array arr of length len in ascending order and returns the sorted array. Your task is to use sortArray(int *arr, intlen) function and complete the code in findMaxElement(int *arr1,int len1,int *arr2,int len2) so that it passes all test cases.
TESTCASE 1:
Input:
[2, 5, 1, 3, 9, 8, 4, 6, 5, 2, 3, 11], 12,
[11, 13, 2, 4, 15, 17, 67, 44, 2, 100, 0, 23]11
Expected Return Value:
100
TESTCASE 2:
Input:
[100, 22, 43, 912, 56, 89, 85], 7, [234, 123, 456, 234, 890, 101], 6
Expected Return Value:
912
Solution in C
int *sortArray(int *arr,int *len)
{
int i=0,j=0,temp=0,index=0;
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
return arr;
}
findMaxElement(int *arr1,int len1,int *arr2,int len2)
{
//WRITE DOWN CODE HERE
arr1= sortArray(arr1,len1);
arr2= sortArray(arr2,len2);
if(arr1[len1-1]>arr2[len2-1])
return arr1[len1-1];
else
return arr2[len2-1];
}