Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Quearn

Quearn Logo Quearn Logo

Quearn Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Quearn Academy
  • About Us
  • Blog
  • Contact Us
Home/Questions/Q 803886
Next
In Process
codemania
  • 0
codemania
Asked: May 27, 20232023-05-27T09:30:55+00:00 2023-05-27T09:30:55+00:00In: Education

Java Program to Find Local Minima in an Array

  • 0
Java Program to Find Local Minima in an Array
codejava
  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    1. codemania
      2023-05-27T09:31:12+00:00Added an answer on May 27, 2023 at 9:31 am
      This answer was edited.

      This is the Java Program to Find Local Minimas in an Array.

      Problem Description

      Given an array of integers, find out the local maxima present in the array. An element in an array is a local minima if it less than the element after it, and the element before it. For the elements at the extreme end only one check is required, that is, the element following the first element or the element before the last element. In case of two or more minima, only one of them is returned. Example: Array = [1, 2, 3, 7, 5, 6] Output: 1 or 5

      Problem Solution

      The idea here is to use an algorithm, similar to the binary search. Check the middle element of the array, if it is less than the elements following it and the element preceding it, then it is the local minima, else if it is less than the preceding element, then the local minima is in the left half, else the local minima is in the right half.

      Program/Source Code
       
      Here is the source code of the Java Program to Find Local Minima in an Array. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

      1.  
      2. //Java Program to Find Local Minimas in an Array
      3.  
      4. import java.io.BufferedReader;
      5. import java.io.InputStreamReader;
      6.  
      7. public class LocalMinima {
      8.     // Function to return the index of the local minima
      9.     static int localMinima(int[] array){
      10.         int low,mid,high;
      11.         low = 0;
      12.         high = array.length-1;
      13.         mid = (low + high)/2;
      14.         int ans;
      15.         while(low<=high){
      16.             mid = (low + high)/2;
      17.             if((mid == 0 || array[mid-1] > array[mid])
      18.                 && (mid == array.length-1 || array[mid+1] > array[mid])){
      19.                 return mid;
      20.             }
      21.             else if(mid > 0 && array[mid-1] < array[mid]){
      22.                 high = mid-1;
      23.             }
      24.             else{
      25.                 low = mid+1;
      26.             }
      27.         }
      28.         return -1;
      29.     }
      30.     // Function to read user input
      31.     public static void main(String[] args) {
      32.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      33.         int size;
      34.         System.out.println("Enter the size of the array");
      35.         try {
      36.             size = Integer.parseInt(br.readLine());
      37.         } catch (Exception e) {
      38.             System.out.println("Invalid Input");
      39.             return;
      40.         }
      41.         int[] array = new int[size];
      42.         System.out.println("Enter array elements");
      43.         int i;
      44.         for (i = 0; i < array.length; i++) {
      45.             try {
      46.                 array[i] = Integer.parseInt(br.readLine());
      47.             } catch (Exception e) {
      48.                 System.out.println("An error occurred");
      49.                 return;
      50.             }
      51.         }
      52.         int index = localMinima(array);
      53.         System.out.println("The local minima is " + array[index]);
      54.     }
      55. }
      Program Explanation

      1. In the function localMinima(), we initialize two variables high and low as array.length-1 and 0 respectively. 2. Using a loop while(low<=high), we first calculate the middle index. 3. Now, in the condition if((mid == 0 || array[mid-1] > array[mid]) && (mid == array.length-1 || array[mid+1] > array[mid])), we check whether the element at current middle index is a minima. 4. If the element is a minima, then we return the current middle index, otherwise using the condition else if(mid > 0 && array[mid-1] < array[mid]), we first check that we are not at the beginning of the array and finally, check if the preceding element is smaller than the current middle element. 5. If it is, we then set high to mid-1, to look in the first half of the array, otherwise, we set low to mid+1 to look for the minima in the second half of the array.

       

      Time Complexity: O(log(n)) where n is the number of elements in the array.

      Runtime Test Cases
       
      Case 1 (Positive test Case - local minima is not at the extreme ends):
       
      Enter the size of the array
      8
      Enter array elements
      23
      12
      11
      10
      13
      5
      57
      58
      The local minima is 10
       
      Case 2 (Positive test Case - local minima is at the beginning of the array):
       
      Enter the size of the array
      5
      Enter array elements
      1
      2
      3
      4
      5
      The local minima is 1
       
       
      Case 3 (Positive test Case - local minima is at the end of the array):
       
      Enter the size of the array
      5
      Enter array elements
      9
      8
      7
      6
      5
      The local minima is 5
      • 0
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Related Questions

    • Codility Passing Car Problem in Java
    • Dutch National Flag Problem in Java
    • Java Program to Check if an Array is Decreasing
    • Java Program to Check if an Array is Increasing
    • Smallest Sum Contiguous Subarray in Java

    Sidebar

    Ask A Question
    • Popular
    • Answers
    • imran

      Point out the wrong statement. A. The standard instances are ...

      • 272 Answers
    • imran

      Point out the correct statement. A. A volume is mounted ...

      • 250 Answers
    • VARISHA PARVEZ

      Which type of e‐commerce focuses on consumers dealing with ...

      • 181 Answers
    • Shivangi Panthi
      Shivangi Panthi added an answer C.the June 6, 2023 at 7:36 pm
    • Shivangi Panthi
      Shivangi Panthi added an answer D.when June 6, 2023 at 7:36 pm
    • Shivangi Panthi
      Shivangi Panthi added an answer B.a June 6, 2023 at 7:35 pm

    Related Questions

    • 30. A noun that dandies neither a male or a ...

      • 1 Answer
    • What does grade 33 cement indicate?

      • 1 Answer
    • . In Unix, which system call creates the new process? ...

      • 1 Answer
    • What are the types of distributed operating systems? a) Zone ...

      • 1 Answer
    • Network operating system runs on ___________ a) every system in ...

      • 7 Answers

    Top Members

    jafri

    jafri

    • 0 Questions
    • 0 Answers
    Namita

    Namita

    • 0 Questions
    • 0 Answers
    Sohail hossain

    Sohail hossain

    • 0 Questions
    • 0 Answers

    Trending Tags

    account algorithm analytics answer c computer computer science python cs e commerce electric electronics it language mcq mechanical physics poll programming python question

    Explore

    • Home
    • Quearn Academy
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    Quearn

    Quearn is a social questions & Answers Engine which will help you establish your community and connect with other people, Also you Can Earn Money.

    About Us

    • Meet The Team
    • Blog
    • About Us
    • Contact Us

    Legal Stuff

    • Terms of Use
    • Privacy Policy
    • Cookie Policy

    Help

    • Knowledge Base
    • Support

    Follow

    © 2018-2023 Quearn®. All Rights Reserved | Developed by ASHAS Industries Proudly 🇮🇳