Find K Maximum Elements of Array in Original Order in C++

sudhir sharma
Updated on 25-Jan-2021 04:52:14

194 Views

In this problem, we are given an array arr[] of n elements. Our task is to find k maximum elements of the array in original order. We need to find k maximum elements of the array and then print then as they were indexed originally.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 3, 6, 2}, k  = 2Output: 5, 6Explanation: The largest two elements of the array are 6 and 5. But 5 comes before 6 in the original array hence we have printed in that way.Solution ApproachTo solve the problem, and print k elements in the original order.For ... Read More

Find Integers That Divide Maximum Number of Elements in Array in C++

sudhir sharma
Updated on 25-Jan-2021 04:51:49

439 Views

In this problem, we are given an array arr[] of n integers.Our task is to find integers that divides maximum number of elements of the array. Problem Description: We need to find a number p which can divide the maximum number of elements of the array. In case, there are more than one such element we will return the smaller one.Let’s take an example to understand the problem,  Input: arr[] = {4, 5, 6, 7, 8}Output: 2Explanation: The element 2 divides {4, 6, 8}.Solution ApproachA simple solution to the problem is by looping through the array and then for each element of the array, divide each ... Read More

Find K Items with the Lowest Values in C++

sudhir sharma
Updated on 25-Jan-2021 04:51:25

151 Views

In this problem, we are given a list that consists of items and their values and an integer k. Our task is to find K items with the lowest values. Problem description: We need to find k items from the list that have the lowest value.Let’s take an example to understand the problem, Input: item-value = { {item1, 200}, {item2, 100}, {item3, 500}, {item4, 400}} k = 2Output: item1 , item2Explanation: Two elements with least value are item1 with 200 and item2 with 100.Solution ApproachA solution to the problem is by finding k items with the least value greedily. We will first sort the item ... Read More

Largest Substring Between Two Equal Characters in a String in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:55:06

225 Views

We are required to write a JavaScript function that takes in a string of characters as the only argument.The function should find that longest string which is sandwiched between two identical characters and return its length.For example −If the input string is −const str = 'sadtrsewak';Then the output should be −const output = 6;because between two 'a' we have the longest desired substring of length 6.ExampleFollowing is the code −const str = 'sadtrsewak'; const longestSubstringBetween = (str = '') => {    const map = {};    let res = -1;    for(let i = 0; i < str.length; i++){ ... Read More

Check String Uniqueness in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:51:19

215 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should return true if all the characters present in the string are unique. And if even one character appears for more than one, the function should return false.We will use a hash set to keep track of the characters we encounter in the string and if at any stage of iteration, we encounter duplicate characters, we will return false otherwise at the end of iteration, we will return true.ExampleFollowing is the code −const str = 'abschyie'; const checkUniqueness ... Read More

Maximum Product of Two Numbers in a List of Integers in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:48:39

296 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.The function should find the maximum product that can be achieved multiplying any two elements of the array. The condition for us is that we have to do this in linear time and constant space.For example −If the input array is −const arr = [3, 9, 2, 1, 0];Then the output should be −const output = 27;because it’s the greatest product and can be achieved by multiplying 3 and 9.ExampleFollowing is the code −const arr = [3, 9, 2, ... Read More

Check If a String is Repeating in Itself in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:44:45

846 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument.The function should detect if the string is a repetition of a same set of characters or not.If it is a repetition of the same set of characters then we should return true, false otherwise.For example −If the input string is −const str = 'carcarcarcar';Then the output should be −const output = true;because the string 'car' is getting repeated over and over again in the string.ExampleFollowing is the code −const str = 'carcarcarcar'; const isRepeating = (str = '') => { ... Read More

Count Palindromes Constructed from a String in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:42:58

366 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument.The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count.For example −If the input string and the number is −const str = 'ij'; const num = 4;Then the output should be −const output = 4;because those four possible palindrome strings are −'iiii', 'jjjj', 'ijji', 'jiij'Approach:We will first count the number of ... Read More

Returning Acronym Based on a String in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:38:41

1K+ Views

We are required to write a JavaScript function that takes in a string of characters as the only argument.The function should build and return the acronym based on the string phrase provided as input.While constructing the acronym the function should take only those words into consideration that starts with an uppercase letter.For example −If the input string is −const str = 'Polar Satellite Launch Vehicle';Then the output should be −const output = 'PSLV';ExampleFollowing is the code −const str = 'Polar Satellite Launch Vehicle'; const buildAcronym = (str = '') => {    const strArr = str.split(' ');    let res ... Read More

Counting Largest Numbers in Row and Column in 2D Array in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:36:48

447 Views

We are required to write a JavaScript function that takes in a two-dimensional array of integers as the only argument.The task of our function is to calculate the count of all such integers from the array that are the greatest both within their row and the column.The function should then return that count.For example −If the input array is −const arr = [    [21, 23, 22],    [26, 26, 25],    [21, 25, 27] ];Then the output should be −const output = 3;because those three numbers are 26, 26, 27ExampleFollowing is the code −const arr = [    [21, ... Read More

Advertisements