20th Feb 2021, 7:20 PM
Perplexed Coder
Perplexed Coder - avatar
8 Answers
+ 1
I don't get what you are doing in your code . and do you realise you are accessing negative index and array out of bound index ? you just need to do cin>>arr[i] b[arr[i]]++
20th Feb 2021, 7:39 PM
Hima
Hima - avatar
+ 1
Roy Kapoor I don't even know what you are trying to do at all. I showed yesterday exactly how to do it or just an idea of what arsenic was talking about . You should honestly go for small problems if you aren't ready for these yet . I know my solution would have been hard to understand but it's all about understanding what is asked in question and if you have basic understanding of language , nothing difficult when it comes to implementation. I see these two lines here, b[n]=0*n; } for(int i=0;i<a[n];i++) what is "0*n" and "i<a[n]" ??? Also did you realised in "b[n]" n never changes .
20th Feb 2021, 7:58 PM
Abhay
Abhay - avatar
+ 1
// simplified + functional version: #include <iostream> using namespace std; int solve(int n) { int i, x, b[n], f = 1; for (i=0; i<n; ++i) b[i] = 0; for (i=0; i<2*n; ++i) { cin >> x; if (0<x && x<=n && ++b[--x]<3) continue; f = 0; break; } return f; } int main() { int t, n; cin >> t; while (t--) { cin >> n; n = solve(n); cout << (n ? "Yes" : "No ") << endl; } }
20th Feb 2021, 8:40 PM
visph
visph - avatar
+ 1
Roy Kapoor arr1=[0]*n int arr1[n]={} // C++ version and you have got it all wrong. Do you know how a for loop in python works ? You have just put the same thing in cpp but that's now how you do it .
21st Feb 2021, 1:20 AM
Hima
Hima - avatar
+ 1
Oh, you're right... fixed version here: https://code.sololearn.com/cdjhEm9h5NOE/?ref=app
21st Feb 2021, 4:25 AM
visph
visph - avatar
0
Actually I made it in py..and was trying to make it in cpp.. t = int(input()) while t: n = int(input()) arr = list(map(int, input().split())) arr1 = [0]*n for i in arr: arr1[i-1] += 1 for i in arr1: if i is not 2: print('NO') break else: print('YES') t -= 1
20th Feb 2021, 7:58 PM
Perplexed Coder
Perplexed Coder - avatar
0
Thanks Abhay,Hima
21st Feb 2021, 3:57 AM
Perplexed Coder
Perplexed Coder - avatar
0
Visph your code is perfect but fails for testcases above 3 2 3 1 2 3 2 1 3 4 1 2 3 2 1 2 3 4 2 1 1 2 3 1 1 1 Output: YES NO NO NO
21st Feb 2021, 3:57 AM
Perplexed Coder
Perplexed Coder - avatar