codeparrot/codecomplex huggingface dataset:
// 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());
	}
	
	void solve() throws Exception {
		int n = nextInt();
		Integer[] a = new Integer[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		Integer[] b = a.clone();
		Arrays.sort(b);
		int d = 0;
		for (int i = 0; i < n; i++) {
			if (!a[i].equals(b[i])) d++;
		}
		out.println(d > 2? "NO" : "YES");
	}

	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 static java.util.Arrays.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import java.util.*;
import java.math.*;
import java.io.*;

public class A implements Runnable
{
    String file = "input";
    
    boolean TEST = System.getProperty("ONLINE_JUDGE") == null;
    
    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();
        sortInt(b);
        int count = 0;
        for(int i = 0; i < a.length; i++)
            if(a[i] != b[i]) count++;
        if(count == 0 || count == 2) out.println("YES");
        else out.println("NO");
    }
    
    Random rnd = new Random();
    
    void sortInt(int[] a)
    {
        sortInt(a, 0, a.length - 1);
    }
    
    void sortInt(int[] a, int from, int to)
    {
        if(from >= to) return;
        int i = from - 1;
        int p = rnd.nextInt(to - from + 1) + from;
        int t = a[p]; a[p] = a[to]; a[to] = t;
        for(int j = from; j < to; j++)
            if(a[j] <= a[to])
            {
                i++;
                t = a[i]; a[i] = a[j]; a[j] = t;
            }
        t = a[i + 1]; a[i + 1] = a[to]; a[to] = t;
        sortInt(a, i + 2, to);
        while(i >= 0 && a[i] == a[i + 1]) i--;
        sortInt(a, from, i);
        
    }
    
    String next() throws IOException
    {
        while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine());
        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());
    }
    
    void print(Object... o)
    {
        System.out.println(deepToString(o));
    }
    
    void gcj(Object o)
    {
        String s = String.valueOf(o);
        out.println("Case #" + test + ": " + s);
        System.out.println("Case #" + test + ": " + s);
    }
    
    BufferedReader input;
    PrintWriter out;
    StringTokenizer st;
    int test;
    
    void init() throws IOException
    {
        if(TEST) input = new BufferedReader(new FileReader(file + ".in")); 
        else input = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new BufferedOutputStream(System.out));
    }
    
    public static void main(String[] args) throws IOException
    {
        new Thread(null, new A(), "", 1 << 22).start();
    }
    
    public void run()
    {
        try
        {
            init();
            if(TEST) 
            {
                int runs = nextInt();
                for(int i = 0; i < runs; i++) solve();
            }
            else solve();
            out.close();        
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
// 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.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class A {

    static StreamTokenizer st;
    static class Sort implements Comparable<Sort> {
        int val;
        public int compareTo(Sort o) {
            return this.val-o.val;
        }
    }
    public static void main(String[] args) throws IOException{
        st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = nextInt();
        Sort[]a = new Sort[n+1];
        int[]b = new int[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = new Sort();
            a[i].val = nextInt();
            b[i] = a[i].val;
        }
        Arrays.sort(a, 1, n+1);
        int k1 = 0, k2 = 0;
        for (int i = 1; i <= n; i++) {
            if (b[i] != a[i].val) {
                if (k1==0)
                    k1 = i;
                else if (k2==0)
                    k2 = i;
                else {
                    System.out.println("NO");
                    return;
                }
            }
        }
        if (k1==0)
            System.out.println("YES");
        else if (k2==0)
            System.out.println("NO");
        else {
            if (b[k1]==a[k2].val && b[k2]==a[k1].val)
                System.out.println("YES");
            else
                System.out.println("NO");
        }
        pw.close();
    }

    private static int nextInt() throws IOException{
        st.nextToken();
        return (int) st.nval;
    }

}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

public class C {
	BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer st = null;
	PrintWriter out;

	public void solution() throws IOException {
		int n = nextInt();
		int a[] = new int[n];
		int b[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
			b[i] = a[i];
		}
		Arrays.sort(a);
		int ans = 0;
		for (int i = 0; i < n; i++) {
			if (a[i] != b[i]) {
				ans++;
			}
		}

		if (ans == 2 || ans == 0) {
			System.out.println("YES");
		} else {
			System.out.println("NO");
		}

	}

	public String nextToken() throws IOException {
		if (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(bf.readLine());
		}
		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());
	}

	public void print(int a[]) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
	}

	public static void main(String args[]) throws IOException {
		new C().solution();
	}
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.io.*;
import java.util.*;

