C algorithm problem flip array

Algorithm problem flip array

Given a n integer array a with length N, the elements are different. Ask if there is such a fragment in the array. Only turning the fragment over can make the whole array in ascending order. Where array fragment [l,r] represents sequence a[l], a[l+1],   a[r]. The original array is
a[1], a[2], ..., a[l-2], a[l-1], a[l], a[l+1], ..., a[r-1], a[r], a[r+1], a[r+2], ..., a[n-1], a[n],
The array after reversing the fragment [l,r] is
a[1], a[2], ..., a[l-2], a[l-1], a[r], a[r-1], ..., a[l+1], a[l], a[r+1], a[r+2], ..., a[n-1], a[n].

input sample input
The first row of data is an integer: n (1 ≤ n ≤ 105), indicating the array length. 4
The second row of data is n integers a[1], a[2] , a[n] (1≤a[i]≤109). 2 1 3 4
output sample output
Output "yes" if it exists; otherwise, output "no" without quotation marks. yes
using System;


namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = Convert.ToInt32(Console.ReadLine());
            string input = Console.ReadLine();
            string[] temp = input.Split(' ');
            int[] num = new int[count];
            bool output = false;
            for (int i = 0; i < count; i++)
            {
                num[i] = Convert.ToInt32(temp[i]);
            }
            //Pick array fragment

            for (int i = 0; i < count; i++)
            {
                for (int  k = count; i < k; k--)
                {

                    int[] num3 = new int[count];
                    for (int j = 0; j < count; j++)
                        num3[j] = num[j];

                    Array.Reverse(num3,i,k-i);



                    bool flag = true;
                    for (int j = 0; j < count-1; j++)
                    {
                        if (num3[j] > num3[j + 1])
                        {
                            flag = false;
                            break;
                        }

                    }
                    if (flag) output = true;

                }
            }

            if (output) Console.WriteLine("yes");
            else Console.WriteLine("no");

            Console.ReadLine();
        }
    }
}

Tags: Fragment

Posted on Sun, 05 Jan 2020 22:30:07 -0800 by metazai