0% found this document useful (0 votes)
11 views5 pages

Java Skill Test: 1) Write A Program To Find All "Leaders" in An Array

The document contains two Java programs: one to find all leaders in an array and another to determine the length of the longest increasing subsequence. The first program identifies leaders by comparing each element with the maximum from the right, while the second program uses dynamic programming to compute the longest increasing subsequence. Both programs include sample arrays and print the results to the console.

Uploaded by

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

Java Skill Test: 1) Write A Program To Find All "Leaders" in An Array

The document contains two Java programs: one to find all leaders in an array and another to determine the length of the longest increasing subsequence. The first program identifies leaders by comparing each element with the maximum from the right, while the second program uses dynamic programming to compute the longest increasing subsequence. Both programs include sample arrays and print the results to the console.

Uploaded by

mokshagnanamburu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java

Skill test
1) Write a program to find all "leaders"
in an array
Import java.util.ArrayList;
Import java.util.Collections;

Public class LeadersInArray {


Public static void findLeaders(int[] arr) {
Int n = arr.length;
ArrayList<Integer> leaders = new
ArrayList<>();
Int maxFromRight = arr[n – 1]; //
Rightmost element is always a leader

// Add last element as it’s always a leader


Leaders.add(maxFromRight);

// Traverse from second-last element to


the first
For (int I = n – 2; I >= 0; i--) {
If (arr[i] > maxFromRight) { // If current
element is greater than maxFromRight
Leaders.add(arr[i]);
maxFromRight = arr[i]; // Update
maxFromRight
}
}

// Reverse to maintain original order of


leaders
Collections.reverse(leaders);

// Print leaders
System.out.println(“Leaders in the array: “
+ leaders);
}

Public static void main(String[] args) {


Int[] arr = {16, 17, 4, 3, 5, 2};
findLeaders(arr);
}
}
Output:

2) Write a program to find the length of


the longest increasing subsequence in an
array.

Import java.util.Arrays;

Public class LongestIncreasingSubsequence {


Public static int findLIS(int[] arr) {
Int n = arr.length;
Int[] dp = new int[n]; // Array to store LIS
lengths
Arrays.fill(dp, 1); // Each element alone is an
LIS of length 1

// Compute LIS for each element


For (int I = 1; I < n; i++) {
For (int j = 0; j < I; j++) {
If (arr[i] > arr[j]) { // Increasing sequence
check
Dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}

// Find the maximum LIS length


Int maxLIS =
Arrays.stream(dp).max().getAsInt();
Return maxLIS;
}
Public static void main(String[] args) {
Int[] arr = {10, 22, 9, 33, 21, 50, 41, 60};
System.out.println(“Length of Longest
Increasing Subsequence: “ + findLIS(arr));
}
}

Output:

You might also like