Decent Arrays


This is a companion discussion topic for the original entry at https://toph.co/p/decent-arrays
#include<stdio.h>
int main()
{
    int n,i;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }

    for(i=0;i<n;i++){
        if(a[i]>a[i+1]){
            printf("No");
            break;
        }
    }
    if(i==n)
        printf("Yes");

    return 0;
}

what is the problem in this code that it is showing wa?

check this
5
1 2 2 3 4

#include<bits/stdc++.h>
using namespace std;



int main()
{
   int n;
   cin>>n;
   int ar[n];
   for(int i =0 ;i<n;i++)
    cin>>ar[i];

    bool shit = false;
 for(int i =0 ;i<n;i++)
 {
     if(ar[i]<=ar[i+1])
        continue;
     else
     {
        shit  = true;
        break;
     }

 }
 if(shit)
 {
     cout<<"No"<<endl;
 }
 else if(n!=0)
    cout<<"Yes"<<endl;

}

Whats Problem in my code??

@Rabeya The problem is in line 11-12 in your code. change,

for(i=0;i<n;i++){
    if(a[i]>a[i+1]){

with

for(i=1;i<n;i++){
    if(a[i-1]>a[i]){

The problem occurs when i becomes n -1 , anything like a[i+1] doesn’t exist. cause i +1 = n - 1 + 1 = n. and calling a[n] will ofcousre will show an error cause the index you are refering to is out of the array range. Therefore, I have change those lines such that this thing never occurs and your code should be accepted.

It is actually funny. You have done the same mistake as @Rabeya In your case the problem is in line 13-15. Read what I have written above and your problem should be solved.

And of course, @STS_12 and @adnan99, welcome to the Toph Community.

is there anyone in python,only one test case is not approving,but why?
N=int(input())
bul=False
if 0 < N < 100:
a=1
A=input()
A1=A.split()
for x in A1:
x=int(x)
if 1<=x<=1000 and x>=a:
a=x
bul=True

    else:
        bul=False
if bul:
    print("Yes")
else:
    print("No")

@amir.ahmed, many uses python.

There are many things in your code that are unnecessary. For example, lines 3-4. You don’t need to test if there the input size is 1 hundred or 1 million. Many people do it but it is unnecessary.

However, the problem is not in lines 3-4 but in line 8. You have already used x in the loop so use another variable. I will change the loop variable with i traditionally. However It doesn’t matter in your case. And also, in line 4, The value of the first number will not always be 1 so you have to set it first. and also checking if 1 <= x <= 1000 is also unnecessary. Your code with my edits:

input()
A = input()
A1 = A.split()
a = int(A1[0])
for i in A1:
	x = int(i)
	if x >= a:
		a = x
		bul = True
	else:
		bul = False
		break
if bul:
    print("Yes")
else:
    print("No")

See my submission using your code here.
See how I have initialized a in line 4.

Actually, the real problem in your code is not any of the things I have mentioned above. Simply putting the break statement would do. Everything else was right in case of this problem. However, you should keep in mind the things I have mentioned above in the future.

Your code with just the break statement included can be found here.

But why only adding break fixes it? Find it out yourself!!!

package javaapplication29;

import java.util.Scanner;

public class solution {

    public static void main(String[] args) {
       Scanner sc= new Scanner (System.in);
       int n=sc.nextInt();
       boolean x=true;
       int N[]= new int[n];
       int i=0;
       while(i<n){
           N[i]=sc.nextInt();
           i++;
       }
       i=0;
       while(i<n-1){
           if(N[i]<N[i+1]){
               x=true;
           }
           else{
               x=false;
               break;
           }
           i++;
       }
       if(x==true) System.out.println("Yes");
       else if(x==false)  System.out.println("No");
    }
    
}

fault of this code?

ignore 1st line but yet in 4th test case it shows wrong ans
why?

updated code but still same problem

import java.util.Scanner;

public class solution {

    public static void main(String[] args) {
       Scanner sc= new Scanner (System.in);
       int n=sc.nextInt();
       boolean x=true;
       int N[]= new int[n];
       int i=0;
       while(i<n){
           N[i]=sc.nextInt();
           i++;
       }
       i=0;
       while(i<n-1){
           if(N[i]<N[i+1]){
               x=true;
           }
           else if(N[i+1]>N[i]){
           x=true;
           }
           else{
               x=false;
               break;
           }
           i++;
       }
       if(x==true) System.out.println("Yes");
       else if(x==false)  System.out.println("No");
    }
    
}

@ph3nixsabit
The problem is when N[i] == N[i+1], your code will fail.

What’s the problem in my code???

#include<stdio.h>
#include<math.h>
#include<string.h>
/*
Author: Md Alhaz Mondal.
Institute: Faridpur Polytechnic Institute.
Faridpur,Dhaka,Bangladesh.
*/
int main()
{
int T,i=0,j=0,n,s=0;
scanf("%d",&T);
if((0<T)&&(T<100))
{
int ar[T];
while(i<T)
{
scanf("%d",&n);
ar[i]=n;
i++;
}
while(j<T)
{
if((ar[j]>ar[j+1])||(ar[j]==ar[j+1]))
{
s=1;
}
j++;
}
if(s==0)
{
printf(“Yes”);
}
else
{
printf(“No”);
}
}
return 0;
}

@hredhayxz, follow this to mark up your codes correctly. I will look at this when I get your notification.

#include<stdio.h>
int main()
{
int n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

for(i=0;i<n-1;i++){
    if(a[i]>a[i+1]){
        printf("No\n");
        return 0;
    }
}
    printf("Yes\n");

return 0;

}
accepted in your perspective

Please don’t share accepted codes in the community. You should point out the mistakes so that one can figure out the solution on their own.

And please format your codes properly before sharing them in the community. You can use this post as a reference.

ohh ok … sorry. bro.

Not to worry, I just pointing out the community rules since you seemed to be new here.
happy coding!!!

The Problem is Simple. You are taking a list of strings as input but trying to compare them as numbers.
Even in Python strings can also be compared the same way as numbers, it doesn’t mean the comparison between numbers and their equivalent string expressions will be the same.

Simply, even your code runs successfully, your submission won’t be accepted. You have to convert the strings of the list to numbers first before comparing them.

And again, Please remove the link of the accepted solution you have posted here. Although not punishable, it is usually discouraged by the moderators of Toph.

This is another poorly defined problem. It should ask to check for either strictly ascending or non-descending values. “Ascending” can mean either of these two things. Or perhaps it is asking for something else? Should [1,1,1,1,1,1,1,1] be considered “ascending”?

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, i;
cin>>n;
if(n==1) cout<<“Yes”<<endl;
else
{
char a[n];
for(i=0; i<n;i++)
cin>>a[i];
for(i=1; i<n; i++)
{

    if(a[i-1]>a[i])
    {
        cout<<"No"<<endl;
        break;
    }
}
if(i==n) cout<<"Yes"<<endl;
}

}

where is the problem in my code?