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
  • Quearn Academy (Free Courses)
  • Sili (Free AI)
  • Quearn Drive (30GB Free Storage)
  • Guest Post (Lifetime Dofollow Backlink)
  • About Us
  • Blog
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Quearn Academy (Free Courses)
  • Sili (Free AI)
  • Quearn Drive (30GB Free Storage)
  • Guest Post (Lifetime Dofollow Backlink)
  • About Us
  • Blog
Home/Questions/Q 803892
Next
In Process

Quearn Latest Questions

codemania
  • 0
codemania
Asked: May 27, 20232023-05-27T09:38:32+00:00 2023-05-27T09:38:32+00:00In: Education

Java Program to Check if an Array is Decreasing

  • 0
Java Program to Check if an Array is Decreasing
codejava
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    1. codemania
      codemania
      2023-05-27T09:38:57+00:00Added an answer on May 27, 2023 at 9:38 am

      This is the Java Program to Check if an Array is Strictly Decreasing.

      Problem Description

      Given an array of integers check whether it is strictly decreasing or not.
      A strictly decreasing array is an array whose each element is smaller than it’s preceding element.

      Example:
      Array = [5, 4, 3, 2, 1]

      Output: Array is strictly decreasing.

      Problem Solution

      Iterate through the array and check whether the current array element, is smaller than its preceding element if all the elements are smaller than its preceding element return true, else return false.

      Here is the source code of the Java Program to Check if an Array is Strictly Decreasing. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

      1. //Java Program to Check if an Array is Strictly Decreasing
      2. import java.io.BufferedReader;
      3. import java.io.InputStreamReader;
      4. public class StrictlyDecreasing {
      5.     // Function to check array is strictly decreasing 
      6.     static boolean checkStrictlyDecreasing(int[] array){
      7.     boolean result=true;
      8.     int i;
      9.     for(i=0;i<array.length-1;i++){
      10.         if(array[i]<=array[i+1])
      11.         {
      12.             result=false;
      13.             break;
      14.         }
      15.       }
      16.     return result;
      17.     }
      18.     // Function to read input and decreasing output
      19.     public static void main(String[] args) {
      20.         BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
      21.         int size;
      22.         System.out.println("Enter the size of the array");
      23.         try{
      24.             size=Integer.parseInt(br.readLine());
      25.         }
      26.         catch(Exception e)
      27.         {
      28.             System.out.println("Invalid Input");
      29.             return;
      30.         }
      31.         int[] array=new int[size];
      32.         System.out.println("Enter array elements");
      33.         int i;
      34.         for(i=0;i<array.length;i++){
      35.             try{
      36.                 array[i]=Integer.parseInt(br.readLine());
      37.             }
      38.             catch(Exception e)
      39.             {
      40.                 System.out.println("An error occurred");
      41.             }
      42.         }
      43.         boolean result=checkStrictlyDecreasing(array);
      44.         if(result){
      45.             System.out.println("Array is strictly decreasing");
      46.         }
      47.         else{
      48.             System.out.println("Array is not strictly decreasing");
      49.         }
      50.     }
      51. }
      Program Explanation

      1. In function checkStrictlyDecreasing(), the loop for(i=0; i<array.length; i++) is used to iterate through the array.
      2. The condition if(array[i] <= array[i+1]) checks if the current element is smaller than its following element.
      3. If the condition is true, flag is set to false, indicating that the array is not strictly decreasing.
      4. Finally, the flag variable is returned.

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

      Runtime Test Cases
       
      Case 1 (Positive Test Case):
       
      Enter the size of the array
      5
      Enter array elements
      5
      4
      3
      2
      1
      Array is strictly decreasing
       
      Case 2 (Negative Test Case):
       
      Enter the size of the array
      8
      Enter array elements
      6
      5
      5
      4
      3
      2
      1
      1
      Array is not strictly decreasing
      • 0
      • Share
        Share
        • Share on Facebook
        • 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 Increasing
    • Smallest Sum Contiguous Subarray in Java
    • Java Program to Find the Largest Subarray with Equal Number of 0s and 1s

    Sidebar

    Ask A Question
    • Popular
    • Answers
    • imran

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

      • 284 Answers
    • imran

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

      • 261 Answers
    • VARISHA PARVEZ

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

      • 181 Answers
    • Sourabh Pandey
      Sourabh Pandey added an answer a) true July 21, 2023 at 7:20 am
    • Sourabh Pandey
      Sourabh Pandey added an answer a) dispersion coefficient = magnetizing current / ideal short circuit… July 21, 2023 at 7:20 am
    • riya Dhwan
      riya Dhwan added an answer current July 8, 2023 at 10:48 pm

    Related Questions

    • 28. _____________are NOT a part of the Scheduled banking ...

      • 1 Answer
    • 25. A bank is a financial intermediary because A. it ...

      • 1 Answer
    • 24. How many developments banks are there in India? A. ...

      • 1 Answer
    • 23. Who insures banks in India? A. IRDA B. EXIM ...

      • 1 Answer
    • 22. What is the full-form of NBFC? A. Non-Bank Financial ...

      • 1 Answer

    Top Members

    Misba Waqas

    Misba Waqas

    • 0 Questions
    • 0 Answers
    R.R.R SPORTS

    R.R.R SPORTS

    • 0 Questions
    • 0 Answers
    Farhan Butt

    Farhan Butt

    • 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

    • Quearn Do QnA Earn Money
    • 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 🇮🇳

    Enable Notifications OK No thanks