retagged by
560 views
2 votes
2 votes
int a[20];
unsigned int m; // global variable 
int foo(int a[]) {
	int i=0,count = 0;
	while(i < 20) m |= 1<<(a[i++]-1);
	i = (sizeof(int)<<3)-1;
	while(i>=0) 
		if(m&(1<<(i--))) count++;
	return count;
}

In the above C code,

Array a is initialized with elements $1\leq a[i] \leq 30$ and passed to this function, output of the function foo() is,

(Assume size of int is $4$ bytes)

A No of distinct elements in a[] which are more than $20$

B No of distinct elements in a[] which less than $20$

C No of distinct elements in a[]

D None of these 

retagged by

1 Answer

Best answer
3 votes
3 votes
int a[20];
unsigned int m; // global variable 
int foo(int a[]) {
	int i=0,count = 0;
	while(i < 20) 
	{
	    m |= 1<<(a[i++]-1);
//biwise OR, if a[i] is x, xth bit in m becomes 1
	}
	i = (sizeof(int)<<3)-1;
//i = 4 << 3 - 1, i = 31
//i should have been 30 for below code
	while(i>=0) 
		if(m&(1<<(i--))) count++;
//count gets incremented for each bit of m == 1
	return count;
//Returns the no. of distinct elements in a (a can have only 20 elements)
}
selected by
Answer:

Related questions


Deprecated: Implicit conversion from float-string "1651462735.029" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1651462735.029" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1651462735.029" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1651462735.029" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1651462733.856" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1651462733.856" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1651462733.856" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1651462733.856" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1559402682.654" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1559402682.654" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1559402682.654" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1559402682.654" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1558160596.945" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1558160596.945" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1558160596.945" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1558160596.945" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803
1.0k
views
3 answers
6 votes
GO Classes asked May 2, 2022
1,003 views
What will be output printed by the following program?#include<stdio.h main() { int c=4; switch(c) { c=c-1; case 4: printf("IITB ")...
734
views
2 answers
7 votes
GO Classes asked May 2, 2022
734 views
Trace the execution of the following if statements and select the output from this code fragmentint main() { int a = 3, b = 7 ; if ( (a + b) % 2 != 0 ) { printf("tetrahed...
6.2k
views
1 answers
6 votes
Satbir asked Jun 1, 2019
6,169 views
#include<stdio.h #include<stdlib.h int main(void) { int maxLineCount = 500, maxCharCount = 500, index, j, count; char *line = NULL; size_t size; char *a[maxLineCount]; fo...
1.2k
views
1 answers
0 votes
Aishvarya Akshaya Vi asked May 18, 2019
1,197 views
What will be the output of the below code? the answer given is E)0 but I am not getting it. #include <stdio.h void fun(short int *a,char *b) { b += 2; short int *...