Write a C# Sharp program to check a given array of integers and return true if the specified number of same elements appears at the start and end of the given array

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to check a given array of integers and return true if the specified number of same elements appears at the start and end of the given array

Sample Output:

True
False
True

الأجوبة

using System;
namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {               
          Console.WriteLine(test(new[] { 3, 7, 5, 5, 3, 7 }, 2)); 
          Console.WriteLine(test(new[] { 3, 7, 5, 5, 3, 7 }, 3)); 
           Console.WriteLine(test(new[] { 3, 7, 5, 5, 3, 7, 5 }, 3)); 
  
        }       
        
       static bool test(int[] numbers, int len)
         {
           int arra_size = numbers.Length;

            for (int i = 0; i < len; i++)
            {
                if (numbers[i] != numbers[arra_size - len + i])
                {
                    return false;
                }
            }

            return true;
         }    
   } 
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...