Examveda

What will be the time complexity of the given code?
#include <stdio.h> 
#include <string.h> 
#include <iostream.h>
using namespace std;
void swap(char *x, char *y) 
{ 
	char temp; 
	temp = *x; 
	*x = *y; 
	*y = temp; 
} 
 
void func(char *a, int l, int r) 
{ 
int i; 
if (l == r) 
	cout<<a<<” ,”; 
else
{ 
	for (i = l; i <= r; i++) 
	{ 
		swap((a+l), (a+i)); 
		func(a, l+1, r); 
		swap((a+l), (a+i)); 
	} 
} 
} 
 
int main() 
{ 
	char str[] = "AB"; 
	int n = strlen(str); 
	func(str, 0, n-1); 
	return 0; 
}

A. O(n2)

B. O(n * n!)

C. O(n!)

D. O(n log n)

Answer: Option B


This Question Belongs to Data Structure >> Miscellaneous On Data Structures

Join The Discussion

Related Questions on Miscellaneous on Data Structures