import java.lang.Math; class Solution { public int maxChunksToSorted(int[] arr) { int noOfChunk=0; int TotalEleInChunk=0; int maxEleOfChunk=-1; int TotalEleInVariousChunk=0; for(int i:arr){ maxEleOfChunk=Math.max(i,maxEleOfChunk); TotalEleInChunk+=1; if(TotalEleInChunk==(maxEleOfChunk+1-TotalEleInVariousChunk)){ noOfChunk+=1; TotalEleInVariousChunk+=TotalEleInChunk; TotalEleInChunk=0; maxEleOfChunk=-1; } } return noOfChunk; } }
import java.lang.Math;
class Solution {
public int maxChunksToSorted(int[] arr) {
int noOfChunk=0;
int TotalEleInChunk=0;
int maxEleOfChunk=-1;
int TotalEleInVariousChunk=0;
for(int i:arr){
maxEleOfChunk=Math.max(i,maxEleOfChunk);
TotalEleInChunk+=1;
if(TotalEleInChunk==(maxEleOfChunk+1-TotalEleInVariousChunk)){
noOfChunk+=1;
TotalEleInVariousChunk+=TotalEleInChunk;
TotalEleInChunk=0;
maxEleOfChunk=-1;
}
}
return noOfChunk;
}
}
#PYTHON SOLUTION
class Solution:
def maxChunksToSorted(self, arr: List[int]) -> int:
noOfChunk=0
TotalEleInChunk=0
maxEleOfChunk=-1
TotalEleInVariousChunk=0
for i in arr:
maxEleOfChunk=max(i,maxEleOfChunk)
TotalEleInChunk+=1
if(TotalEleInChunk==(maxEleOfChunk+1-TotalEleInVariousChunk)):
noOfChunk+=1
TotalEleInVariousChunk+=TotalEleInChunk
TotalEleInChunk=0
maxEleOfChunk=-1
print(i,maxEleOfChunk,TotalEleInChunk,TotalEleInVariousChunk,noOfChunk)
return noOfChunk
#[0,1,2,3,4,5]
# [2,1,0,4,3,5]
# 0,1,2
# 3,4
# 5