codeparrot/codecomplex huggingface dataset:
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.io.*;
import java.util.*;

public class Solution {


	private StringTokenizer st;
	private BufferedReader in;
	private PrintWriter out;

	public void solve() throws IOException {
		int n = nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; ++i) {
			a[i] = nextInt();
		}
		int[] b = a.clone();
		Arrays.sort(b);
		int diff = 0;
		for (int i = 0; i < n; ++i) {
			if (a[i] != b[i]) {
				diff++;
			}
		}
		out.println(diff <= 2 ? "YES" : "NO");
	}

	public void run() throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		eat("");
		
		solve();
		
		out.close();
	}
	
	void eat(String s) {
		st = new StringTokenizer(s);
	}
	
	String next() throws IOException {
		while (!st.hasMoreTokens()) {
			String line = in.readLine();
			if (line == null) {
				return null;
			}
			eat(line);
		}
		return st.nextToken();
	}
	
	int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	
	long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	
	double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	
	public static void main(String[] args) throws IOException {
		Locale.setDefault(Locale.US);
		new Solution().run();
	}
	
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn


import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

import static java.lang.Math.*;

public class A implements Runnable{
	
	final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
	
	BufferedReader in;
	PrintWriter out;
	StringTokenizer tok = new StringTokenizer("");
	
	void init() throws FileNotFoundException{
		if (ONLINE_JUDGE){
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
		}else{
			in = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter("output.txt");
		}
	}
	
	String readString() throws IOException{
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(in.readLine());
			}catch (Exception e){
				return null;
			}
		}
		return tok.nextToken();
	}
	
	int readInt() throws IOException{
		return Integer.parseInt(readString());
	}
	
	long readLong() throws IOException{
		return Long.parseLong(readString());
	}
	
	double readDouble() throws IOException{
		return Double.parseDouble(readString());
	}
	
	public static void main(String[] args){
		new Thread(null, new A(), "", 128 * (1L << 20)).start();
	}
	
	long timeBegin, timeEnd;

	void time(){
		timeEnd = System.currentTimeMillis();
		System.err.println("Time = " + (timeEnd - timeBegin));
	}
	
	long memoryTotal, memoryFree;
	

	void memory(){
		memoryFree = Runtime.getRuntime().freeMemory();
		System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB");
	}
	
	void debug(Object... objects){
		if (DEBUG){
			for (Object o: objects){
				System.err.println(o.toString());
			}
		}
	}
	
	public void run(){
		try{
			timeBegin = System.currentTimeMillis();
			memoryTotal = Runtime.getRuntime().freeMemory();
			init();
			solve();
			out.close();
			time();
			memory();
		}catch (Exception e){
			e.printStackTrace(System.err);
			System.exit(-1);
		}
	}
	
	boolean DEBUG = false;
	
	void solve() throws IOException{
		int n = readInt();
		
		int[] a = new int[n];
		Integer[] b = new Integer[n];
		for (int i = 0; i < n; ++i){
			a[i] = readInt();
			b[i] = a[i];
		}
		
		Arrays.sort(b);
		int count = 0;
		for (int i = 0; i < n; ++i){
			if (a[i] != b[i]){
				count++;
			}
		}
		
		if (count == 2 || count == 0){
			out.println("YES");
		}else{
			out.println("NO");
		}
	}
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;


public class Main {

    
    public static void main(String[] args) {
        Scanner in=new Scanner(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        int n=in.nextInt();
        Vector<Integer> mas=new Vector<Integer>();
        Vector<Integer> mas2=new Vector<Integer>();
        int index=-1;
        boolean res=false;
        for(int i=0; i<n; i++){
            mas.add(in.nextInt());
            if(i!=0 && mas.get(i)<mas.get(i-1)){
                index=i-1;
                break;
            }
        }
        if(index==-1) res=true;
        else{
            int min=mas.get(index+1);
            int minIndex=index+1;
            for(int i=index+2; i<n; i++){
                mas.add(in.nextInt());
                if(mas.get(i)<=min){
                    min=mas.get(i);
                    minIndex=i;
                    
                }
            }
            
            mas2.addAll(mas);
            mas.set(minIndex, mas.get(index));
            mas.set(index, min);
            int o=mas.hashCode();
            Collections.sort(mas);
            int nw=mas.hashCode();
            res=nw==o;
        }
        if(!res){
            mas=mas2;
            for(int i=n-1; i>=0; i--){
                if(i!=n-1 && mas.get(i)>mas.get(i+1)){
                    index=i+1;
                    break;
                }
            }
            if(index==-1) res=true;
            else{
                int max=mas.get(index-1);
                int maxIndex=index-1;
                for(int i=index-1; i>=0; i--){
                    if(mas.get(i)>=max){
                        max=mas.get(i);
                        maxIndex=i;
                    }
                }
                mas.set(maxIndex, mas.get(index));
                mas.set(index, max);
                int o=mas.hashCode();
                Collections.sort(mas);
                int nw=mas.hashCode();
                res=res||nw==o;
            }
        }
        if(res) out.println("YES");
        else out.println("NO");
        out.close();
    }

}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;


public class Main{

