-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp187.java
55 lines (48 loc) · 1.49 KB
/
p187.java
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
/*Given an array of positive integers. We need to make the given array a ‘Palindrome’. The only allowed operation is”merging” (of two adjacent elements). Merging two adjacent elements means replacing them with their sum. The task is to find the minimum number of merge operations required to make the given array a ‘Palindrome’.
Example :
Input : arr[] = {15, 4, 15}
Output : 0
Array is already a palindrome. So we
do not need any merge operation.
Input : arr[] = {1, 4, 5, 1}
Output : 1
We can make given array palindrome with
minimum one merging (merging 4 and 5 to
make 9)
Input : arr[] = {11, 14, 15, 99}
Output : 3
We need to merge all elements to make
a palindrome.
The expected time complexity is O(n).*/
import java.util.*;
public class minOperation {
public static int minOp(int n,int arr[]){
int i=0;
int j=n-1;
int ans=0;
while(i<=j){
if(arr[i]==arr[j]){
i++;
j--;
}else if(arr[i]<arr[j]){
i++;
arr[i]+=arr[i-1];
ans++;
}else if(arr[j]<arr[i]){
j--;
arr[j]+=arr[j+1];
ans++;
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println(minOp(n,arr));
}
}