Questions asked by montycshttps://discuss.codechef.com/questions/asked-by/190608/montycs/?type=rssQuestions asked by <a href="/users/190608/montycs" >montycs</a>enFri, 23 Feb 2018 22:43:25 +0530Best place for a beginnerhttps://discuss.codechef.com/questions/94507/best-place-for-a-beginner<p>I want to know a beginner-friendly site among codechef, hackerrank, hackerearth etc. I started with spoj and after a while, the complexity increased after 15 odd questions. I want to follow a step-by-step procedure where I can learn basics. I want to where you guys started, places you practised at and resources you guys followed.</p>montycsTue, 28 Mar 2017 17:23:36 +0530https://discuss.codechef.com/questions/94507/best-place-for-a-beginnerhelpNext Steps for a Beginnerhttps://discuss.codechef.com/questions/97506/next-steps-for-a-beginner<p>It's been a month or so since I started competitive programming. I am taking it step by step. I want to cover basics at first then move on to intermediate problems.
I'v solved some 9-10 easy implementation problems at Hackerrank. I can say that I can solve problems that require operations on arrays. Now, I want to make the next move. I want to learn further topics. I started searching problems on Hackerrank but they require strong hold on basics.
I just want to know how should I proceed further. Where should I be practicing? Some people say Spoj is the best place where others say Hackerrank. Please guide me here the best place to go such that I can clear my basics and build a strong foundation.</p>montycsWed, 03 May 2017 21:32:04 +0530https://discuss.codechef.com/questions/97506/next-steps-for-a-beginnertutorialsMerge two sorted Linked Listhttps://discuss.codechef.com/questions/98684/merge-two-sorted-linked-list<p>I had a doubt in this problem: <a href="https://www.hackerrank.com/challenges/merge-two-sorted-linked-lists">link</a>
I am following this solution:</p>
<pre><code>if((headA==NULL)&&(headB==NULL)
return NULL;
if((headA!=NULL)&&(headB==NULL))
return headA;
if((headA == NULL)&&(headB!=NULL))
return headB;
if(headA->data < headB->data)
headA->next = MergeLists(headA->next, headB);
else if(headA->data > headB->data)
{
Node* temp = headB;
headB = headB->next;
temp->next = headA;
headA = temp;
headA->next = MergeLists(headA->next, headB);
}
return headA;
</code></pre>
<p>I get that when headA->data < headB->data then we simply move the headA pointer to the next node. But when headA->data > headB->data, then we create a temp pointer, point it where headA is pointing and move headB to next node.
What I don't get is:</p>
<ol>
<li>
<p>How are the previous nodes of headA that were already sorted are linked to this newly created node temp(now headA) as there is no link mentioned in this code.</p>
</li>
<li>
<p>Also, where is the headA pointing next? Is it at the current new temp node?</p>
</li>
</ol>montycsMon, 22 May 2017 21:16:55 +0530https://discuss.codechef.com/questions/98684/merge-two-sorted-linked-listlinked-listSNCKQL 17 - SNAKPROC Helphttps://discuss.codechef.com/questions/99309/snckql-17-snakproc-help<p>I need help in this question : <a href="https://www.codechef.com/SNCKQL17/problems/SNAKPROC">https://www.codechef.com/SNCKQL17/problems/SNAKPROC</a></p>
<p>I used stack to push when 'H' is encountered,pop when 'T' is encountered and do nothing when '.' is encountered. Since there weren't any real elements to push or pop, I simply used 1 as a dummy element that would be pushed or popped whenever required. But still I am get WA. Can anyone point out my mistake? Here's my solution:</p>
<pre><code>#include <iostream>
using namespace std;
int top = -1;
int push(int a[], int x, int n)
{
if(top == n-1)
return 0;
else
{
top += 1;
a[top] = x;
}
return 1;
}
int pop(int a[], int n)
{
if(top == -1)
return 0;
else
{
top--;
}
return 1;
}
int topElement(int a[])
{
return a[top];
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
string s;
cin>>s;
int len;
len = s.length();
bool flag = true;
for(int i=0;i<len;i++)
{
//char x;
//cin>>x;
if(s[i] == '.')
continue;
else if(s[i] == 'H')
{
if(topElement(a) == 1)
{
flag = false;
//break;
}
else push(a, 1, n);
}
else if(s[i] == 'T')
{
pop(a,n);
if(topElement(a) <= -1)
{
flag = false;
//break;
}
}
}
if(flag == true && topElement(a) != 1)
cout<<"Valid"<<endl;
else cout<<"Invalid"<<endl;
}
return 0;
}
</code></pre>montycsSat, 27 May 2017 21:11:25 +0530https://discuss.codechef.com/questions/99309/snckql-17-snakproc-helpsnckql17helpTLG Problem: I am using an approach without the use of arrays as mentioned.https://discuss.codechef.com/questions/112302/tlg-problem-i-am-using-an-approach-without-the-use-of-arrays-as-mentioned<p>HI All, I was solving this problem <a href="https://www.codechef.com/problems/TLG">TLG</a>. The method mentioned in the editorials involves creating 3-4 arrays and then calculating the answer. I am trying a different approach where the lead is calculated during the input of the scores and then compared with the previous lead. I am getting the desired output in my IDE however there seems to be some test case that I am missing because of which I am getting wrong answer at the codechef compiler. Please help in pointing out the mistake in my approach. Here is my code:</p>
<pre><code>#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n];
int b[n];
int p1,p2;
int diff,ans,player;
ans = 0;
p1 = p2 = 0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
cin>>b[i];
p1 += a[i];
p2 += b[i];
diff = abs(a[i] - b[i]);
if(diff > ans)
{
ans = diff;
player = (p1 > p2)?1:2;
}
//cout<<p1<<" "<<p2<<endl;
}
cout<<player<<" "<<ans<<endl;
return 0;
}
</code></pre>montycsWed, 20 Sep 2017 18:51:03 +0530https://discuss.codechef.com/questions/112302/tlg-problem-i-am-using-an-approach-without-the-use-of-arrays-as-mentionedarrayZCOPRAC Video Game Help Need in One Conditionhttps://discuss.codechef.com/questions/112933/zcoprac-video-game-help-need-in-one-condition<p>I am trying to solve <a href="https://www.codechef.com/ZCOPRAC/problems/ZCO14001">this</a>problem. I have implemented the code but there seems to be some mistake with the logic that I am using. I think the problem is when c==4. Can you please help me figure this out.</p>
<p>#include <iostream></p>
<pre><code>using namespace std;
int main()
{
int n,h;
cin>>n>>h;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int c;
cin>>c;
int j=0;
while(c!=0)
{
bool isPicked = false;
if(c==1 && j>0)
{
j--;
}
if(c==2 && j<n-1)
{
j++;
}
if(c==3)
{
if(!isPicked && a[j] > 0)
{
isPicked = true;
a[j]--;
}
}
if(c==4)
{
if(isPicked && a[j] < h)
{
isPicked = false;
a[j]++;
}
}
cin>>c;
}
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
</code></pre>montycsSat, 30 Sep 2017 21:39:44 +0530https://discuss.codechef.com/questions/112933/zcoprac-video-game-help-need-in-one-conditionarraypracticeCompetitive Programming Crash Coursehttps://discuss.codechef.com/questions/114507/competitive-programming-crash-course<p>I have 4 days[Diwali] holidays coming up this week and want to cover most of the basics topics or questions required such that I can start solving advanced topics like Graph, DP etc. Basically what I am looking for is solving those problems that have some important concepts that will be used repetitively in future like sieve of Eratosthenes. I have basic understanding of programming and am below average in mathematics. So please give me suggestions accordingly.</p>montycsSun, 15 Oct 2017 16:28:53 +0530https://discuss.codechef.com/questions/114507/competitive-programming-crash-coursepracticebasicstutorialc++Max Mex Helphttps://discuss.codechef.com/questions/114902/max-mex-help<p>I am solving <a href="https://www.codechef.com/problems/MEX">MEX</a> as a practice problem but I am not able to pass the second subtask's second condition, it keeps giving me WA.. Please help me with the test case. My code:</p>
<pre><code>#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
int a[n];
int c[200000]={0};
for(int i=0;i<n;i++)
{
cin>>a[i];
c[a[i]]++;
}
int ans = 0;
for(int i=0;i<200000;i++)
{
if(c[i] == 0 && k >= 0)
{
ans = i;
k--;
}
}
cout<<ans<<endl;
}
return 0;
}
</code></pre>montycsThu, 19 Oct 2017 19:29:15 +0530https://discuss.codechef.com/questions/114902/max-mex-helparrayoct17c++RAINBOWA Helphttps://discuss.codechef.com/questions/114909/rainbowa-help<p>I'v been keeping <a href="https://www.codechef.com/problems/RAINBOWA">this</a> problem in my TODO list ever since it came out. I am still not able to crack it. I get the problem's requirement but am facing problems while implementing it. I am trying to first check if the input number is palindrome or not by <code>a[i] == a[n-i-1]</code> logic. But after that I am not able to put the logic of checking if the next number is in the sequence part to code. I am trying to set a variable j = 1 and check <code>if(a[i+1] == j+1)</code> then increment j then compare the elements with j.. It should end when next element is not equal to j or j > 7. I know this logic is filled with flaw. Also I read the editorial which further increased my confusion by using ones(1). Please help me by simplifying the logic of <a href="https://www.codechef.com/problems/RAINBOWA">RAINBOWA</a> problem.</p>montycsThu, 19 Oct 2017 20:57:27 +0530https://discuss.codechef.com/questions/114909/rainbowa-helpaug17berezincakewalkWhat is the difference between these two?https://discuss.codechef.com/questions/114978/what-is-the-difference-between-these-two<p>While I was solving <a href="https://www.codechef.com/problems/CNOTE">CNOTE</a>, I set <code>bool = true</code> and implemented the solution. The solution worked fine in my IDE but on Codechef IDE it threw WA. here's the link to that solution: <a href="https://www.codechef.com/viewsolution/15904044">https://www.codechef.com/viewsolution/15904044</a></p>
<p>But when I changed bool to false and then implemented the same code, it got AC. Here's the link: <a href="https://www.codechef.com/viewsolution/15904061">https://www.codechef.com/viewsolution/15904061</a>.
Please help me in finding my mistake with the original solution.</p>montycsFri, 20 Oct 2017 15:07:15 +0530https://discuss.codechef.com/questions/114978/what-is-the-difference-between-these-twoarrayhelpc++How to come up with such ad-hoc solutions?https://discuss.codechef.com/questions/114994/how-to-come-up-with-such-ad-hoc-solutions<p>I was solving <a href="https://www.codechef.com/problems/SALARY">SALARY</a> and except for coming up with naive brute-force method, I couldn't figure out the logic behind this problem. It was only after seeing the editorial I understood the logic. So, My question is how to come up with solutions to such problems. Because it was just some AP or formula.</p>montycsFri, 20 Oct 2017 17:05:54 +0530https://discuss.codechef.com/questions/114994/how-to-come-up-with-such-ad-hoc-solutionsad-hocmathslogicPlease add more questions to this linkhttps://discuss.codechef.com/questions/114999/please-add-more-questions-to-this-link<p>I am following <a href="https://www.codechef.com/certification/prepare#foundation">https://www.codechef.com/certification/prepare#foundation</a> link to prepare and clear my concepts in competitive programming. I am following it step by step. However, I feel there are very few questions mentioned in this link. Also, most of them are of Codechef. So, it's my kind request to all the regular programmers here to please add a few question here[in the answer] which could be from codechef, hackerrank, spoj etc. All those questions that you feel are extremely important as fundamentals or concepts that you remember helped you in clearing and building your logic. Please add links in the answer below. Also if you feel some important ad-hoc or any other category's question that are must-solve for a beginner then please add them here.</p>montycsFri, 20 Oct 2017 17:45:05 +0530https://discuss.codechef.com/questions/114999/please-add-more-questions-to-this-linkprogrammingproblemscompetitiveSPOJ Histogram Helphttps://discuss.codechef.com/questions/115287/spoj-histogram-help<p>I was trying to decode the logic in <a href="http://www.spoj.com/problems/HISTOGRA/">SPOJ HISTOGRA</a> problem but couldn't think of any good solution. So, I referred <a href="http://www.geeksforgeeks.org/largest-rectangle-under-histogram/">this</a> link. Now, I do get the logic behind this problem but now I am stuck at one part.
Here's my understanding:
1. We traverse the array from left to right
2. If the stack is empty or if a[i+1]th element is greater than a[i] then we push
3. If the next element is smaller than we pop and then we calculate the popped element's area.
Now, I am stuck at the step after this. I don't seem to understand how to calculate the area of the popped element. I mean, with respect to which elements should I be calculating the area?
The link I'm referring to says it should be<code>area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1);</code>. I am not able to map this statement. Yes, I do know this ternary condition's output but I am not able to understand how the area is calculated with hist[tp]</p>montycsTue, 24 Oct 2017 22:09:52 +0530https://discuss.codechef.com/questions/115287/spoj-histogram-helpspojarraystackVILTRIBE Helphttps://discuss.codechef.com/questions/116497/viltribe-help<p>What should be the output of A..B...B...A ?</p>montycsWed, 08 Nov 2017 21:31:53 +0530https://discuss.codechef.com/questions/116497/viltribe-helpad-hocMAXDIFF: Which test cases am I failing?https://discuss.codechef.com/questions/116534/maxdiff-which-test-cases-am-i-failing<p>Can you please tell me which test cases my code is throwing WA for <a href="https://www.codechef.com/problems/MAXDIFF">MAXDIFF</a>?
<a href="https://www.codechef.com/viewsolution/16190441">https://www.codechef.com/viewsolution/16190441</a></p>montycsThu, 09 Nov 2017 21:21:15 +0530https://discuss.codechef.com/questions/116534/maxdiff-which-test-cases-am-i-failinggreedyHelp in Codeforces #446 Ahttps://discuss.codechef.com/questions/117753/help-in-codeforces-446-a<p>Can anyone help me in understanding this problem <a href="http://codeforces.com/contest/892/problem/A">Greed</a>. It sounds simple but I don't seem to get the logic. Also the editorials don't have much</p>montycsSat, 18 Nov 2017 13:59:41 +0530https://discuss.codechef.com/questions/117753/help-in-codeforces-446-acodeforcesWeek of Code 35 Lucky Purchase Helphttps://discuss.codechef.com/questions/118291/week-of-code-35-lucky-purchase-help<p>I had participated in Hackerrank Week of Code 35 and came across <a href="https://www.hackerrank.com/contests/w35/challenges/lucky-purchase">this</a> question. I couldn't get full points on this as it showed segmentation fault while evaluation. But, when the editorials came out, I found my solution to be somewhat matching with that of the editorial. Please help in pointing out my mistake.
Here's my code:
#include <iostream></p>
<pre><code>using namespace std;
bool isValid(long long n)
{
long long temp;
temp = n;
int c[2] = {0};
while(n != 0)
{
temp = n%10;
if(temp != 4 && temp != 7)
return 0;
if(temp == 4)
c[0]++;
else if(temp == 7)
c[1]++;
n = n/10;
}
if(c[0] == c[1])
{
return 1;
}
else
return 0;
}
int main()
{
long long n;
cin>>n;
long long ans,index;
ans = INT8_MAX;
string name[100];
bool flag = false;
long long price[INT8_MAX];
for(long long i=0;i<n;i++)
{
cin>>name[i];
cin>>price[i];
}
for(long long i=0;i<n;i++)
{
if(isValid(price[i]))
{
flag = true;
if(price[i] < ans)
{
ans = price[i];
index = i;
}
}
}
if(flag)
{
cout<<name[index];
}
else
cout<<"-1";
return 0;
}
</code></pre>montycsTue, 21 Nov 2017 21:50:49 +0530https://discuss.codechef.com/questions/118291/week-of-code-35-lucky-purchase-helphackerrankIs there an easier method to solve this?https://discuss.codechef.com/questions/118524/is-there-an-easier-method-to-solve-this<p>I came across the editorial of <a href="https://www.hackerrank.com/challenges/two-characters/problem">https://www.hackerrank.com/challenges/two-characters/problem</a> but found it a bit complex. So, Is there any easier method to solve this using C++?</p>montycsSat, 25 Nov 2017 20:54:10 +0530https://discuss.codechef.com/questions/118524/is-there-an-easier-method-to-solve-thishackerrankHelp in Tower of Hanoihttps://discuss.codechef.com/questions/118904/help-in-tower-of-hanoi<p>Hi, I'v been stuck on Tower of Hanoi for quite some time now. I have a fair bit of understanding of recursion and the way function calls itself. But here in Tower of Hanoi, I am unable to understand the flow of the algorithm.
Here's the algorithm I'm following:</p>
<p>T(N, Beg, Aux, End)</p>
<pre><code> 1. T(N-1, Beg, End, Aux)
2. T(1, Beg, Aux, End)
3. T(N-1, Aux, End)
</code></pre>
<p>So, according to the above algorithm, First we swap N-1 disks to an auxiliary peg, then we swap the Nth peg to the destination peg and repeat the following.
But, When I take an example Say T(3, A, B, C) where I have to move disks from A to C, here are the steps that I follow:</p>
<pre><code> 1. (2, A, C, B) ... T(N-1, Beg, End, Aux)
2. (1, A, B, C) ... T(1, Beg, Aux)
3. ?
</code></pre>
<p>I am really confused as which step(s) will follow afterwards. Please help me in figuring out this algorithm. Also, if possible please help me in understanding which functions would go in the stack sequentially..</p>montycsThu, 30 Nov 2017 22:13:19 +0530https://discuss.codechef.com/questions/118904/help-in-tower-of-hanoirecursionDecember Long Challenge 2017 GIT01https://discuss.codechef.com/questions/119686/december-long-challenge-2017-git01<p>I'v seen a lot of people solved this problem with a similar approach i.e take one character at a time and then check for the (i+j)%2 == 0 condition. I for one was stuck at the 2-D array thing and couldn't solve the problem. Are there any similar problems?</p>montycsWed, 13 Dec 2017 21:39:38 +0530https://discuss.codechef.com/questions/119686/december-long-challenge-2017-git01problemHelp in Merge Sort Algorithmhttps://discuss.codechef.com/questions/120241/help-in-merge-sort-algorithm<p>I am studying Merge Sort and have implemented the algorithm. Although I do understand it's recursive nature and the flow, I do not understand how the recursive function is hitting the base condition since it is not mentioned in the code. I implemented this code with the help of other implementation of the same. I have written the comment about the line I am finding it difficult about it's base condition being hit.
#include <iostream></p>
<pre><code>using namespace std;
void Merge(int a[], int l, int mid, int r)
{
int n1,n2,i,j,k;
n1 = mid - l + 1;
n2 = r-mid;
int L[n1], R[n2];
for(i=0;i<n1;i++)
L[i] = a[l+i];
for(j=0;j<n2;j++)
R[j] = a[mid+1+j];
i=0;
j=0;
k=l;
while(i < n1 && j < n2)
{
if(L[i] <= R[j])
{
a[k++] = L[i];
i++;
}
else if(R[i] <= L[j])
{
a[k++] = R[j];
j++;
}
}
while(i<n1)
{
a[k++] = L[i];
i++;
}
while(j<n2)
{
a[k++] = R[j];
j++;
}
}
void mergeSort(int a[],int l,int r)
{
if(l < r)
{
int mid;
mid = (l+(r-1))/2;
mergeSort(a,l,mid); // Once l < r is true it goes to the next function even though there is no return function written..
mergeSort(a,mid+1,r);
Merge(a,l,mid,r);
}
}
int main()
{
cout<<"Enter length of array: "<<endl;
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Elements before sorting: "<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
mergeSort(a,0,n-1);
cout<<endl;
cout<<"Elements after sorting: "<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
</code></pre>montycsMon, 25 Dec 2017 22:06:20 +0530https://discuss.codechef.com/questions/120241/help-in-merge-sort-algorithmrecursionHelp in Merge Sorthttps://discuss.codechef.com/questions/120774/help-in-merge-sort<p>I am trying to implement the Inversion Count problem for merge sort and have implemented it in c++ but I am not getting the desired output. Please help me find the bug in my program:</p>
<pre><code>#include <iostream>
using namespace std;
void mergeArr(int a[], int l, int mid, int r)
{
int n1, n2;
int i,j,k;
int inv_count = 0;
n1 = mid - l + 1;
n2 = r - mid;
int temp[r-l+1];
int L[n1], R[n2];
for(i = 0;i < n1;i++)
L[i] = a[l+i];
for(j = 0;j < n2;j++)
R[j] = a[mid+j+1];
i = j = 0;
k = l;
while(i<n1 && j<n2)
{
if(L[i] <= R[j])
temp[k++] = L[i++];
else
{
temp[k++] = R[j++];
inv_count += (mid - i);
}
}
while(i<n1)
temp[k++] = L[i++];
while(j<n2)
temp[k++] = R[j++];
for(i=l;i<=r;i++)
a[i] = temp[i];
}
void mergeSort(int a[], int l, int r)
{
int inv_count = 0;
if(l < r)
{
int mid;
mid = (l + (r))/2;
mergeSort(a,l,mid);
mergeSort(a,mid+1,r);
mergeArr(a,l,mid, r);
}
//return inv_count;
}
int main()
{
cout<<"Enter the number of elements"<<endl;
int n;
cin>>n;
int a[n];
cout<<"Enter the elements: "<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Array before sorting: "<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
mergeSort(a,0,n-1);
cout<<endl<<"Array after sorting: "<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
//cout<<endl<<"Inversions: "<<mergeSort(a,0,n-1);
return 0;
}
int main()
{
cout<<"Enter the number of elements"<<endl;
int n;
cin>>n;
int a[n];
cout<<"Enter the elements: "<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Array before sorting: "<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
/*mergeSort(a,0,n-1);
cout<<endl<<"Array after sorting: "<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";*/
cout<<"Inversions: "<<mergeSort(a,0,n-1);
return 0;
}
</code></pre>montycsMon, 01 Jan 2018 18:08:36 +0530https://discuss.codechef.com/questions/120774/help-in-merge-sortmergesortsortingJAN 18 Challenge: Maximum Scorehttps://discuss.codechef.com/questions/121001/jan-18-challenge-maximum-score<p>What does the constraint: 1 ≤ sum of N in all test-cases ≤ 3700 mean here?
Should I be adding anything while input or output?</p>montycsSun, 07 Jan 2018 20:30:13 +0530https://discuss.codechef.com/questions/121001/jan-18-challenge-maximum-scorehelpHelp with Puchi and Luggagehttps://discuss.codechef.com/questions/121063/help-with-puchi-and-luggage<p>I am trying to understand this problem: <a href="https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-problems/algorithm/puchi-and-luggage/editorial/">Puchi and luggage</a>. I understand that it is using Inversion Count of merge sort. But here, in author's solution, I don't understand why the Frequency array is updated twice where as it should be updated only once during merging..</p>montycsTue, 09 Jan 2018 22:09:53 +0530https://discuss.codechef.com/questions/121063/help-with-puchi-and-luggagemergesortWhich test case is my code failing?https://discuss.codechef.com/questions/121617/which-test-case-is-my-code-failing<p>I am solving <a href="https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/practice-problems/algorithm/prom-night/">Prom Night</a>. My code passes 3/5 test cases. I want to know the test case it is failing. My code:</p>
<pre><code>#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
bool flag = true;
cin>>m>>n;
int a[m];
int b[n];
for(int i=0;i<m;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cin>>b[i];
if(n<m)
{
cout<<"NO"<<endl;
}
else
{
sort(a,a+m);
sort(b,b+n);
for(int i=0;i<m;i++)
{
//cout<<"a[i]: "<<a[i]<<" b[i]"<<b[i]<<endl;
if(a[i] < b[i])
{
flag = false;
break;
}
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}
</code></pre>montycsThu, 18 Jan 2018 21:10:20 +0530https://discuss.codechef.com/questions/121617/which-test-case-is-my-code-failinghackerearthWhat is the meaning of this constraint?https://discuss.codechef.com/questions/122689/what-is-the-meaning-of-this-constraint<p>In the Chef and the Patents problem there is a constraint:
<strong>1 ≤ sum of K over all test cases ≤ 107</strong></p>
<p>What does this imply? </p>montycsSun, 11 Feb 2018 20:41:25 +0530https://discuss.codechef.com/questions/122689/what-is-the-meaning-of-this-constrainthelplong-contestCan you share your explanation of PERMPAL?https://discuss.codechef.com/questions/123379/can-you-share-your-explanation-of-permpal<p>I am having a tough time in trying to implement a solution for: <a href="https://www.codechef.com/problems/PERMPAL">PERMPAL</a>. Although, I do understand what the problem wants but am not able to come up with a solution. Tried reading editorials but it confused me. So, can you please share your approach or someone else's whom you found easy to understand and implement. </p>montycsMon, 19 Feb 2018 22:29:01 +0530https://discuss.codechef.com/questions/123379/can-you-share-your-explanation-of-permpalfeb18helpeditorialHow do I use Binary Search in this problem?https://discuss.codechef.com/questions/123407/how-do-i-use-binary-search-in-this-problem<p>The problem: <a href="https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/sherlock-and-numbers/">Sherlock and numbers</a>is tagged under binary search but I am really confused how binary search would be used here. Can anyone help me in understanding this problem with binary search point of view?</p>montycsTue, 20 Feb 2018 22:16:21 +0530https://discuss.codechef.com/questions/123407/how-do-i-use-binary-search-in-this-problemhackerearthPlease help in understanding this error?https://discuss.codechef.com/questions/123503/please-help-in-understanding-this-error<p>I am solving this <a href="http://train.usaco.org/usacoprob2?S=ride&a=gWaTTyglmAX">problem</a> from USACO but on submitting the answer, I get the following error: </p>
<pre><code> Run 1: Execution error: Your program had this runtime error:
Illegal file open (test.out). The program ran for 0.000 CPU
seconds before the error. It used 4300 KB of memory.
------ Data for Run 1 [length=14 bytes] ------
COMETQ
HVNGAT
----------------------------
</code></pre>
<p>Here is my solution:</p>
<pre><code> /*
ID: test1234@
TASK: ride
LANG: C++
*/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
ofstream fout ("test.out");
ifstream fin ("test.in");
string s1;
string s2;
cin>>s1;
cin>>s2;
int ans1,ans2;
ans1 = ans2 = 1;
for(int i=0;i<s1.length ();i++)
{
ans1 = ans1 * (s1[i] - 'A' + 1);
}
for(int i=0;i<s2.length ();i++)
{
ans2 = ans2 * (s2[i] - 'A' + 1);
}
if(ans1%47 == ans2%47)
cout<<"GO"<<endl;
else
cout<<"STAY"<<endl;
return 0;
}
</code></pre>montycsThu, 22 Feb 2018 22:31:42 +0530https://discuss.codechef.com/questions/123503/please-help-in-understanding-this-errorusacoNeed help in SPOJ 2DSORThttps://discuss.codechef.com/questions/123544/need-help-in-spoj-2dsort<p>I am trying to solve SORT2D, the approach I am thinking of is using a <code>vector<pair<int,int>> v</code> and then <code>sort(v.begin(),v.end()</code> but then how do I compare the y co-ordinate's position when x values are equal?</p>montycsFri, 23 Feb 2018 22:43:25 +0530https://discuss.codechef.com/questions/123544/need-help-in-spoj-2dsortspoj