public class A {

	final String filename = new String("C").toLowerCase();

	void solve() throws Exception {
		int n = nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		if (isSorted(a)) {
			out.println("YES");
			return;
		}
		int pos1 = -1;
		for (int i = 0; i < n - 1; i++) {
			if (a[i] > a[i + 1]) {
				int j = i;
				while (j >= 0 && a[j] == a[i]) {
					j--;
				}
				pos1 = j + 1;
				break;
			}

		}
		int pos2 = -1;
		for (int i = n - 2; i >= 0; i--) {
			if (a[i] > a[i + 1]) {
				int j = i + 1;
				while (j < n && a[j] == a[i + 1]) {
					j++;
				}
				pos2 = j - 1;
				break;
			}
		}
		int tmp = a[pos1];
		a[pos1] = a[pos2];
		a[pos2] = tmp;
		if (isSorted(a)) {
			out.println("YES");
		} else {
			out.println("NO");
		}
	}

	boolean isSorted(int[] a) {
		for (int i = 0; i < a.length - 1; i++) {
			if (a[i] > a[i + 1]) {
				return false;
			}
		}
		return true;
	}

	void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			// in = new BufferedReader(new FileReader("input.txt"));
			// out = new PrintWriter("output.txt");

			solve();

			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	BufferedReader in;
	StringTokenizer st;
	PrintWriter out;

	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());
	}

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

}
// 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.Random;
import java.util.StringTokenizer;

public class c {
    class IO {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer st;
        Random rnd = new Random();;
        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());
        }
    }
    
    void run() throws IOException{
        IO read=new IO();
        int n=read.nextInt();
        int a[]=new int[n],b[]=new int[n];
        for(int i=0;i<n;i++)
            a[i]=b[i]=read.nextInt();
        Arrays.sort(b);
        int cnt=0;
        for(int i=0;i<n;i++)
            if(a[i]!=b[i])
                cnt++;
        if(cnt==0||cnt==2)
            read.out.println("YES");
        else
            read.out.println("NO");
        read.out.close();
    }
    public static void main(String[] args) throws IOException {
        new c().run();
    }
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
//package round136;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;

public class A {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni();
		int[] a = new int[n];
		int[] b = new int[n];
		for(int i = 0;i < n;i++)b[i] = a[i] = ni();
		
