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 803888
Next
In Process
codemania
  • 0
codemania
Asked: May 27, 20232023-05-27T09:33:29+00:00 2023-05-27T09:33:29+00:00In: Education

Java Program to Find the Largest Subarray with Equal Number of 0s and 1s

  • 0
Java Program to Find the Largest Subarray with Equal Number of 0s and 1s
codejava
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

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

      This is the Java Program to Find the Largest sub-Array consisting of Equal Number of 0’s and 1’s.

      Problem Description

      Given an array, find out the longest subarray consisting of the equal number of zeroes and ones.

      Example:
      ArrayOne = [1, 2, 0, 5, 6, 0, 1, 0]

      Output
      1 2 0 5 6 0 1

      Problem Solution

      Maintain two variables start and end to store the starting and ending index of the subarray and initialize them to -1. Also, create a boolean variable flag mark it to be true. Iterate through the array, and check if the element is 0 or 1 if it is one and the flag is true set the flag to false, otherwise set the flag to true and update the variables starting and ending indexes.

      Program/Source Code

      Here is the source code of the Java Program to Find the Largest sub-Array consisting of Equal Number of 0’s and 1’s. 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 Find the Largest sub-Array 
      2. // Consisting of Equal Number of 0's and 1's
      3. import java.io.BufferedReader;
      4. import java.io.InputStreamReader;
      5. public class EqualZeroesAndOnes {
      6.     // Function to find out the largest subarray having equal zeroes and ones.
      7.     static int[] largestSubarray(int[] array){
      8.         int[] index = new int[2];
      9.         int start,end,i;
      10.         start = end = -1;
      11.         boolean flag = true;
      12.         for(i=0; i<array.length; i++){
      13.             if (array[i] == 0 || array[i] == 1){
      14.                 if(start == -1){
      15.                     start = i;
      16.                     flag = false;
      17.                 }
      18.                 else if (flag){
      19.                     flag = false;
      20.                 }
      21.                 else{
      22.                     flag = true;
      23.                     end = i;
      24.                 }
      25.             }
      26.         }
      27.         if(start != -1 && end != -1){
      28.             index[0] = start;
      29.             index[1] = end;
      30.         }
      31.         return index;
      32.     }
      33.     // Function to read the input and to display the subarray
      34.     public static void main(String[] args) {
      35.         BufferedReader br = new BufferedReader
      36.                             (new InputStreamReader(System.in));
      37.         int size;
      38.         System.out.println("Enter the size of the array");
      39.         try {
      40.             size = Integer.parseInt(br.readLine());
      41.         } catch (Exception e) {
      42.             System.out.println("Invalid Input");
      43.             return;
      44.         }
      45.         int[] array = new int[size];
      46.         System.out.println("Enter array elements");
      47.         int i;
      48.         for (i = 0; i < array.length; i++) {
      49.             try {
      50.                 array[i] = Integer.parseInt(br.readLine());
      51.             } catch (Exception e) {
      52.                 System.out.println("An error occurred");
      53.             }
      54.         }
      55.         int[] index = largestSubarray(array);
      56.         if(index[0] == 0 && index[1] == 0)
      57.             System.out.println("No such subarray exists");
      58.         else{
      59.             for(i=index[0]; i<=index[1]; i++){
      60.                 System.out.print(array[i] + " ");
      61.             }
      62.         }
      63.     }
      64. }
      Program Explanation

      1. In function largestSubarray(), the variables start and end are initialized to -1 and flag is set to true.
      2. The loop for(i=0; i<array.length; i++) is used to iterate through the array.
      3. The condition if(array[i] == 0 || array[i] == 1) checks for zero and one.
      4. The nested condition if(start == -1) checks if the current element is the first element of the subarray.
      5. The nested condition else if(flag) checks if the number of zeroes and ones were previously equal.
      6. The else part sets the end variable to point to the last entry of the subarray.

      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
      8
      Enter array elements
      1
      2
      0
      5
      6
      0
      1
      0
      1 2 0 5 6 0 1 
       
      Case 2 (Negative Test Case):
       
      Enter the size of the array
      5
      Enter array elements
      1
      2
      3
      4
      5
      No such subarray exists
      • 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 ...

      • 218 Answers
    • imran

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

      • 206 Answers
    • VARISHA PARVEZ

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

      • 180 Answers
    • Shivangi
      Shivangi added an answer D. all of the mentioned May 29, 2023 at 7:55 pm
    • Shriyam Dubey
      Shriyam Dubey added an answer gets increased by 50 percent May 29, 2023 at 7:55 pm
    • Shivangi
      Shivangi added an answer D.all May 29, 2023 at 7:55 pm

    Related Questions

    • 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 ...

      • 0 Answers
    • Network operating system runs on ___________ a) every system in ...

      • 3 Answers
    • Which principle states that programs, users, and even the systems ...

      • 1 Answer

    Top Members

    Afiya Arshi

    Afiya Arshi

    • 0 Questions
    • 0 Answers
    Hafsa Quraishi

    Hafsa Quraishi

    • 0 Questions
    • 0 Answers
    TANNU .

    TANNU .

    • 0 Questions
    • 0 Answers

    Trending Tags

    account algorithm analytics answer bank c computer computer science python cs e commerce electric electronics it language mcq mechanical physics 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 🇮🇳