TuningBill Assignment
TuningBill Assignment
TuningBill Assignment
#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;}
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) .