Problem Statement This problem is from Leetcode – Max Consecutive Ones. The problem statement is as below: Given a binary array nums, return the length of the longest run of consecutive 1’s in the array. Solution Initialize variables Iterate Though the Array You can checkout the code from Github here: Max Consecutive Ones. See the […]
Number Complement – Leetcode #476
Problem Statement This problem is from Leetcode – Number Complement. The problem statement in short is as below: Given an integer num, return its binary complement by flipping all 0s to 1s and all 1s to 0s in its binary representation. Solution This problem is simple enough. We first compute the binary of the given […]
Assign Cookies – Leetcode #455
Problem Statement This problem is from Leetcode – Assign Cookies. The problem statement in short is as below: Given two arrays g (children’s greed factors) and s (cookie sizes), assign each child at most one cookie such that s[j] >= g[i] to satisfy the child. Return the maximum number of content children. Solution You can […]
Find All Numbers Disappeared in an Array – Leetcode #448
Problem Statement This problem is from Leetcode – Find All Numbers Disappeared in an Array. Given an array nums of n integers, where each nums[i] is in the range [1, n], find and return all integers in the range [1, n] that are missing from nums. Solution The solution to this problem is simple enough. […]
How many ways to express a natural number N as sum of two or more consecutive natural numbers
Problem Statement This problem is asking us to find out the number of ways a natural number N can be expressed as a sum of two or more consecutive natural numbers. For example: 5 can be expressed as 2+3. This gives us the count as 1. 9 can be expressed as 4+5 and 2+3+4. This […]
Longest Uniform Sub-String
Problem Statement Given a string, find the longest uniform sub-string in it. Return the repeating character and the number of times it repeats. Solution Let us take an example string like “abcdddss“. The question is asking us to return the longest sub-string with the same characters. In this case the longest uniform sub-string is “ddd”. […]
First Non-Repeating Character
Problem Statement Given an input string, find the first non-repeating character in it. Solution Let us take an example string like “aabccdeff“. The question is asking us to return the first character that does not repeat. In this case the first non-repeating character is “b“ Approach Declare a hashmap with Character as key and Boolean […]