Professional Documents
Culture Documents
Assessment 1
Assessment 1
1. Consider an integer array ‘data’ and another integer ‘n’. Write a program that will return the nth
smallest element in the array ‘data’.
Example 1:
Output: 30
Example 2:
Output: 40 Constraints:
Constraints:
Approach 1:
Approach 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
//Input the number of elements
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
if (n < 1 || n > 994 || arr[i] < 1 || arr[i] > 106)
{
cout << "Enter the data within limits.";
return 0;
}
}
int k;
cout << "Enter N: ";
cin >> k;
if (k > n)
{
cout << "Enter the data within limits.";
return 0;
}
set<int> s(arr, arr + n);
set<int>::iterator itr = s.begin(); // s.begin() returns a pointer to firs
t
// element in the set
advance(itr, k - 1); // itr points to kth element in set
return 0;
}
Approach 3:
#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
int temp;
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++)
{
cin >> temp;
if (n < 1 || n > 994 || temp < 1 || temp > 106)
{
cout << "Enter the data within limits.";
return 0;
}
arr.push_back(temp);
}
int k;
cout << "Enter N: ";
cin >> k;
if (k > n)
{
cout << "Enter the data within limits.";
return 0;
}
map<int, int> m;
for (int i = 0; i < n; i++)
{
m[arr[i]] += 1; // mapping every element with it's
// frequency
}
int ans = Kth_smallest(m, k);
cout << "The " << k << "th smallest element is " << ans
<< endl;
return 0;
}
2. Consider an array A of n random numbers.
(i) Write an iterative function MinMaxIterative to find the minimum and maximum element in the
given array A.
#include <bits/stdc++.h>
int main()
{
int n;
//Input number of elements
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
//Input the elements
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
(ii) Write a function following the divide and conquer approach (MinMaxDaC) to find the minimum
and maximum element in the given array A.
(iii) Compare the number of comparisons (among the elements of array A) by MinMaxIterative and
MinMaxDaC for n = 100, 200, 400, 1000, 10000
3. In 1202, Fibonacci introduced the sequence to Western European mathematics, although the
sequence had been described earlier in Indian mathematics, as early as200BC in work by Pingala on
enumerating possible patterns of Sanskrit poetry formed from syllables of two lengths. Consider the
Fibonacci sequence.0; 1; 1; 2; 3; 5; 8; 13; 21; : : :where,F0=0,F1=1.
1. Write a procedure to find Fn using recursive definition of the Fibonacci numbers. Discuss
the time complexity of the algorithm.
#include<bits/stdc++.h>
using namespace std;
int main ()
{
int n;
cout<<"Enter the number: ";
//Input the number for which you need to find Fibonacci
cin>>n;
cout<<"The Fibonacci number: ";
cout << fib(n);
return 0;
}
int fib(int n)
{
//Fibonacci of 1 is 1 and 2 is 1
if (n <= 1)
return n;
//Fib(i) = Fib(i-1) + Fib(i-2)
return fib(n-1) + fib(n-2);
}
2. Write a iterative procedure to find Fn. Discuss the time complexity of the algorithm.
#include <bits/stdc++.h>
int main ()
{
int n;
cout<<"Enter the number: ";
cin>>n;
cout<<"The Fibonacci number: ";
cout << fib(n);
return 0;
}
3. Design an efficient procedure to find Fn using Binet’s formula. Discuss the time complexity and
implementation issue if any.
#include <bits/stdc++.h>
int main()
{
int n;
//Input
cout<<"Enter the number: ";
cin>>n;
//Output
cout<<"The Fibonacci number: ";
cout << fib(n);
return 0;
}
4. Provide an efficient implementation of computing Fn following the Prof. Donald Knuth’s idea.
#include <bits/stdc++.h>
using namespace std;
// Helper function that multiplies 2 matrices fibo and Matrix of size 2*2, and
puts the multiplication result back to fibo[][]
void multiply(int fibo[2][2], int Matrix[2][2]);
// Helper function that calculates fibo[][] raise to the power n and puts the
result in fibo[][] Note that this function is designed only for fib() and won'
t work as general power function
void power(int fibo[2][2], int n);
if (n == 0)
return 0;
power(fibo, n - 1);
return fibo[0][0];
}
int main()
{
int n;
4. (i) Given two binary strings that represent value of two integers, find the product of two strings.
For example, if the first bit string is “1010” and second bit string is “0110”, output should be 60. Your
algorithm should be executing faster than O(n3).
#include <bits/stdc++.h>
int Base = 2;
//High Part
int high1 = n1;
int low2 = 0, j = 0;
while (j < m2)
{
low2 = low2 + (n2 % 10) * (int)power(10, j);
n2 = n2 / 10;
j++;
}
int high2 = n2;
//Finding Karatsuba for the Low, Middle and High Parts Respectively
int z0 = karatsuba(low1, low2);
int z1 = karatsuba((low1 + high1), (low2 + high2));
int z2 = karatsuba(high1, high2);
int output = a + b + c;
return output;
}
int NToDec(int num, int n)
{
if(n == 10)
{
return num;
}
int decimal = 0;
int main()
{
int number1, number2;
//Converting
number1 = NToDec(number1, Base);
number2 = NToDec(number2, Base);
(ii) Perform the multiplication of two numbers (base = 8) with the help of a faster algorithm.
#include <bits/stdc++.h>
int Base = 8;
//High Part
int high1 = n1;
int low2 = 0, j = 0;
while (j < m2)
{
low2 = low2 + (n2 % 10) * (int)power(10, j);
n2 = n2 / 10;
j++;
}
int high2 = n2;
//Finding Karatsuba for the Low, Middle and High Parts Respectively
int z0 = karatsuba(low1, low2);
int z1 = karatsuba((low1 + high1), (low2 + high2));
int z2 = karatsuba(high1, high2);
int output = a + b + c;
return output;
}
int NToDec(int num, int n)
{
if(n == 10)
{
return num;
}
int decimal = 0;
return decimal;
}
int main()
{
int number1, number2;
//Converting
number1 = NToDec(number1, Base);
number2 = NToDec(number2, Base);
(iii) Perform the multiplication of two numbers (number system base input by user) with the help of
a faster algorithm.
#include <bits/stdc++.h>
int Base;
//High Part
int high1 = n1;
int low2 = 0, j = 0;
while (j < m2)
{
low2 = low2 + (n2 % 10) * (int)power(10, j);
n2 = n2 / 10;
j++;
}
int high2 = n2;
//Finding Karatsuba for the Low, Middle and High Parts Respectively
int z0 = karatsuba(low1, low2);
int z1 = karatsuba((low1 + high1), (low2 + high2));
int z2 = karatsuba(high1, high2);
int output = a + b + c;
return output;
}
int NToDec(int num, int n)
{
if(n == 10)
{
return num;
}
int decimal = 0;
return decimal;
}
int main()
{
int number1, number2;
//Converting
number1 = NToDec(number1, Base);
number2 = NToDec(number2, Base);