Using C++ write a program to find the kth smallest element in the concatenation of two vectors A and B, given that both vectors are sorted in decreasing order. You can assume 1 ≤ k ≤ A.size() + B.size(). The function declaration is given below.
int findKthSmallest(vector A, vector B, int k);
For example, for A = [4, 2, 1] and B = [7, 5, 2, -3], the 2nd smallest element in the concatenation of A and B is 1.
Your job is to implement a findKthSmallest as a single function as described above.
Answer:
#include <iostream>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
int findKthSmallest(vector <int> A,vector <int> B,
int k)
{
int a_size,b_size;
vector <int> C;
a_size =A.size()-1;
b_size = B.size()-1;
while((a_size>=0)&&(b_size>=0))
{
if(A[a_size] < B[b_size])
{
C.push_back(A[a_size]);
a_size = a_size -1;
}
else
{
C.push_back(B[b_size]);
b_size--;
}
}
if(a_size<0){
while(b_size >=0)
{
C.push_back(B[b_size]);
b_size= b_size-1;
}
}
else
{
while(a_size >=0)
{
C.push_back(A[a_size]);
a_size= a_size-1;
}
}
return C[k-1];
}
int main()
{
vector <int> A;
vector <int> B;
int i,res,k;
for(i=0;i<3;i++)
{
A.push_back(4);
A.push_back(2);
A.push_back(1);
}
for(i=0;i<4;i++)
{
B.push_back(7);
B.push_back(5);
B.push_back(2);
B.push_back(-3);
}
cout<<"Enter k value for calculating smallest element merging
array :";
cin>>k;
res = findKthSmallest(A,B,k);
cout<<k<<" th smallest element is :"<<res;
return 0;
}
we need to traverse the array from the last index because array
is in descending order.
Always array last index starts from size -1.
we need to declare a new vector in order to store the merging
result.
If the element in the first array is lesser than the second array
then we need to store first array
value in the new array.Otherwise store the element of second
array.
we need to decrement the index value of the array from which you
have appended the element into result array.
we need to insert all the elements in sorted order in the resultant vector.
Array indexes starts from zero .so we need to extract the K-1th
index if we want to access kth element.



Using C++ write a program to find the kth smallest element in the concatenation of two...
write the method that return the kth smallest item in the binary search tree. for example if the tree has 18 node and the user enter 13 then the method should return the 13 smallest element in the tree. I has a method that done it recursively but I want to Implement findKth nonrecursively public String findKth( String k ) { return findKth( k, root).data; } public Node findKth(int k, Node t ) { if( t...
Write a program that can find the smallest element in a vector. Given a vector such as [1 2 -2 7 -8 9 3 1], you program has to find its smallest element and output it in the command window. In the vector above, the smallest element would be the -8. Your program should work for any vector with any size. the answer should be answered by matlab.
(Find the smallest element) use pointers to write a function that finds the smallest element in an array of integers. Use {1,2,4,5,10,100,2,-22} to test the function. using the following header. int minindex(double* list, int size) example output: array size: 8 number 1: 1 number 2: 2 number 3: 4 number 4: 5 number 5: 10 number 6: 100 number 7: 2 number 8: -22 smallest element is: -22 C++ only.
Using ONLY the following header functions: #include "cmpt_error.h" #include <iostream> #include <string> #include <vector> #include <cassert> using namespace std; Create a C++ function that satisfies the condition using recursion, NO WHILE OR FOR LOOPS. Pre-condition: a.size() == b.size(), and a.size() > 0 Post-condition: Returns a vector equal to {min(a[0],b[0]), min(a[1],b[1]), min(a[2],b[2]), ..., min(a[n],b[n])}, where n == a.size(). For example, min_vec({3, 4, 1}, {2, 5, 2}) returns the new vector {2, 4, 1}. These are the function headers: vector<int> min_vec(const vector<int>&...
C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...
Q1) Using Divide and conquer approach, solve the kth element in 2 sorted arrays problem. Given two sorted arrays both of size n find the element in k’th position of the combined sorted array. 1. Mention the steps of Divide, Conquer and Combine (refer to L5- Divide and Conquer Lecture notes, slide 3, to see an example on merge sort) 2. Draw the recursive tree. 3. What is the recurrence equation? 4. Guess a solution based on the recursive tree...
In C++
Sorted List of User Entered Numbers 2. Write a program that reads in a list of integers into a vector with base type int. Provide the facility to read this vector from an input file. Make sure to ask the user for a filename. The output is a two-column list. The first column is a list of the distinct vector elements. The second column is the count of the number of occurrences for each element. The list should...
this program is in C.
Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...
Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!
C# QUESTION Requirements Your task is to write a program that will print out all the s of the command line arguments to a program For example, given the arguments 'gaz, wx,and 'edc, your program should output wsxaazea Your implementation is expected to use Heap's algorithm according to the following pseudocode: procedure generatelk: integer, A: array of any): if k 1 then output A) else // Generate permutations with kth unaltered //Initially k length(A) // Generate permutations for kth swapped...