Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Assessment 1

Name: Aneesh Panda Course: CSE2012


Reg. No: 20BKT0088 Slot: L49+50

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:

Input: data = [30,20,10,50,60,40], k = 3

Output: 30

Example 2:

Input: data = [30,20,10,50,60,40], k = 4

Output: 40 Constraints:

Constraints:

• 1 <= n <= length of data <= 994

• 1 <= data[i] <= 106

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

cout << *itr << "\n";

return 0;
}

Approach 3:

#include <bits/stdc++.h>
using namespace std;

int Kth_smallest(map<int, int> m, int k)


{
int freq = 0;
for (auto it = m.begin(); it != m.end(); it++)
{
freq += (it->second); // adding the frequencies of
// each element
if (freq >= k) // if at any point frequency becomes
// greater than or equal to k then
// return that element
{
return it->first;
}
}
return -1; // returning -1 if k>size of the array which
// is an impossible scenario
}
int main()
{
int n;
//Input the number of elements
cout << "Enter the number of elements: ";
cin >> n;

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>

using namespace std;

//Declaring the iterative function


void MinMaxIterative(int*, int);

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];
}

//Call the iterative function


MinMaxIterative(arr, n);
}

void MinMaxIterative(int arr[], int n)


{
//Initialise Min and Max
int min = arr[0];
int max = arr[0];

for (int i = 1; i < n; i++)


{
//Set max
if (arr[i] > max)
{
max = arr[i];
}
//Set min
else if (arr[i] < min)
{
min = arr[i];
}
}

cout<<"Maximum Element in the array: "<<max<<endl;


cout<<"Minimum Element in the array: "<<min<<endl;
}

(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;

//Declaring the recursive function


int fib(int);

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>

using namespace std;

//Declaring the iterative function


int fib(int n)
{

// Declare an array to store Fibonacci numbers. 1 extra to handle case, n


= 0
int f[n + 2];
int i;

// 0th and 1st number of the series are 0 and 1


f[0] = 0;
f[1] = 1;

for (i = 2; i <= n; i++)


{
//Add the previous 2 numbers in the series and store it
f[i] = f[i - 1] + f[i - 2];
}
return f[n];
}

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>

using namespace std;

//Declaring the function


int fib(int n)
{
//Binet's Formula
double phi = (1 + sqrt(5)) / 2;
return round(pow(phi, n) / sqrt(5));
}

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);

//Declare the function


int fib(int n)
{
//Initialise the matrix
int fibo[2][2] = {{1, 1}, {1, 0}};

if (n == 0)
return 0;

power(fibo, n - 1);

return fibo[0][0];
}

//Declare the Multiplication Function


void multiply(int fibo[2][2], int Matrix[2][2])
{
//Initialising the elements of the matrix
int x = fibo[0][0] * Matrix[0][0] +
fibo[0][1] * Matrix[1][0];
int y = fibo[0][0] * Matrix[0][1] +
fibo[0][1] * Matrix[1][1];
int z = fibo[1][0] * Matrix[0][0] +
fibo[1][1] * Matrix[1][0];
int w = fibo[1][0] * Matrix[0][1] +
fibo[1][1] * Matrix[1][1];

//Assigning the variables of the matrix


fibo[0][0] = x;
fibo[0][1] = y;
fibo[1][0] = z;
fibo[1][1] = w;
}
//Declaring the power function
void power(int fibo[2][2], int n)
{
int i;
int Matrix[2][2] = {{1, 1}, {1, 0}};

// n - 1 times multiply the matrix to {{1,0},{0,1}}


for (i = 2; i <= n; i++)
multiply(fibo, Matrix);
}

int main()
{
int n;

//Input the number


cout << "Enter the number: ";
cin >> n;
cout << "The Fibonacci number: ";
cout << fib(n);
return 0;
}

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;

using namespace std;

//Function to find the number of digits


int NoOfDigits(int n)
{
//Initialising Count
int count = 0;

//Finding the number of digits


while (n != 0)
{
n = n / 10;
++count;
}
return count;
}

//Function to find the power


