Searching and Sorting: Find the majority element in the array
Problem Statement: You are given an arrya of repeating elements. You need to find and print the majority element. An element that appears more tha n/2 times, is called as majority element. Example Input : {3, 3, 4, 2, 4, 4, 2, 4, 4} Output : 4 4 is repeated 5 times that is greater […]
Arrays: Find numbers with even number of digits
Problem Statement: You are given an array of integers. You need to return the count of the numbers that has even number of digits. Example Input arr = {12, 123, 1234, 12345, 123456} Output: 3 The numbers {12, 1234, 123456} has even digits. Solution The solution is very simple. Convert the number into string and […]
Arrays: Find unique numbers that sum upto zero
Problem Statement: You are given an integer n. You need to return the integers that sum upto 0. Example Input: n = 5 Output: {-5, -1, 1, 2, 3} Solution Method 1: In this method, we will use the numbers from 1, 2, .. n-1 and then negation. Method 2: Consider the cases below n […]
Arrays: Check If N and Its Double Exist
Problem Statement: You are given an array, you need to check if a number and its double exist in that array. Example Input arr = {10, 2, 5, 3} Output: True N = 5 M = 10. 5*2 = 10. So there exist 2 integers, such that the integer and its double exist. Solution In […]
Arrays: Check if 3 consecutive odds exist
Problem Statement: You are given an array, you need to check if there are 3 consecutive odds exist. If exist then return true else false. Example Input: arr = [2,6,4,1] Output: false Solution The solution is very simple. Just iterate throught the array and check if the element is odd, then increment the odd variable. […]
Strings: Check if Word Equals Summation of Two Words
Problem Statement: You are given 3 words. You need to check if the summation of the letters lead up to third word. Example Input: s1 = “acb”, s2 = “cba”, s3 = “cdb” Output: true s1 = “acb” -> 0 2 1 s2 = “cba” -> 2 1 0 s3 = “cdb” -> 2 3 […]
Strings: Longer Contiguous Segments of Ones than Zeros
Problem Statement: You are given a binary string, you need to return true if the longest contiguous sequence of 1s is greater than the longest contiguous sequence of 0. Example Input: s = “1101” Output: true Beause longest continious segment of 1 is 2, and 0 is 1. Solution The solution is very simple. We […]
Strings: Minimum Number of Swaps to Make the Binary String Alternating
Problem Statement: You are given a binary string of 0s and 1s. You need to get the minimum steps to make the string alternating. Example Input: s = “111000” Output: 1 Swap elements at s[1] and s[4] to make the string alternating. Solution In the solution, we count the number of characters that are misplaced […]
Strings: Longest Substring Of All Vowels in Order
Problem Statement: You are given a string, that contains only vowels and you need to get the longest substring that has all vowels “a’, ‘e’, ‘i’, ‘o’, ‘u’ in aplhabetical order. Example Input: word = “aeeeiiiioooauuuaeiou” Output: 5 Solution For this solution, we need to check for alphabets in increasing order of their vowels. We […]
Strings: Number of Different Integers in a String
Problem Statement: You are given a string that has numbers. Now you will replace the characters with space. Now count the number of distinct number. Example Input: word = “hello1234world234” Output: 2 Why? Replace char with space —–1234—–234 We get 2 distinct words. Solution Solution is very simple. We use unordered_Set, to store the distinct […]