-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromeOpetationToArray.cs
57 lines (51 loc) · 1.13 KB
/
PalindromeOpetationToArray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
namespace ArrayPalindrome
{
internal static class Program
{
public static void Main(string[] args)
{
var arr = new int[] {1, 3, 8, 6, 1};
var length = arr.Length;
var t = minOperatins(arr, length);
if (t == 0)
{
Console.WriteLine("Array is already palindrome, Minimum no of merge operations took is " + t);
}
else if (t == length - 1)
{
Console.WriteLine("merging all elements to get a palindrome, Minimum no of merge operations took is " + t);
}
else
{
Console.WriteLine("Minimum no of merge operations took is " + t);
}
}
private static int minOperatins(int[] arr, int length)
{
int ans = 0, i = 0;
var j = length - 1;
while (i <= j)
{
if (arr[i] == arr[j])
{
i++;
j--;
}
else if (arr[i] > arr[j])
{
arr[j - 1] = arr[j - 1] + arr[j];
j--;
ans++;
}
else
{
arr[i + 1] = arr[i + 1] + arr[i];
i++;
ans++;
}
}
return ans;
}
}
}