TuningBill Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Name- Kaushal Kumar Roll No- 2019UCS0077 Branch - 2019 CSE

Q1) Find k smallest elements from a stream of integers.


Answer-> We are given a stream of integers which means that input is continuous and can go
on for a long time and we have to find the k smallest integers at a given time. The main problem
here is that as the input is continuous and it cannot be known whether the next element that
comes will be greater than or lower than previous elements. Now this problem can be solved in
many ways, first for maintaining a vector of k smallest elements and when the new elements
come then we have to check whether it satisfies the condition by iterating through the array. The
time complexity for this algorithm will be O(k) for a new element and for the complete solution
will be O(n*k). We can further lower the complexity by using a Priority Queue from C++ STL as
a Min Heap. Then we can access the largest element in O(1) and if the element is smaller then
the time for that will be O(logK) so the final time complexity will be O(Nlogk).
Solution in C++:

#include <bits/stdc++.h>
using namespace std;
void helper(vector<int>& nums, int k) {
priority_queue<int> pq1;
for(int i=0;i<nums.size();i++){
pq1.push(nums[i]);
}
int m=(nums.size()-k);
while(m--){
pq1.pop();
}

while(k--){
cout<<pq1.top()<<" ";
pq1.pop();
}
cout <<endl;
}
int main()
{
int n1,k;
cin >> n1;
cin >>k;
vector<int> v1;
for(int i=0;i<n1;i++){
int a;
cin>>a;
v1.push_back(a);
if(i>3) helper(v1,k);
}
}
Q2)Write a program that generates all permutations of a given input string s of size n.
ANSWER)
#include <bits/stdc++.h>
using namespace std;
vector<string> v1; // GLobal Variable to Store the results
void helper(string s, string s1,int k){
if(s1.length()==k){
v1.push_back(s1);
return; // backtracking for other permutations.
}
for(int i=0 ; i<s.length() ; i++)
{
char ans = s[i];
string left= s.substr(0,i);
string right = s.substr(i+1);
string rest = left + right;
helper(rest , s1+ans,k);
}
}
int main()
{
string s1="kaushal",s2="";
int k=3; // length of desired string
helper(s1,s2,k);
for(int i=0;i<v1.size();i++){
cout << v1[i]<<" ";
}
}

The time complexity for this binary search solution is O(n*n!) as the area where we have to
search becomes less and the space complexity is O(n!- no of permutations with length not equal
to k) .
Q3)Given an integer array A with n elements such that the elements first increase in value till an
index k, and later decrease in value till the end of the array. Return the element with the
maximum value in the array. k is of course unknown in the beginning.
ANSWER)
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n1;
cin >> n1;
vector<int> v1;
for(int i=0;i<n1;i++){
int a;
cin>>a;
v1.push_back(a);
}
int n = v1.size()-1;
int l=0,r=n-1;
while (l <= r) {
int m = l + (r - l) / 2;
if ((r == l + 1) && v1[l] >= v1[r]){
cout << v1[l];
break;
}
if ((r == l + 1) && v1[l] < v1[r])
{ cout << v1[r];
break;}

if (v1[m] > v1[m + 1] && v1[m] > v1[m - 1]){


cout << v1[m];
break;}
if (v1[m] > v1[m + 1] && v1[m] < v1[m - 1])
r = m - 1;

else
l = m + 1;
}
}

The time complexity for this binary search solution is O(logN) as the area where we have to
search becomes less and the space complexity is O(1) .

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy