We are given with a string of 0’s and 1’s only. The string represents a binary number read from left to right. i.e. 001 is 4 and not 1. The goal is to find all substrings that represent an even decimal number.We will do this by checking the first value of all substrings, if it is 0 then number is even if 1 then number will be odd. Increment count by length-i as all substrings with this sbstr[0]=’0’ will be even in decimal.Let us understand with examples.Input − str=”101”Output − Count of even decimal value substrings in a binary string ... Read More
We are given with four arrays. Goal is to find quadruplets of elements from the four arrays that have sum equal to a given Sum value. The elements selected should be such that all 4 elements belong to different arrays.We will do this by traversing all arrays using for loop and check if A[i]+B[j]+C[k]+D[l]==sum. If yes increment count.Let us understand with examples −Input −A[]={ 1, 3, 1}, B[]={ 2, 4, 5 } , C[]={ 1, 1, 2 } , D[]= { 4, 4, 0} Sum=5Output − Count of quadruplets with given sum are − 2Explanation −2 quadrutplets are: (A[0], B[0], ... Read More
We are given a graph containing nodes and edges. The goal is to find the maximum number of possible nodes that are connected to any edge of the graph. We know that no. of nodes will always be less than or equal to the number of edges in a complete graph.We will do this by trying to make a complete graph where the number of nodes is n then there will be n(n-1)/2 edges.edge=n(n-1)/2 (here n for nodes )2*edge=n(n-1). Once n(n-1)> no. of edges then we have extra nodes. So iterate from i=1 to i=n.Till i(i-1)>2*edge. Return n-i as result.Let ... Read More
We are given an array of integers. The goal is to count numbers such that the absolute difference between the sum of all elements and that element is greater than variable k.We will do this by getting the sum of elements of the array. Now for each element arr[i], check if −sum-2(arr[i])>k, as sum already includes arr[i] so subtract it twice. If true increment count.Let’s understand with examples.Input − arr[]= { 1, 2, 3, 0, 3, 2, 0, 1 }, k=10Output − Count of elements: 2Explanation − Sum of elements is 1212-1-1=10, 12-2-2=8, 12-3-3=6, 12-0-0=12.Only 12 > 10, so for ... Read More
We are given an array of positive integers. The goal is to find the count of numbers that can be made power of two by adding 1 to them utmost once.We will check using log2(i) that number is power of two or can become power of two by adding 1 to it. If yes increment count.Let’s understand with examples.Input − arr[]= {1, 3, 2, 5, 6 }, Output − Count of numbers that can become power of 2: 3Explanation − 1+1=2 → 21 , 3+1=4 → 22 , 2=21 others will become 5+1=6, 6+1=7Input − arr[]= {2, 4, 8, 16 ... Read More
We are given an array of consecutive numbers of length n. The array has only one number which is repeated more than once. The goal is to get the number of times that element is repeated in the array. Or we can say find the length of a repeated element in the array.We will traverse the array from i=0 to i
We are given a number N. The goal is to count the numbers upto N that are perfect squares as well as perfect cubes. For example, 1, 64 are both perfect squares and perfect cubes.We will use sqrt() to calculate square root and cbrt() to calculate cube root of a number.Let’s understand with examples.Input − N=100Output − Count of numbers that are perfect squares and cubes − 2Explanation − 1 and 64 are only numbers from 1 to 100 that are both perfect squares and cubes.Input − N=5000Output −Count of numbers that are perfect squares and cubes − 3Explanation − ... Read More
We are given an interval [first, last]. The goal is to find the count of numbers that have a unit digit k and lie between range [first, last].We will do this by traversing from i=first to i=last. For each number i compare its unit digit with the k, if they are the same increment the count.Let’s understand with examples.Input − first=8 last=40 , k=8Output − Count of numbers with unit digit k − 4Explanation −Numbers between 8 and 40 with unit digit = 8 8, 18, 28, 38Input − first=100 last=200 , k=9Output − Count of numbers with unit digit ... Read More
We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns the number that appears for more than (n/2) number of times where n is the length of the array. If there is no such element in the array, our function should return falseLet's write the code for this function −Exampleconst arr = [12, 5, 67, 12, 4, 12, 4, 12, 6, 12, 12]; const arr1 = [3, 565, 7, 23, 87, 23, 3, 65, 1, 3, 6, 7]; const findMajority = arr => { let maxChar = -Infinity, maxCount ... Read More
We are required to write a JavaScript function that takes in two arrays of Numbers, say first and second and checks for their equality.Equality in our case will be determined by one of these two conditions −The arrays are equal if they contain the same elements irrespective of their order.If the sum of all the elements of the first array and second array is equal.For example −[3, 5, 6, 7, 7] and [7, 5, 3, 7, 6] are equal arrays [1, 2, 3, 1, 2] and [7, 2] are also equal arrays but [3, 4, 2, 5] and [2, 3, ... Read More