0% found this document useful (0 votes)
5 views2 pages

Longest Palindromic Sublist Finder

Uploaded by

pawan.sahu.2027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Longest Palindromic Sublist Finder

Uploaded by

pawan.sahu.2027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🔹 Problem: 2

You are given a singly linked list of integers.


Find the length of the longest contiguous sublist that is a palindrome.

public static int maxPalindrome(Node head) {

Node prev = null;

Node curr = head;

int maxLength = 0;

while (curr != null) {

Node next = [Link];

[Link] = prev;

// Check for odd length palindrome

int oddLen = 2 * countCommon(prev, next) + 1;

maxLength = [Link](maxLength, oddLen);

// Check for even length palindrome

int evenLen = 2 * countCommon(curr, next);

maxLength = [Link](maxLength, evenLen);

prev = curr;

curr = next;

return maxLength;

// Count common elements in two directions

public static int countCommon(Node a, Node b) {

int count = 0;

while (a != null && b != null && [Link] == [Link]) {

count++;

a = [Link];

b = [Link];

return count;

}
🔸 Problem Statement: 3

You're given:

 An integer array A of size n: represents positions or "heights".


 An integer K: your maximum jump range.
 An integer array C of size n: cost to land at each position.

public class MinCostJumpGame {

public static int minCost(int[] A, int[] C, int K) {

int n = [Link];

long[] dp = new long[n];

[Link](dp, Long.MAX_VALUE);

dp[0] = 0;

for (int i = 0; i < n; i++) {

if (dp[i] == Long.MAX_VALUE) continue;

for (int j = i + 1; j <= [Link](i + K, n - 1); j++) {

long cost = [Link](A[i] - A[j]) + C[j];

dp[j] = [Link](dp[j], dp[i] + cost);

return dp[n - 1] == Long.MAX_VALUE ? -1 : (int) dp[n - 1];

public static void main(String[] args) {

int[] A = {1, 3, 5, 2, 8};

int[] C = {0, 2, 4, 1, 3};

int K = 2;

[Link](minCost(A, C, K)); // Output: 14

You might also like