    public static void main(String[] args) throws Exception {
        Parserdoubt12 s = new Parserdoubt12(System.in);
        
        int n = s.nextInt();
        
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }
        
        int copy[] = a.clone();
        Arrays.sort(a);
        int count = 0;
        for (int i = 0; i < copy.length; i++) {
            if(a[i] != copy[i]) count++;
        }
        if(count <= 2) System.out.println("YES");
        else System.out.println("NO");
    }
    
    
}


class Parserdoubt12
{
   final private int BUFFER_SIZE = 1 << 17;
 
   private DataInputStream din;
   private byte[] buffer;
   private int bufferPointer, bytesRead;
 
   public Parserdoubt12(InputStream in)
   {
      din = new DataInputStream(in);
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
   }
   public String nextString() throws Exception
   {
       StringBuffer sb=new StringBuffer("");
       byte c = read();
       while (c <= ' ') c = read();
       do
       {
           sb.append((char)c);
           c=read();
       }while(c>' ');
       return sb.toString();
   }
   public char nextChar() throws Exception
   {
       byte c=read();
       while(c<=' ') c= read();
       return (char)c;
   }
   public int nextInt() throws Exception
   {
      int ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      boolean neg = c == '-';
      if (neg) c = read();
      do
      {
          ret = ret * 10 + c - '0';
         c = read();
      } while (c > ' ');
      if (neg) return -ret;
      return ret;
   }
   public long nextLong() throws Exception
   {
      long ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      boolean neg = c == '-';
      if (neg) c = read();
      do
      {
          ret = ret * 10 + c - '0';
         c = read();
      } while (c > ' ');
      if (neg) return -ret;
      return ret;
   }
   private void fillBuffer() throws Exception
   {
      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
      if (bytesRead == -1) buffer[0] = -1;
   }
 
   private byte read() throws Exception
   {
      if (bufferPointer == bytesRead) fillBuffer();
      return buffer[bufferPointer++];
   }
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.util.*;

public class Main {

	public static void main(String[] args) {

		// long start = System.currentTimeMillis();
		// long end = System.currentTimeMillis();
		// System.out.println(" Execution time was "+(end-start)+" ms.");

		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt();
		int a[] = new int[n];
		int b[] = new int[n];
		for(int i = 0;i<n;i++){
			a[i]=kb.nextInt();
			b[i]=a[i];
		}
		Arrays.sort(a);
		int count = 0;
		for(int i=0;i<n;i++){
			if(a[i]!=b[i])count++;
		}
		if(count<=2)
			System.out.println("YES");
		else
			System.out.println("NO");

	}

}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.io.*;
import java.util.*;
import java.math.*;

public class A {
	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer st;
	static Random  rnd;

	void solve() throws IOException {
		int n = nextInt();

		int[] arr = new int[n];
		Integer[] arrCopy = new Integer[n];

		for (int i = 0; i < n; i++)
			arr[i] = arrCopy[i] = nextInt();

		Arrays.sort(arrCopy);

		int bad = 0;

		for (int i = 0; i < n; i++)
			if (arr[i] != arrCopy[i])
				++bad;

		boolean fail = bad > 2;

		out.println(!fail ? "YES" : "NO");
	}

	public static void main(String[] args) {
		new A().run();
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);

			rnd = new Random();

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(42);
		}
	}

	String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String line = in.readLine();

			if (line == null)
				return null;

			st = new StringTokenizer(line);
		}

		return st.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.regex.*;