int power(int x, int y)
{
int temp;

//Finding the value of the power


if (y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}

//Initialising the Kasturba Function


int karatsuba(int n1, int n2)
{
if (n1 < Base || n2 < Base)
{
return n1 * n2;
}

//Calculate the size of numbers


int size1 = NoOfDigits(n1);
int size2 = NoOfDigits(n2);
int m = max(size1, size2);
int m2 = m / 2;

//Splitting the numbers


//Low Part
int low1 = 0, i = 0;
while (i < m2)
{
low1 = low1 + (n1 % 10) * (int)power(10, i);
n1 = n1 / 10;
i++;
}

//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);

//Finding the Number to Return


int a = z2 * (power(Base, 2 * m2));
int b = (z1 - z2 - z0) * (power(Base, m2));
int c = z0;

int output = a + b + c;

return output;
}
int NToDec(int num, int n)
{
if(n == 10)
{
return num;
}
int decimal = 0;

// Initializing base value to 1, since n^0 = 1


int base = 1;

//Temporary Variable to iterate through for multiplication


int temp = num;
while (temp)
{
//Finding the last digit
int last_digit = temp % 10;
//Reducing the number
temp = temp / 10;
//Finding the decimal number
decimal += last_digit * base;
//For the next digit
base = base * n;
}
return decimal;
}

int main()
{
int number1, number2;

//Inputting the Numbers


cout << "Enter the Binary Number 1: ";
cin >> number1;
cout << "Enter the Binary Number 2: ";
cin >> number2;

//Converting
number1 = NToDec(number1, Base);
number2 = NToDec(number2, Base);

//Finding using Karatsuba


int res = karatsuba(number1, number2);
cout << "Product of " << number1 << " and " << number2 << " is " << res <<
endl;
}

(ii) Perform the multiplication of two numbers (base = 8) with the help of a faster algorithm.

#include <bits/stdc++.h>

int Base = 8;

using namespace std;

//Function to find the number of digits


int NoOfDigits(int n)
{
//Initialising Count
int count = 0;

//Finding the number of digits


while (n != 0)
{
n = n / 10;
++count;
}
return count;
}

//Function to find the power


int power(int x, int y)
{
int temp;

//Finding the value of the power


if (y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}

//Initialising the Kasturba Function


int karatsuba(int n1, int n2)
{
if (n1 < Base || n2 < Base)
{
return n1 * n2;
}

//Calculate the size of numbers


int size1 = NoOfDigits(n1);
int size2 = NoOfDigits(n2);
int m = max(size1, size2);
int m2 = m / 2;

//Splitting the numbers


//Low Part
int low1 = 0, i = 0;
while (i < m2)
{
low1 = low1 + (n1 % 10) * (int)power(10, i);
n1 = n1 / 10;
i++;
}

//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);

//Finding the Number to Return


int a = z2 * (power(Base, 2 * m2));
int b = (z1 - z2 - z0) * (power(Base, m2));
int c = z0;

int output = a + b + c;

return output;
}
int NToDec(int num, int n)
{
if(n == 10)
{
return num;
}
int decimal = 0;

// Initializing base value to 1, since n^0 = 1


int base = 1;

//Temporary Variable to iterate through for multiplication


int temp = num;
while (temp)
{
//Finding the last digit
int last_digit = temp % 10;
//Reducing the number
temp = temp / 10;
//Finding the decimal number
decimal += last_digit * base;
//For the next digit
base = base * n;
}

return decimal;
}
int main()
{
int number1, number2;

//Inputting the Numbers


cout << "Enter the Octal Number 1: ";
cin >> number1;
cout << "Enter the Octal Number 2: ";
cin >> number2;

//Converting
number1 = NToDec(number1, Base);
number2 = NToDec(number2, Base);

//Finding using Karatsuba


int res = karatsuba(number1, number2);
cout << "Product of " << number1 << " and " << number2 << " is " << res <<
endl;
}

(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;

using namespace std;

//Function to find the number of digits


int NoOfDigits(int n)
{
//Initialising Count
int count = 0;

//Finding the number of digits


while (n != 0)
{
n = n / 10;
++count;
}
return count;
}

//Function to find the power


int power(int x, int y)
{
int temp;

//Finding the value of the power


if (y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}

//Initialising the Kasturba Function


int karatsuba(int n1, int n2)
{
if (n1 < Base || n2 < Base)
{
return n1 * n2;
}

//Calculate the size of numbers


int size1 = NoOfDigits(n1);
int size2 = NoOfDigits(n2);
int m = max(size1, size2);
int m2 = m / 2;

//Splitting the numbers


//Low Part
int low1 = 0, i = 0;
while (i < m2)
{
low1 = low1 + (n1 % 10) * (int)power(10, i);
n1 = n1 / 10;
i++;
}

//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);

//Finding the Number to Return


int a = z2 * (power(Base, 2 * m2));
int b = (z1 - z2 - z0) * (power(Base, m2));
int c = z0;

int output = a + b + c;

return output;
}
int NToDec(int num, int n)
{
if(n == 10)
{
return num;
}
int decimal = 0;

// Initializing base value to 1, since n^0 = 1


int base = 1;

//Temporary Variable to iterate through for multiplication


int temp = num;
while (temp)
{
//Finding the last digit
int last_digit = temp % 10;
//Reducing the number
temp = temp / 10;
//Finding the decimal number
decimal += last_digit * base;
//For the next digit
base = base * n;
}

return decimal;
}

int main()
{
int number1, number2;

//Input the base


cout << "Enter the base: ";
cin >> Base;

//Inputting the Numbers


cout << "Enter the Base Number 1: ";
cin >> number1;
cout << "Enter the Base Number 2: ";
cin >> number2;

//Converting
number1 = NToDec(number1, Base);
number2 = NToDec(number2, Base);

//Finding using Karatsuba


int res = karatsuba(number1, number2);
cout << "Product of " << number1 << " and " << number2 << " is " << res <<
endl;
}

You might also like