		Arrays.sort(b);
		int ct = 0;
		for(int i = 0;i < n;i++){
			if(a[i] != b[i])ct++;
		}
		if(ct <= 2){
			out.println("YES");
		}else{
			out.println("NO");
		}
	}
	
	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}
	
	public static void main(String[] args) throws Exception
	{
		new A().run();
	}
	
	public int ni()
	{
		try {
			int num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}
			
			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}
	
	public long nl()
	{
		try {
			long num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}
			
			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}
	
	public String ns()
	{
		try{
			int b = 0;
			StringBuilder sb = new StringBuilder();
			while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
			if(b == -1)return "";
			sb.append((char)b);
			while(true){
				b = is.read();
				if(b == -1)return sb.toString();
				if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
				sb.append((char)b);
			}
		} catch (IOException e) {
		}
		return "";
	}
	
	public char[] ns(int n)
	{
		char[] buf = new char[n];
		try{
			int b = 0, p = 0;
			while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
			if(b == -1)return null;
			buf[p++] = (char)b;
			while(p < n){
				b = is.read();
				if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
				buf[p++] = (char)b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}
	
	
	double nd() { return Double.parseDouble(ns()); }
	boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.InputStream;

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

class TaskA {
	public void solve(int testNumber, InputReader in, OutputWriter out) {
        
        
        int n = in.nextInt();
        int a[] = in.nextIntArray(n);
        int i,j,k;

        int b[] = a.clone();
        ArrayUtils.randomShuffle(a);
        Arrays.sort(a);
        
        int c[] = new int[n];
        k=0;
        for(i=0;i<n;++i) if(a[i]!=b[i]) c[k++] = i;
        
        String res = "NO";
        if(k==0){
            res = "YES";
        }else
        if(k==1){
           
        }else
        if(k==2){
            i = c[0]; j = c[1];
            if(a[i]==b[j] && a[j]==b[i]) res = "YES";
        }
        
        out.writeln(res);
	}
}

class InputReader{
    private BufferedReader reader;
    private StringTokenizer tokenizer;
    
    public InputReader(InputStream stream){
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }
    
    public String next(){
        while(tokenizer==null || !tokenizer.hasMoreTokens()){
            try{
                tokenizer = new StringTokenizer(reader.readLine());
            }catch(Exception e){
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    
    public int nextInt(){
        return Integer.parseInt(next());
    }

    public int[] nextIntArray(int size){
        int array[] = new int[size];
        for(int i=0; i<size; ++i) array[i] = nextInt();
        return array;
    }
}

class OutputWriter{
    private PrintWriter out;
    
    public OutputWriter(Writer out){
        this.out = new PrintWriter(out);
    }

    public OutputWriter(OutputStream out){
        this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
    }
    
    public void close(){
        out.flush();
        out.close();
    }

    public void writeln(Object ... o){
        for(Object x : o) out.print(x);
        out.println();
    }
}

class ArrayUtils{
    private final static Random random = new Random(System.nanoTime());
    
    public static void randomShuffle(int a[]){
        int n = a.length;
        for(int i=0;i<n;++i){
            int j = random.nextInt(n-i);
            int t = a[i]; a[i] = a[j]; a[j] = t;
        }
    }
}
// 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 {

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    class Pointd implements Comparable<Pointd>{
        int x, in;

        @Override
        public int compareTo(Pointd o) {
            if(x > o.x) return 1;
            if(x < o.x) return -1;
            if(in < o.in) return -1;
            if(in > o.in) return 1;
            return 0;
        }

        public Pointd(int x, int in) {
            super();
            this.x = x;
            this.in = in;
        }
    }
    
    void solve() throws IOException {
        int n = readInt();
        Pointd[] a = new Pointd[n];
        for(int i = 0; i < n; i++){
            a[i] = new Pointd(readInt(), i);
        }
        Arrays.sort(a);
        
        int count = 0;
        for(int i = 0; i < n; i++){
            if(a[i].x != a[a[i].in].x) count++;
        }
        if(count == 0 || count == 2) out.println("YES");
        else out.println("NO");
    }
    

    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()) {
            tok = new StringTokenizer(in.readLine());
        }
        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());
    }

    int[] readArr(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = readInt();
        }
        return res;
    }

    long[] readArrL(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = readLong();
        }
        return res;
    }

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

    public void run() {
        try {
            long t1 = System.currentTimeMillis();
            init();
            solve();
            out.close();
            long t2 = System.currentTimeMillis();
            System.err.println("Time = " + (t2 - t1));
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }
}
// Problem: 220_A. Little Elephant and Problem
// Time complexity: nlogn
import java.util.*;
public class r220a {
	public static void main(String args[]) {
		Scanner in =new Scanner(System.in);
		int N = in.nextInt();
		ArrayList<Integer> list = new ArrayList<Integer>();
		ArrayList<Integer> sort = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			int k = in.nextInt();
			list.add(k);
			sort.add(k);
		}
		
		Collections.sort(sort);
		
		int count = 0;
		for(int i = 0; i < N; i++) {
			if(sort.get(i).intValue() != list.get(i).intValue())
				count++;
		}
		if(count != 2 && count != 0)
			System.out.println("NO");
		else
			System.out.println("YES");
	}
}