import static java.lang.Math.*;
public class A {

    public A() throws Exception {
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i=0; i<n; i++) arr[i] = in.nextInt();
        int[] arr2 = arr.clone();
        Arrays.sort(arr2);
        int diff = 0;
        for (int i=0; i<n; i++) {
            if (arr2[i]!=arr[i]) diff++;
        }
        if (diff<=2) System.out.println("YES");
        else System.out.println("NO"); 
    }

    Scanner in = new Scanner(System.in);
    StringBuilder buf = new StringBuilder();
    public static void main(String[] args) throws Exception { // {{{
        new A();
    } // }}}
    public static void debug(Object... arr) { // {{{
        System.err.println(Arrays.deepToString(arr));
    } // }}}
    public static class Scanner { // {{{
        BufferedReader br;
        String line;
        StringTokenizer st;
        public Scanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }
        public boolean hasNext() throws IOException {
            while ((st==null||!st.hasMoreTokens())&&(line=br.readLine())!=null)
                st = new StringTokenizer(line);
            return st.hasMoreTokens();
        }
        public String next() throws IOException {
            if (hasNext()) return st.nextToken();
            throw new NoSuchElementException();
        }
        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }
        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    } // }}}
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Solution implements Runnable {

	BufferedReader in;
	PrintWriter out;
	StringTokenizer st;

	String nextToken() throws Exception {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(in.readLine());
		}
		return st.nextToken();
	}

	int nextInt() throws Exception {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws Exception {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws Exception {
		return Double.parseDouble(nextToken());
	}
	
	int fu(int[] a, int l) {
		for (int i = l; i < a.length - 1; i++) {
			if (a[i] > a[i + 1]) return i;
		}
		return a.length;
	}
	
	void swap(int[] a, int q, int w) {
		int t = a[q]; a[q] = a[w]; a[w] = t;
	}
	
	void solve() throws Exception {
		int n = nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) a[i] = nextInt();
		int q = fu(a, 0);
		if (q == n) out.println("YES"); else {
			int w = fu(a, q + 1);
			if (w < n) {
				boolean ans = false;
				swap(a, q, w);
				ans |= fu(a, 0) == n;
				swap(a, q, w);
				if (q < n - 1) {
					swap(a, q + 1, w);
					ans |= fu(a, 0) == n;
					swap(a, q + 1, w);
				}
				if (w < n - 1) {
					swap(a, q, w + 1);
					ans |= fu(a, 0) == n;
					swap(a, q, w + 1);
				}
				if (q < n - 1 && w < n - 1) {
					swap(a, q + 1, w + 1);
					ans |= fu(a, 0) == n;
					swap(a, q + 1, w + 1);
				}
				if (ans) out.println("YES"); else out.println("NO");
			} else {
				int j = q + 1;
				while (j < n && a[j] == a[q + 1]) j++;
				j--;
				swap(a, q, j);
				if (fu(a, 0) == n) out.println("YES"); else {
					swap(a, q, j);
					q++;
					j = q - 1;
					while (j >= 0 && a[j] == a[q - 1]) j--;
					j++;
					swap(a, q, j);
					if (fu(a, 0) == n) out.println("YES"); else out.println("NO");
				}
			}
		}
	}

	public void run() {
		try {
			Locale.setDefault(Locale.UK);
			in = new BufferedReader(new InputStreamReader(System.in));
			// in = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter(System.out);
			// out = new PrintWriter("output.txt");
			solve();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) {
		// new Thread(null, new Solution(), "1", 1 << 28).start();
		(new Solution()).run();
	}

}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author niyaznigmatul 
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		FastPrinter out = new FastPrinter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
	public void solve(int testNumber, FastScanner in, FastPrinter out) {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        int[] b = a.clone();
        ArrayUtils.sort(b);
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] != b[i]) {
                ++count;
            }
        }
        out.println(count <= 2 ? "YES" : "NO");
	}
}

class FastScanner extends BufferedReader {

    boolean isEOF;

    public FastScanner(InputStream is) {
        super(new InputStreamReader(is));
    }

