Given a binary array, find the index of 0 to be replaced with 1 to get maximum length sequence of continuous ones.





For example,



Consider the array { 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 }.

The index to be replaced is 7 to get continuous sequence of length 6 containing all 1’s.



We can efficiently solve this problem in linear time and constant space. The idea is to traverse the given array and maintain index of previous zero encountered. Then for each subsequent zeroes, we can easily find out number of 1’s between current zero and last zero. For each element we check if maximum sequence of continuous 1’s ending at that element (including last zero which is now replaced by 1) exceeds maximum sequence found so far. If yes, we update the maximum sequence to current sequence length and index of optimal zero to index of last zero encountered.

C #include <stdio.h> // Find index of 0 to replaced with 1 to get maximum sequence // of continuous 1's int findIndexofZero(int arr[], int n) { int max_count = 0; // stores maximum number of 1's (including zero) int max_index = -1; // stores index of 0 to be replaced int prev_zero_index = -1; // stores index of previous zero int count = 0; // store current count of zeros // consider each index i of the array for (int i = 0; i < n; i++) { // if current element is 1 if (arr[i] == 1) { count++; } else // if current element is 0 { // reset count to 1 + number of ones to the left of current 0 count = i - prev_zero_index; // update prev_zero_index to current index prev_zero_index = i; } // update maximum count and index of 0 to be replaced if required if (count > max_count) { max_count = count; max_index = prev_zero_index; } } // return index of 0 to be replaced or -1 if array contains all 1's return max_index; } // main function int main(void) { int arr[] = { 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 }; int n = sizeof(arr) / sizeof(arr[0]); int index = findIndexofZero(arr, n); if (index != -1) printf("Index to be replaced is %d", index); else printf("Invalid input"); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include <stdio.h> // Find index of 0 to replaced with 1 to get maximum sequence // of continuous 1's int findIndexofZero ( int arr [ ] , int n ) { int max_count = 0 ; // stores maximum number of 1's (including zero) int max_index = - 1 ; // stores index of 0 to be replaced int prev_zero_index = - 1 ; // stores index of previous zero int count = 0 ; // store current count of zeros // consider each index i of the array for ( int i = 0 ; i < n ; i ++ ) { // if current element is 1 if ( arr [ i ] == 1 ) { count ++ ; } else // if current element is 0 { // reset count to 1 + number of ones to the left of current 0 count = i - prev_zero_index ; // update prev_zero_index to current index prev_zero_index = i ; } // update maximum count and index of 0 to be replaced if required if ( count > max_count ) { max_count = count ; max_index = prev_zero_index ; } } // return index of 0 to be replaced or -1 if array contains all 1's return max_index ; } // main function int main ( void ) { int arr [ ] = { 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int index = findIndexofZero ( arr , n ) ; if ( index != - 1 ) printf ( "Index to be replaced is %d" , index ) ; else printf ( "Invalid input" ) ; return 0 ; } Download Run Code Java class FindIndexofZero { // Find index of 0 to replaced with 1 to get maximum sequence // of continuous 1's public static int findIndexofZero(int[] A) { int max_count = 0; // stores maximum number of 1's (including 0) int max_index = -1; // stores index of 0 to be replaced int prev_zero_index = -1; // stores index of previous zero int count = 0; // store current count of zeros // consider each index i of the array for (int i = 0; i < A.length; i++) { // if current element is 1 if (A[i] == 1) { count++; } else // if current element is 0 { // reset count to 1 + no. of ones to the left of current 0 count = i - prev_zero_index; // update prev_zero_index to current index prev_zero_index = i; } // update maximum count & index of 0 to be replaced if required if (count > max_count) { max_count = count; max_index = prev_zero_index; } } // return index of 0 to be replaced or -1 if array contains all 1's return max_index; } // main function public static void main (String[] args) { int[] A = { 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 }; int index = findIndexofZero(A); if (index != -1) { System.out.print("Index to be replaced is " + index); } else { System.out.print("Invalid input"); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 class FindIndexofZero { // Find index of 0 to replaced with 1 to get maximum sequence // of continuous 1's public static int findIndexofZero ( int [ ] A ) { int max_count = 0 ; // stores maximum number of 1's (including 0) int max_index = - 1 ; // stores index of 0 to be replaced int prev_zero_index = - 1 ; // stores index of previous zero int count = 0 ; // store current count of zeros // consider each index i of the array for ( int i = 0 ; i < A . length ; i ++ ) { // if current element is 1 if ( A [ i ] == 1 ) { count ++ ; } else // if current element is 0 { // reset count to 1 + no. of ones to the left of current 0 count = i - prev_zero_index ; // update prev_zero_index to current index prev_zero_index = i ; } // update maximum count & index of 0 to be replaced if required if ( count > max_count ) { max_count = count ; max_index = prev_zero_index ; } } // return index of 0 to be replaced or -1 if array contains all 1's return max_index ; } // main function public static void main ( String [ ] args ) { int [ ] A = { 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 } ; int index = findIndexofZero ( A ) ; if ( index != - 1 ) { System . out . print ( "Index to be replaced is " + index ) ; } else { System . out . print ( "Invalid input" ) ; } } } Download Run Code



Output:



Index to be replaced is 7





The time complexity of above solution is O(n) and auxiliary space used by the program is O(1) .





We have discussed two more approaches to solve this problem:

1. By replacing non-zero elements with count of their adjacent consecutive 1’s

2. Using Sliding Window Technique



(1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.

Like us? Please spread the word and help us grow. Happy coding 🙂

