Kotlin Coding with Codility

Content Table:


Find Out the number of interesting Clock with Kotlin


fun main() {
	val count = solution("15:15:00", "15:15:12")
	println(count)
}

fun solution(S: String, T: String): Int {
	val s = S.split(":")
	val t = T.split(":")
	
	val sH = strToTime(s[0])
	val sM = strToTime(s[1])
	val sS = strToTime(s[2])
	
	val tH = strToTime(t[0])
	val tM = strToTime(t[1])
	val tS = strToTime(t[2])
	
	var total = 0
	
	for (h in sH..tH) {
		if(h < tH) {
			for (m in 0..59) {
				for (s in 0..59) {
					if(interestingClock(h,m,s)) total++
				}
			}
		} else {
			for (m in sM..tM) {
				if(m < tM) {
					for (s in 0..59) {
						if(interestingClock(h,m,s)) total++
					}				
				}else {
					for (s in sS..tS) {
						if(interestingClock(h,m,s)) total++
					}				
				}
			}			
		}
	}
	
	return total
}

fun interestingClock(h: Int, m: Int, s: Int): Boolean {
	val list: MutableList<Int> = arrayListOf()
    list.addAll(numToList(h))
    list.addAll(numToList(m))
	list.addAll(numToList(s))
   
    if (list.distinct().count() <= 2)
		return true
	else 
    	return false
}

fun numToList(num: Int): List<Int> {
	val list: MutableList<Int> = mutableListOf()
    if(num < 10){
    	list.add(0)
    	list.add(num)
    }else {
    	list.add(num % 10)
    	list.add(num/10)
    }
    
    return list
}

fun strToTime(s:String): Int {
	if(s.startsWith("0")){
		val chars: Array<Char> = s.toCharArray().toTypedArray()
		return Character.getNumericValue(chars[1])
	}
	
	return s.toInt()
}

Count number of occurrences (or frequency) in a sorted array

Count number of occurrences (or frequency) in a sorted array
Expected time complexity is O(Logn)

– Example:


Input: arr[] = {1, 1, 2, 2, 2, 2, 3,},   x = 2
Output: 4 // x (or 2) occurs 4 times in arr[]

Solution -> Improved Binary Search
1) Use Binary search to get index of the first occurrence of x in arr[]. Let the index of the first occurrence be i.
2) Use Binary search to get index of the last occurrence of x in arr[]. Let the index of the last occurrence be j.
3) Return (j – i + 1);


import java.util.*

fun main() {
	var A = intArrayOf(1, 2, 2, 3, 4)
	println(solution(A, 2))
}

fun solution(A: IntArray, x: Int): Int {
	
	if (A.size == 0)
		return -1
	
	var leftOccur = binarySearchLeftMostOccurs(A, x, 0, A.size - 1)
	var rightOccur = leftOccur
	
	if (leftOccur == -1)
		return -1
	else {
		rightOccur = binarySearchRightMostOccurs(A, x, leftOccur, A.size - 1)
	} 
	
	return rightOccur - leftOccur + 1
}

fun binarySearchLeftMostOccurs(A: IntArray, x: Int, leftmost: Int, rightmost: Int): Int {

	if(leftmost == rightmost) {
		if (A[leftmost] == x) {
			return leftmost
		}
		
		return -1
	}
	
	var pivot = (leftmost + rightmost) / 2
	
	if(A[pivot] == x && ((pivot == leftmost) || (A[pivot - 1] < A[pivot]))) {
		return pivot
	}
	
	if(A[pivot] >= x){
		return binarySearchLeftMostOccurs(A, x, leftmost, pivot)
	}else {
		return binarySearchLeftMostOccurs(A, x, pivot + 1, rightmost)
	}
}

fun binarySearchRightMostOccurs(A: IntArray, x: Int, leftmost: Int, rightmost: Int): Int {
	if(leftmost == rightmost) {
		if (A[rightmost] == x) {
			return rightmost
		}
		
		return -1
	}
	
	var pivot = (leftmost + rightmost) / 2
	
	if(A[pivot] == x && ((pivot == rightmost) || (A[pivot] < A[pivot + 1]))) {
		return pivot
	}
	
	if(A[pivot] <= x){
		return binarySearchRightMostOccurs(A, x, pivot + 1, rightmost)
	}else {
		return binarySearchRightMostOccurs(A, x, leftmost, pivot)
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *