We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
funcfairRations(B:[Int])->String{// Write your code herevaroddIndex:[Int]=[Int]()varoddDict:[Int:Int]=[:]varsteps:Int=0forindexin0...B.count-1{ifB[index]%2==1{oddIndex.append(index)oddDict[index]=index}}ifoddIndex.count%2==1{return"NO"}while(oddIndex.count>1){varcurrentOddIndex:Int=oddIndex.first!steps+=2letnextOddIndex:Int=currentOddIndex+1ifoddDict[nextOddIndex]==nil{oddIndex[0]=nextOddIndexoddDict.removeValue(forKey:currentOddIndex)oddDict[nextOddIndex]=nextOddIndexcontinue}while(currentOddIndex<=nextOddIndex){oddIndex.removeFirst()oddDict.removeValue(forKey:currentOddIndex)currentOddIndex+=1}}returnString(steps)}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fair Rations
You are viewing a single comment's thread. Return to all comments →
SWIFT