Question

Write a method public static boolean FindSum (int[] a, int m) that takes an ascending sorted array a with n distinct integers

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#source code in java:

public class Test{

public static boolean FindSum(int[] a,int m){

int least=0;

int high=a.length-1;

while(least<high){

if(a[least]+a[high]==m){

return true;

}

else if(a[least]+a[high]<m) {

least=least+1;

}

else{

high=high-1;

}

}

return false;

}

public static void main(String args[]){

int a[]={1,6,8,9,10,11};

int m=16;

System.out.println(FindSum(a,m));

System.out.println(FindSum(a,24));

}

}

1 2 3 4 5 6 public class Test{ public static boolean FindSum(int[] a, int m) { int least=0; int high=a. length-1; while(least

The TIme complexity is 0(n) runtime which is best case of the algorithm

#source code output:

user@user-Latitude - 3490:-/Desktop$ javac Test.java user@user-Latitude - 3490:-/Desktop$ java Test true false user@user-Lati

#if you have any doubt or more information needed comment below..i will respond as possible as soon..thanks.

Add a comment
Know the answer?
Add Answer to:
Write a method public static boolean FindSum (int[] a, int m) that takes an ascending sorted...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Write in Java An array is sorted (in ascending order) if each element of the...

    Please Write in Java An array is sorted (in ascending order) if each element of the array is less than or equal to the next element . An array of size 0 or 1 is sorted Compare the first two elements of the array ; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted. Write a boolean -valued  method  named  isSorted that accepts an integer  array , and the number of...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Implement a method to build an AVL tree out of a sorted (ascending order) array of...

    Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You may use any of...

  • Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[]...

    Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[] two) Given two arrays, it returns a new array with all values that are in both arrays. The returned array has a size fitting its elements exactly and without duplicates. The order of values does not matter. For example, given {1,5,5,1} and {5,3}, the method returns {5}.

  • 1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines...

    1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines whether two distinct elements of the array add up to some specified value, "key". If so, return 1. Otherwise, return 0. The function signature is: int canMakeSum(int *array, int n, int key);

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int...

    Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int M, int K) this method determines the number of elements in the int array (arr) that are divisible by M but not K. Then write code inside main method to test your method: your main method accepts three numbers from the command argument list, N, M, K, use the first number to create int array with size of N, assign random number between 0...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • DO NOT CHANGE THE METHOD HEADER Write a method int indexFirstOne (int [] input) The input...

    DO NOT CHANGE THE METHOD HEADER Write a method int indexFirstOne (int [] input) The input array is sorted and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst case runtime must be O(logn) where n is the number of elements. Example a = [0, 0, 1, 1, 1] return 2

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT