Problem Statement:
Input :- "1-2-3-4-5"
O/P:- Consecutive
Input :- "5-4-3-2-1"
O/P:- Consecutive
Input :- "1-4-3-2-5"
O/P:- Not Consecutive
Program
Test Case 2
Input :- "1-2-3-4-5"
O/P:- Consecutive
Input :- "5-4-3-2-1"
O/P:- Consecutive
Input :- "1-4-3-2-5"
O/P:- Not Consecutive
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cosecutive
{
class ConsecutiveClass
{
static void Main(string[] args)
{
string s = "1-2-3-4-5-6";
List<string> numbers = s.Split('-').ToList();
int hyphenCount = s.ToCharArray().Count(x => x == '-');
int distinctNumCnt=numbers.Distinct().Count();
int listMaxLen = distinctNumCnt - 1;
int loopcount=0;
if ((hyphenCount + 1) == distinctNumCnt)
{
for (int i = 0; i < numbers.Count/2; i++)
{
if (Math.Abs(Convert.ToInt32(numbers[listMaxLen - i]) - Convert.ToInt32(numbers[i])) + 1 == distinctNumCnt)
{
distinctNumCnt -= 2;
loopcount++;
}
else
{
Console.WriteLine("Not Consecutive");
break;
}
}
if (loopcount == numbers.Count / 2)
Console.WriteLine("Consecutive");
}
else
Console.WriteLine("Not Consecutive");
Console.ReadLine();
}
}
}
Test Case 1
Input :- "1-2-3-4-5"
O/p:
Test Case 2
Input :- "1-2-3-4-7"
O/p:
No comments:
Post a Comment