-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2130.java
More file actions
46 lines (40 loc) · 816 Bytes
/
Copy pathQuestion2130.java
File metadata and controls
46 lines (40 loc) · 816 Bytes
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
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 2130
*/
public class Question2130
{
public static int pairSum(ListNode head)
{
int length = 0;
ListNode temp = head;
while(temp!=null)
{
length++;
temp=temp.next;
}
temp = head;
int[] arr = new int[length];
int index= 0;
while(temp!=null)
{
arr[index++] = temp.val;
temp = temp.next;
}
int maximum = Integer.MIN_VALUE;
for(int i = 0;i<index/2+1;i++)
{
if((arr[i] + arr[length-i-1])>maximum)
maximum = (arr[i] + arr[length-i-1]);
}
return maximum;
}
public static void main(String[] args)
{
ListNode head = LinkedList.create(5,"unsorted");
LinkedList.display(head);
int result = pairSum(head);
System.out.println(result);
}
}