1,3,5,4,2
can somebody tell me what is the Length of LIS in this, and what elements will you take.???
if its 3 - (1,3,5/4) & (2,4,5)
then why in the hint it is written only one valid permutation
1,3,5,4,2
can somebody tell me what is the Length of LIS in this, and what elements will you take.???
if its 3 - (1,3,5/4) & (2,4,5)
then why in the hint it is written only one valid permutation
For even values we can handle for 4 separately and for rest we can -
Take an array insert values from 1 to n/2 in increasing order and values from n/2+1 to n in decreasing order and swap first and last values.
for eg -
For 6
Array after performing above operations - 4 2 3 6 5 1
And the resultant array is our answer.
it didn’t say “only” but “one of the valid permutations”.
1
9
YES
1 3 5 7 9 8 6 4 2
when n is odd this output is correct according to me…but why then my submission is wrong?
can anyone pls look at my code…
it is passing all the cases but one (ie) 6th case … i cant wrap my head around it
void solve ( ){
ll n; cin>>n;
vector<ll> ans;
if ( n<=2){
cout<<"NO";
return;
}
else cout<<"YES\n";
//cout<<1<<" ";
ans.push_back(1);
if(n%2==0){
for(ll i = n-1; i >n/2;i--)
{
ans.push_back(i);
//cout<<i<<" ";
}
ans.push_back(n);
for(ll i = 2;i<=(n)/2 ; i++) ans.push_back(i);
}
else{
for(ll i =n; i >(n+1)/2;i--)
{
ans.push_back(i);
//cout<<i<<" ";
}
for(ll i = 2;i<=(n+1)/2 ; i++) ans.push_back(i);
}
for(ll i = 0; i<n;i++) cout<<ans[i]<<" ";
}```
#include<bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n;
cin>>n;
if(n%2){
cout << “YES” << endl;
for(int i = 1; i <= (n/2); i++){
cout << i << " ";
}
cout << n << " ";
for(int i = n-1; i > (n/2); i–){
cout << i << " ";
}
cout << endl;
}
else if(n != 2){
cout << “YES” << endl;
cout << (n/2) << " ";
for(int i = 1; i < (n/2); i++){
cout << i << " ";
}
for(int i = n; i > (n/2); i–){
cout << i << " ";
}
cout << endl;
}
else{
cout << “NO” << endl;
}
}
return 0;
}
Can somebody please tell me the problem in my code, I intended to make a pattern like this-
For Even(6) -{1,6,2,5,3,4}
For Odd(5) - {1,3,5,4,2}
Code for the above implementation-
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define ulli unsigned long long int
#define lli long long int
#define ll long long
#define dd double
#define endl "\n"
#define mod 1000000007
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define limit 1000000000000000000
using namespace std;
int main()
{
fast
lli t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
if(n==2){
cout<<"NO"<<endl;
}
else{
cout<<"YES"<<endl;
vector<ll> v(n);
if(n&1){
v[n/2]=n;
ll count=1;
for(ll i=0;i<n/2;i++){
v[i]=count;
count+=2; }
for(ll i=(n/2)+1;i<n;i++){
v[i]=v[(n-i)-1]+1;
}
}
else{
ll count=1;
for(ll i=0;i<n;i=i+2){
v[i]=count;
count++;
}
count=n;
for(ll i=1;i<n;i=i+2){
v[i]=count;
count--;
}
}
for(ll i=0;i<n;i++){
cout<<v[i]<<" ";
}
cout<<endl;
}
}
return 0;
}
What’s wrong with my approach?
can anyone please tell me whats wrong in my code??
for N=5, it gives 1 2 5 4 3
and N=6, it gives 1 2 5 4 3 6
cin>>t;
while(t–)
{
cin>>n;
if(n % 2)
{
int i = 1;
x = n/2;
cout<<"YES"<<endl;
for(i = 1; i <= x; i++)
{
cout<<i<<" ";
}
for(int j = n; j >= i; j--)
{
cout<<j<<" ";
}
}
else if(n != 2)
{
int i = 1;
x = (n-1)/2;
cout<<"YES"<<endl;
for(i = 1; i <= x; i++)
{
cout<<i<<" ";
}
for(int j = n - 1; j >= i; j--)
{
cout<<j<<" ";
}
cout<<n;
}
else
{
cout<<"NO";
}
cout<<endl;
}
how is it wrong??
same here man , life sucks
you might be thinking 2 3 and 4 5 6 , which are not equal in length , but that is when it were asked sub array ,
in the question it Largest inc. subsequence
which gives us 2 3 6 and 4 5 6 which are equal in length
For even why cant we do this, n=6 , 123654
if you get it, why your code is wrong, do you bother explaining it.? your code seems good to me.
For N=6,
Your code output = (1 4 2 5 3 6)
LIS for this is (1236), Length = 4.
In reverse order 6 3 5 2 4 1
LIS = 35 or 24 , Length = 2
Hence Its Incorrect
for N= 6, lis is 4 (1, 2, 3, 6) but in reverse its 3 (3, 4, 5)
Can someone tell why this approach is wrong?
#include <iostream>
using namespace std;
int main() {
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
if(n==1)
{
cout<<"Yes\n";
cout<<"2\n";
}
else if(n==2)
{
cout<<"No\n";
}
else
{
cout<<"Yes\n";
if(n%2 == 0)
{
long long int k = n;
for(long long int i=1;i<=n;i++)
{
if(i%2 != 0)
cout<<i<<" ";
else
{
cout<<k<<" ";
k -= 2;
}
}
}
else
{
for(long long int i=1;i<=n;i++)
{
if(i%2 != 0)
cout<<i<<" ";
}
for(long long int i=n;i>=1;i--)
{
if(i%2 == 0)
cout<<i<<" ";
}
}
cout<<"\n";
}
}
return 0;
}
Same I did
Someone please help me!!
I’m not able to understand, what’s wrong with my logic
ll n,i,j,k;
cin>>n;
if(n==2)
{
cout<<"NO"<<nl;
return;
}
if(n%2==0)
{
cout<<"YES"<<nl;
for(i=1;i<=n-1;i+=2)
cout<<i<<" ";
for(i=n;i>=2;i-=2)
cout<<i<<" ";
cout<<endl;
}
else
{
cout<<"YES"<<nl;
for(i=1;i<=n;i+=2)
cout<<i<<" ";
for(i=n-1;i>=2;i-=2)
cout<<i<<" ";
cout<<endl;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
long long int n;
cin>>n;
if(n==2){
cout<<“NO”<<endl;
continue;
}
if(n%2==0 && n>2){
cout<<“YES”<<endl;
long long int arr1[n];
arr1[1]=n;
for(int i=2;i<=n/2;i++){
arr1[i]=i-1;
}
arr1[(n/2)+1]=n-1;
long long int a=n-1;
for(int i=(n/2)+2;i<=n;i++){
a–;
arr1[i]=a;
}
for(int i=1;i<=n;i++){
cout<<arr1[i]<<" “;
}
cout<<endl;
continue;
}
cout<<“YES”<<endl;
long long int arr2[n];
for(int i=1;i<=n/2;i++){
arr2[i]=i;
}
arr2[(n/2)+1]=n;
long long int b=n;
for(int i=(n/2)+2;i<=n;i++){
b–;
arr2[i]=b;
}
for(int i=1;i<=n;i++){
cout<<arr2[i]<<” ";
}
cout<<endl;
}
return 0;
}
//can anyone tell me why this is wrong. My logic seems to be right