Write a static method named contains that accepts two arrays of integers a1 and
ID: 3643592 • Letter: W
Question
Write a static method named contains that accepts two arrays of integers a1 and a2 asparameters and that returns a boolean value indicating whether or not a2's sequence of
elements appears in a1 (true for yes, false for no). The sequence of elements in a2 may
appear anywhere in a1 but must appear consecutively and in the same order. For example, if
variables called list1 and list2 store the following values:
int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8};
int[] list2 = {1, 2, 1};
Then the call of contains(list1, list2) should return true because list2's sequence of
values {1, 2, 1} is contained in list1 starting at index 5. If list2 had stored the values {2,
1, 2}, the call of contains(list1, list2) would return false because list1 does not
contain that sequence of values. Any two lists with identical elements are considered to contain
each other, so a call such as contains(list1, list1) should return true.
Explanation / Answer
import java.lang.*;
public class HelloWorld{
public static void main(String []args){
// System.out.println("Hello World");
double iArr[] = {16.1, 12.3, 22.2, 14.4};
double[] cArr ={12.3, 22.2};
System.out.println(isSubset(iArr,cArr,iArr.length,cArr.length));
}
public static boolean isSubset(double arr1[], double arr2[], int m, int n)
{
int count = 0;
boolean flag=true;
for(int i=0;i<=m-n;i++){
for(int j=0;j<n;j++){
int k=i;
if(arr1[k]==arr2[j]){
count++;
}
k++;
}
if(count==n){
flag = true;
}
else{
flag = false;
}
}
return flag;
}
}
output:
testcase 1:
array1 :{16.1, 12.3, 22.2, 14.4}
array2: {12.3, 22.2}
o/p: true
testcase2:
array1 :{16.1, 12.3, 22.2, 14.4}
array2: {12.3, 14.4}
o/p: false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.