Miscellaneous On Data Structures - Study Mode
[#1001] What will be the output for the following code? #include <stdio.h>
#include <stdlib.h>
void combination(int arr[], int aux[], int start, int end, int index, int r)
int compare (const void * a, const void * b)
{ return ( *(int*)a - *(int*)b )
}
void print(int arr[], int n, int r)
{
int aux[r]
qsort (arr, n, sizeof(int), compare)
combination(arr, aux, 0, n-1, 0, r)
}
void combination(int arr[], int aux[], int start, int end, int index, int r)
{
if (index == r)
{
for (int i=0
i<r
i++)
printf("%d " ,aux[i])
printf(", ")
return
}
for (int i=start
i<=end && end-i+1 >= r-index
i++)
{
aux[index] = arr[i]
combination(arr, aux, i+1, end, index+1, r)
while (arr[i] == arr[i+1])
i++
}
}
int main()
{
int arr[] = {1, 2, 2}
int r = 2
int n = sizeof(arr)/sizeof(arr[0])
print(arr, n, r)
}
Correct Answer
(C) 1 2, 2 2,
[#1002] What will be the recurrence relation of the following code? int xpowy(int x, int n)
if (n==0) return 1
if (n==1) return x
if ((n % 2) == 0)
return xpowy(x*x, n/2)
else
return xpowy(x*x, n/2) * x
Correct Answer
(D) T(n) = T(n/2) + O(1)
[#1003] What will be the output of the following code? #include <bits/stdc++.h>
using namespace std
void convert(int arr[], int n)
{
int temp[n]
memcpy(temp, arr, n*sizeof(int))
sort(temp, temp + n)
unordered_map<int, int> map
int sort_index = 0
for (int i = 0
i < n
i++)
map[temp[i]] = sort_index++
for (int i = 0
i < n
i++)
arr[i] = map[arr[i]]
}
void printArr(int arr[], int n)
{
for (int i=0
i<n
i++)
cout << arr[i] << " "
}
int main()
{
int arr[] = {3,5,2,4}
int n = sizeof(arr)/sizeof(arr[0])
convert(arr , n)
printArr(arr, n)
return 0
}
Correct Answer
(B) 1 3 0 2
[#1004] What will be the output for following code? #include<stdio.h>
int func(int x, int y)
{
if (y == 0)
return 1
else if (y%2 == 0)
return func(x, y/2)*func(x, y/2)
else
return x*func(x, y/2)*func(x, y/2)
}
int main()
{
int x = 2
int y = 3
printf("%d", func(x, y))
return 0
}
Correct Answer
(C) 8
[#1005] What is the output of the following code? #include<stdio.h>
int get_sum(int n)
{
int sm, i
for(i = 1
i <= n
i++)
sm += i
return sm
}
int main()
{
int n = 10
int ans = get_sum(n)
printf("%d",ans)
return 0
}
Correct Answer
(D) Depends on compiler