Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

41. You are in a party of N people, where only one person is known to everyone.

Such a
person may be present in the party, if yes, (s)he doesn’t know anyone in the party.
Your task is to find the stranger (celebrity) in party.
You will be given a square matrix M[][] where if an element of row i and column j
is set to 1 it means ith person knows jth person. You need to complete the function
getId() which finds the id of the celebrity if present else return -1. The function
getId() takes two arguments, the square matrix M and its size N.
Note: Expected time complexity is O(N) with constant extra space.
Input:
The first line of input contains an element T denoting the number of test cases. Then
T test cases follow. Each test case consist of 2 lines. The first line of each test case
contains a number denoting the size of the matrix M. Then in the next line are space
separated values of the matrix M.
Output:
For each test case output will be the id of the celebrity if present (0 based index).
Else -1 will be printed.
User Task:
The task is to complete the function getId() which returns the Id of celebrity if
present, else -1.
Constraints:
1 <= T <= 50
2 <= N <= 501
0 <= M[][] <= 1
Answer:
#include<stdio.h>
#include<stdlib.h>
int getId(int mat[],int row,int column)
{
int i,j,k=0;
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(mat[i*row+j]==0)
k++;
else
break;
}
if(k==column)
return i;
else
k=0;
}
return -1;
}
main()
{
int **M;
int i,j,k,T,N,id;
scanf("%d",&T);
if(T<1 || T>50)
printf("Invalid T value\n");
else
{
scanf("%d",&N);
if(N<2 || N>501)
printf("Invalid N value\n");
else
{
M=(int**)malloc(sizeof(int*)*T);
for(i=0;i<N;i++)
{
M[i]=(int*)malloc(sizeof(int)*N*N);
}
for(i=0;i<T;i++)
{
for(j=0;j<N*N;j++)
{
scanf("%d",&M[i][j]);
}
}
for(i=0;i<T;i++)
{
id=getId(M[i],N,N);
printf("Id:%d\n",id);
}
}
}
}
Output:
Input (To be used only for expected output) :
1
3
010000010
Output :
1
Description:
For the above test case the matrix will look like
010
000
010
Here, the celebrity is the person with index 1 ie id 1

42. Given a string, reverse only vowels in it; leaving rest of the string as it is.
Answer:
#include<stdio.h>
#include<string.h>
void reverseVowels(char *);
int checkVowel(char);
main()
{
char str[30];
printf("Enter the String\n");
scanf("%s", str);
reverseVowels(str);
printf("%s\n",str);
}

void reverseVowels(char *str)


{
int len=strlen(str);
int i=0,j=len-1;
while(i<j)
{
while(checkVowel(str[i])==0)
i++;
while(checkVowel(str[j])==0)
j--;
if(i<j) // Swapping the vowel characters and moving to next index
{
char t;
t=str[i];
str[i]=str[j];
str[j]=t;
i++;
j--;
}
}
}

int checkVowel(char c)
{
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
return 1;
else
return 0;
}
Output:
Input : abcdef
Output : ebcdaf
Description:
For the above test case character ‘a’ will be swapped with ‘e’ and no other changes in the
string

43. Given an odd length word which should be printed from the middle of the word.
Answer:
#include<stdio.h>
#include<string.h>
main()
{
char str[25],mod[25];
int length,i,j,k,l,x=0;
scanf("%s",str);
length=strlen(str);
if(length%2==0)
printf("Invalid Input");
else
{
i=length/2;
for(k=0;k<=i;k++) // Creating the modified string for display
mod[k]=str[k+i];
for(l=0;l<=i;l++)
mod[k++]=str[l];
mod[k]='\0';
for(k=0;k<length;k++)
{
for(l=0;l<length-x;l++)
printf(" ");
for(j=0;j<=x;j++)
printf("%c",mod[j]);
printf("\n");
x++;
}
}
}
Output:
Input: PROGRAM
Output:
G
GR
GRA
GRAM
GRAMP
GRAMPR
GRAMPRO
Description:
In the code the input PROGRAM is first modified to GRAMPRO in another string and then
the string is displayed in the needed pattern
Predict the output Questions (User Defined Functions):

1 What is the output of the following code?


#include<stdio.h>
int compute(int x, int *y, int **z)
{
int sum;
*y=*y+3;
**z=**z+2;
sum=x+*y+**z;
return sum;
}
main()
{
int a=5;
int *b=&a;
int **c=&b;
printf(“%d”,compute(a,b,c));
}
Answer:
25
Description:
Since ‘a’ is passed as value and b and c are as references *y and **z refers to same memory
location which has data 5. So the data will be updated to 10. But value of x in function is still
5. So the computed sum will be 25.

2 How many times the function fact will be invoked in the following program?
int fact(int x)
{
If(x==0)
return 1;
else
return x*fact(x-1);
}
main()
{
printf(“%d”,fact(10));
}
Answer:
11
Description:
Since the base condition is for value 0, the fact(0) will also be invoked within fact(1).

3 #include <stdio.h>
void demo();
main()
{
void (*fun)();
fun = demo;
(*fun)();
fun();
return 0;
}

void demo()
{
printf("Welcome ");
}
Answer:
Welcome Welcome
Description:
fun is assigned to point to demo. So the two statements "(*fun)();" and "fun();" mean the
same.

4 Consider the following C-program:


void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d,", k);
}

main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf ("%dn", sum);
}
What does the above program print?
Answer:
2,0,4,8, 0
Description:
Since it is a recursive function the final remainder will be displayed followed by the
remainders in the computed in the previous calls and since it is call by value the value of sum
will be 0.

5 The value of j at the end of the execution of the following C program.


int incr(int i)
{
static int count = 0;
count = count + i;
return (count);
}
main()
{
int i,j;
for (i = 0; i <=4; i++)
j = incr(i);
}
Answer:
10
Description:
Since count is static the updated value of count will be preserved for each function invocation

You might also like