Sandeep
// C++ Program to find kth element from two sorted arrays
#include <iostream>
using namespace std;
int kthElement(int arr1[], int arr2[], int n, int m, int k)
{
// C++ Program to find kth element from two sorted arrays
// In case we have reached end of array 1
int st1=0, st2=0;
while (1)
{
if (st1 == n)
return arr2[st2 + k - 1];
// In case we have reached end of array 2
if (st2 == m)
return arr1[st1 + k - 1];
// k should never reach 0 or exceed sizes
// of arrays
if (k == 0 || k > (n - st1) + (m - st2))
return -1;
// Compare first elements of arrays and return
if (k == 1)
return (arr1[st1] < arr2[st2]) ?
arr1[st1] : arr2[st2];
int curr = k / 2;
// Size of array 1 is less than k / 2
if(curr-1>=n-st1)
{
// Last element of array 1 is not kth
// We can directly return the (k - m)th
// element in array 2
if (arr1[n - 1] < arr2[st2 + curr - 1])
return arr2[st2 + (k - (n - st1) - 1)];
else
{
st2 += curr;
k -= curr;
}
}
// Size of array 2 is less than k / 2
if(curr - 1 >= m - st2)
{
if (arr2[m - 1] < arr1[st1 + curr - 1])
return arr1[st1 + (k - (m - st2) - 1)];
else
{
st1 += curr;
k -= curr;
}
}
else
{
// Normal comparison, move starting index
// of one array k / 2 to the right
if (arr1[curr + st1 - 1] < arr2[curr + st2 - 1])
🥲🥲🥲🥲🥲🥲🥲//This condition fails if the size of the step ..i.e curr exceeds the size of array...so this code is giving me error if there is a skew in the size of the array
{
st1 += curr;
k -= curr;
}
else
{
st2 += curr;
k -= curr;
}
}
}
}
// Driver code
int main()
{
int arr1[7] = { 5 ,33, 55, 65, 76, 80, 90 };
int arr2[39] = { 10 ,13 ,14, 15, 15, 22, 27, 32, 34, 36, 36, 37, 39, 40, 42, 45, 49, 50, 50, 53, 56, 56, 57, 61, 65, 66, 70, 70 ,71, 74, 78, 84, 87, 90, 91, 94, 94 ,96 ,99 };
int k = 39;
int p= kthElement(arr1, arr2, 7, 39, k);
return 0;
}
hazer_hazer
Hi
I've got template function that I directly call somewhere:
template<class First, class ...Rest>
void foo(First && first, Rest && ...other) const {
bar(first);
bar(other...);
}
It invokes these:
template<class Arg>
void bar(Arg && single) const {
std::cout << single << ' ';
}
void bar(Specific && specific) const {
std::cout << "SPECIFIC";
}
template<class ...Args>
void bar(Args && ...args) const {
(bar(std::forward<Args>(args)), ...);
}
I thought that it gonna work because C++ chooses the most specific overloading, but, as I see, it doesn't if I call it from another templated function, so bar(Specific) never be called. I know, that if I directly call bar then bar(Specific) will be used if I pass Specific type, anyway, from in templated function it is not.
Help please 😔
Himanshu
1st-----
#include<bits/stdc++.h>
using namespace std;
void check_req(int X, int Y, int K, int N){
bool oke = false;
int page[N], cost[N];
for(int i=0; i<N; i++){
cin >> page[i] >> cost[i];
}
for(int i=0; i<N; i++){
if(page[i] >= X-Y && cost[i] <= K){
oke = true;
break;
}
}
if(oke)
cout << "LuckChef" << endl;
else
cout << "UnluckyChef" << endl;
}
int main(){
int t;
cin >> t;
int X, Y, K, N;
for(int i=0; i<t; i++){
cin >> X >> Y >> K >> N;
check_req(X, Y, K, N);
}
return 0;
}
2nd----
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
for(int i=0; i<t; i++) {
int X, Y, K, N;
cin>> X >> Y >> K >> N ;
int pages[N], costs[N];
for(int i=0; i<N; i++) {
cin>>pages[i]>>costs[i];
}
bool flag = false;
for(int i=0; i<N; i++) {
if(pages[i] >= X-Y && costs[i] <= K) {
flag = true;
break;
}
}
if(flag)
cout<<"LuckyChef"<<endl;
else
cout<<"UnluckyChef"<<endl;
}
return 0;
}
Why 2nd code is faster than 1st code??