Merge Sort: Counting Inversions
In an array, arr , the elements at indices i and j (where i < j ) form an inversion if arr[ i ] > arr[ j ]. In other words, inverted elements arr[ i ] and arr[ j ] are considered to be "out of order". To correct an inversion, we can swap adjacent elements. Function Description Complete the function countInversions in the editor below. countInversions has the following parameter(s): int arr[n]: an array of integers to sort Returns int: the number of inversions Input Format
View Solution →Strings: Making Anagrams
A student is taking a cryptography class and has found anagrams to be very useful. Two strings are anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency. For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not. The student decides on an encryption scheme that involves two large strings. The encryption is dependent on the minimum number of char
View Solution →Alternating Characters
You are given a string containing characters A and B only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string. Your task is to find the minimum number of required deletions. Function Description Complete the alternatingCharacters function in the editor below. alternatingCharacters has the following parameter(s): string s: a string Returns int: the minimum num
View Solution →Sherlock and the Valid String
Sherlock considers a string to be valid if all characters of the string appear the same number of times. It is also valid if he can remove just 1 character at 1 index in the string, and the remaining characters will occur the same number of times. Given a string s, determine if it is valid. If so, return YES, otherwise return NO. Function Description Complete the isValid function in the editor below. isValid has the following parameter(s): string s: a string Returns string: eit
View Solution →Special String Again
A string is said to be a special string if either of two conditions is met: All of the characters are the same, e.g. aaa. All characters except the middle one are the same, e.g. aadaa. A special substring is any substring of a string which meets one of those criteria. Given a string, determine how many special substrings can be formed from it. Function Description Complete the substrCount function in the editor below. It should return an integer representing the number of special
View Solution →