Exam 2 Week 11 Program Design and Abet nethod LongestArray, which given an int[J
ID: 3701705 • Letter: E
Question
Exam 2 Week 11 Program Design and Abet nethod LongestArray, which given an int[JII (an array of longesta longestArray: Write a arrays), returns the intll 8. (10 points) longes in it with the greatest number of elements. integer 1 {12, . 1, 1, 1, ?), {1, 2,3),(1,2,3,4))·2(12, itaray datiI arn t 9. (10 points) smallestwordBeginning: Given an array of Strings, return the first three letters of the smallest string in the array. You may assume all words are at least 3 letters long. // ("Apple, "Orange", "Bananas", "Carrot" >App / f Blue") .> Blu /I f*Happy "Joy, "smile"Joy" public static string smallestWordBeginning(String!l words) t 20 points Page 8 of 13Explanation / Answer
Solution 8:
public static int[] longestArray(int[][] arr){
int l=arr.length;
int maxi=Integer.MIN_VALUE;
int[] ans=null;
for(int i=0;i<l;i++){
if(arr[i].length>maxi){
maxi=arr[i].length;
ans=arr[i];
}
}
return ans;
}
Sample input :
{{1,2},{3,4,5,6},{1,10,20,43,1,3,12}}
sample output:
1 10 20 43 1 3 12
Solution 9:
public static String smallestWordBeginning(String[] words){
int maxi=Integer.MIN_VALUE;
String ans=null;
for(int i=0;i<words.length;i++){
if(words[i].length()>maxi){
maxi=words[i].length();
ans=words[i];
}
}
return ans.substring(0,3);
}
sample input:
arr={"Hello","lovely","Handsome","such a beauty"};
sample output:
suc
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.