Maximum Consecutive Ones In An Array ( LeetCode
Vložit
- čas přidán 26. 07. 2024
- We have to write a program in java to count the maximum consecutive ones in an array. This is a Leetcode problem number 485.
So consider an array 0,1,1,0,0,1,1,1,0. The count of maximum consecutive ones will be 3.
For array 0,1,1,0. The count of consecutive ones will be 2.
For array 1,1,1,1,1. The count of consecutive ones will be 4 ie. the length of the array
For array 0,0. The count of consecutive ones will 0.
How do we solve this question?
The naive approach is simple. We can create a variable count to store the count of consecutive ones and another variable max count to store the maximum of all the counts.
We can run a loop to check the consecutive ones.
But this isn't the optimized approach.
Let's understand the optimized approach in this lecture.
Watch the complete lecture to know more.
Subscribe @TAPACADEMY
For more information, fill out this form: forms.gle/8eiUmM92Fx563Aen9
or call us at 8884881203
Facebook: / thetapacademy
Instagram: / tapacademy_online
Linkedin: / 73820805
Website: www.thetapacademy.com
#java #array #arrayjava #javaprogramming #programming #coding #coder #job #placement
sir your explanation for dsa is the best...there is something in your explanation which no one else can explain...
SIR your explaination is just looking like woow
Sir,according to me NO OnE explains like you with animation
We cant forget logic even because you are using animation
we are watching like a movie, How can we forget sir
Superb sir
Rohit sir u r my 1st fav on Utube
Thanks brother. Keep supporting Tap Academy
Thank you so much Rohit sir
Waiting for maximum subarray
Sir we can also take 0 also
Sir program is not running,
Showing the error of "math.max"
What should I do sir , please reply sir
Please elaborate the problem
Type maxcount= Math.max(count, maxcount);
public static int countOnes(int num[]) {
int maxCount=0;
int count=0;
for(int i=0;i