    public int read() {
        try {
            int ret = super.read();
            if (isEOF && ret < 0) {
                throw new InputMismatchException();
            }
            isEOF = ret == -1;
            return ret;
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    static boolean isWhiteSpace(int c) {
        return c >= -1 && c <= 32;
    }

    public int nextInt() {
        int c = read();
        while (isWhiteSpace(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int ret = 0;
        while (!isWhiteSpace(c)) {
            if (c < '0' || c > '9') {
                throw new NumberFormatException("digit expected " + (char) c
                        + " found");
            }
            ret = ret * 10 + c - '0';
            c = read();
        }
        return ret * sgn;
    }

    }

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

    public FastPrinter(Writer out) {
        super(out);
    }


}

class ArrayUtils {

    public static void sort(int[] a) {
        Random rand = new Random(System.nanoTime());
        for (int i = 0; i < a.length; i++) {
            int j = rand.nextInt(i + 1);
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
        Arrays.sort(a);
    }


    }
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.geom.Line2D;
import java.awt.Point;
import static java.lang.reflect.Array.*;

public class LittleElephantAndProblem {

    boolean DEBUG = true;
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringBuilder out = new StringBuilder();
    StringTokenizer st = null;

    String s() throws IOException {
        if (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    int i() throws IOException {
        return Integer.parseInt(s());
    }

    int i(String s) throws IOException {
        return Integer.parseInt(s);
    }

    long l() throws IOException {
        return Long.parseLong(s());
    }

    long l(String s) throws IOException {
        return Long.parseLong(s);
    }

    double d() throws IOException {
        return Double.parseDouble(s());
    }

    double d(String s) throws IOException {
        return Double.parseDouble(s);
    }

    void D(Object a) {
        if (DEBUG) {
            int len = getLength(a);
            for (int i = 0; i < len; ++i) {
                System.out.print(get(a, i) + " ");
            }
            System.out.println();
        }
    }

    void D(Object[] a) {
        if (DEBUG) {
            int R = getLength(a), C = getLength(get(a, 0));
            for (int i = 0; i < R; ++i) {
                for (int j = 0; j < C; ++j) {
                    System.out.print(get(get(a, i), j) + " ");
                }
                System.out.println();
            }
        }
    }

    void D(String args) {
        if (DEBUG) {
            System.out.print(args);
        }
    }

    void D(String format, Object... args) {
        if (DEBUG) {
            System.out.printf(format, args);
        }
    }

    void fl() {
        System.out.print(out);
    }
    int n = i();

    public LittleElephantAndProblem() throws IOException {
        List<Integer> a = new ArrayList<Integer>();
        List<Integer> b = new ArrayList<Integer>();
        for (int i = 0; i < n; ++i) {
            int x = i();
            a.add(x);
            b.add(x);
        }
        sort(b);
        int d = 0;
        for (int i = 0; i < n; ++i) {
            if ((int)a.get(i) != (int)b.get(i)) {
                ++d;
            }
        }
        if (d > 2) {
            out.append("NO\n");
        } else {
            out.append("YES\n");
        }
        fl();
    }

    public static void main(String[] args) throws IOException {
        new LittleElephantAndProblem();
    }
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;

public class A {

	private void solve() throws IOException {
		int n = nextInt();
		Integer[] a = new Integer[n];
		Integer[] b = new Integer[n];
		
		for (int i = 0; i < n; i++) {
			a[i] = b[i] = nextInt();
		}
		
		Arrays.sort(a);
		int k = 0;
		for (int i = 0; i < n; i++) {
			if (!a[i].equals(b[i])) {
				k++;
			}
		}
		
		if (k <= 2) {
			println("YES");
		} else {
			println("NO");
		}
	}

	private String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	private int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	private double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}

	private long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	private void print(Object o) {
		writer.print(o);
	}

	private void println(Object o) {
		writer.println(o);
	}

	private void printf(String format, Object... o) {
		writer.printf(format, o);
	}

	public static void main(String[] args) {
		long time = System.currentTimeMillis();
		Locale.setDefault(Locale.US);
		new A().run();
		System.err.printf("%.3f\n", 1e-3 * (System.currentTimeMillis() - time));
	}

	BufferedReader reader;
	StringTokenizer tokenizer;
	PrintWriter writer;

	private void run() {
		try {
			reader = new BufferedReader(new InputStreamReader(System.in));
			writer = new PrintWriter(System.out);
			solve();
			reader.close();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(13);
		}
	}
}