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

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7760883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:00:33+00:00 2026-06-01T14:00:33+00:00

Given the array, i need to find how many monotonically increasing Sub-arrays there are

  • 0

Given the array, i need to find how many monotonically increasing Sub-arrays there are in that array?

For example, with [0, 1, 3, 1 , 2] – has 2 monotonical sub-arrays : [0, 1,3] and [1,2].

public class SUB_ARRAY {
    public static void main(String a[]){
        int[] x = new int[6];
        x[0]=1;
        x[1]=2;
        x[2]=3;
        x[3]=6;
        x[4]=9;
        x[5]=10;
        ArrayList<Object> arraylist = new ArrayList<Object>();
        HashSet list = new HashSet();
        for ( int i=0; i< (x.length -1); i++){
            if (x[i+1]> x[i] ){
                list.add(x[i]);
                list.add(x[i+1]);

            } else if (x[i+1] < x[i] || x[i+1]==x[i]) {
                arraylist.add(list.clone());    
                list.clear();    
            }
        }    
        System.out.println(arraylist.size());

    }    
}

The output is : 0 (instead of 1).

So, where I’m wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:00:35+00:00Added an answer on June 1, 2026 at 2:00 pm

    Check out this solution. It now only displays the counter but print you the subarrays. If you need only the continues subarrays you can easily modify it.
    As you see I use neither HashSet nor ArrayList for storing temporary data just a counter.

    import java.util.ArrayList;
    public class SUB_ARRAY{
        public static int SUBARRAY_MINIMUM_LENGTH = 2;
        public static void main(String a[]){
            ArrayList<Integer> x = new ArrayList<Integer>();
            x.add(5);
            x.add(0);
            x.add(1);
            x.add(3);
            x.add(4);
            x.add(2);
            x.add(3);
            x.add(6);
            x.add(1);
            x.add(0);
            x.add(4);
            int monoton = 0;
            int changed = -1;
            System.out.println("Initial array: " + x.toString());
            for ( int i=0; i< x.size() -1; ++i){
                if (x.get(i+1) > x.get(i)){
                    if (changed > -1){
                        for (int j = changed; j <i+2; ++j){
                            monoton += checkSubArray(x, j, i+2);;
                        }
                    }
                    else{
                        System.out.println("New monoton subarray start index: " + i + " value: " + x.get(i));
                        changed = i;
                        monoton += checkSubArray(x, changed, i+2);
                    }
                }
                else if (changed > -1){
                    changed = -1;
                }
            }    
            System.out.println("Monoton count: " + monoton);
        }    
    
        private static int checkSubArray(ArrayList<Integer> x, int start, int end)
        {
            if (end-start < SUBARRAY_MINIMUM_LENGTH){ 
                return 0;
            }
            for (int subi = start; subi < end; ++subi){
                System.out.print(" " + x.get(subi));
            }
            System.out.println();
            return 1;
        }
    }
    

    The output will be the following

    Initial array: [5, 0, 1, 3, 4, 2, 3, 6, 1, 0, 4]
    New monoton subarray start index: 1 value: 0
     0 1
     0 1 3
     1 3
     0 1 3 4
     1 3 4
     3 4
    New monoton subarray start index: 5 value: 2
     2 3
     2 3 6
     3 6
    New monoton subarray start index: 9 value: 0
     0 4
    Monoton count: 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given array a= [1,4,5,9,2].I need to find/print combinations of two values where sum =
Given an array that has the numbers {0......2^k -1} except for one number ,
Given an array of size N I need to find the min number of
Given an integer array, i need to find which number occurred most number of
I have an assignment that is the following: For a given integer array, find
I have a string. I need to replace all instances of a given array
I'm given an array (a2d) and I need to determine if every row and
I need to write effective and quick method to search byte array for given
I need to convert a navigable map to a 2D String array. Below given
Given a list of arrays and lots of setup time I need to quickly

Explore

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

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.