Get Max Value Per Key in a JavaScript Array

AmitDiwan
Updated on 09-Oct-2020 11:12:28

490 Views

Suppose, we have an array of objects like this −const arr = [    {a:1, b:"apples"},    {a:3, b:"apples"},    {a:4, b:"apples"},    {a:1, b:"bananas"},    {a:3, b:"bananas"},    {a:5, b:"bananas"},    {a:6, b:"bananas"},    {a:3, b:"oranges"},    {a:5, b:"oranges"},    {a:6, b:"oranges"},    {a:10, b:"oranges"} ];We are required to write a JavaScript function that takes in one such array and returns an array of objects.The array should contain an object for each unique value of "b" property where the "a" property has the highest value.The code for this will be −const arr = [    {a:1, b:"apples"},    {a:3, ... Read More

Finding Transpose of a 2-D Array in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:10:00

267 Views

We are required to write a JavaScript function that takes in a two-dimensional array and returns its transposed array.The code for this will be −Method 1: Using Array.prototype.forEach()const arr = [    [0, 1],    [2, 3],    [4, 5] ]; const transpose = arr => {    const res = [];    arr.forEach((el, ind) => {       el.forEach((elm, index) => {          res[index] = res[index] || [];          res[index][ind] = elm;       });    });    return res; }; console.log(transpose(arr));Method 2: Using Array.prototype.reduce()const arr = [   ... Read More

Hexadecimal Color to RGB Color in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:08:01

248 Views

We are required to write a JavaScript function that takes in a hexadecimal color and returns its RGB representation.The function should return an object containing the respective values of red green and blue color −For example:hexToRgb('#0080C0') should return 0, 128, 192The code for this will be −const hex = '#0080C0'; const hexToRGB = hex => {    let r = 0, g = 0, b = 0;    // handling 3 digit hex    if(hex.length == 4){       r = "0x" + hex[1] + hex[1];       g = "0x" + hex[2] + hex[2];       ... Read More

RGB Color to Hexadecimal Color in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:05:17

362 Views

We are required to write a JavaScript function that takes in a RGB color and returns its hexadecimal representation.The function should take in an object containing three numbers representing the respective values of red green and blue color.For example:rgbToHex(0, 128, 192) should return '#0080C0'The code for this will be −const rgbColor = {    red: 0,    green: 51,    blue: 155 } function rgbToHex({    red: r,    green: g,    blue: b }) {    const prefix = '#';    const hex = prefix + ((1

Maximum Value K for Array Elements in C++

Ayush Gupta
Updated on 09-Oct-2020 09:48:49

350 Views

In this problem, we are given an array arr. Our task is to create a program to find the Maximum value K such that the array has at-least K elements that are >= K in C++.Problem DescriptionWe need to find a value K, that fulfills the condition that there are K or more elements in the array that are greater than or equal to K.Let’s take an example to understand the problem, Input:arr[] = {3, 5, 1, 7, 6, 6, 4, 8}Output:5ExplanationElements in the array that are greater than or equal to 5: 5, 6, 6, 7, 8.Solution ApproachA simple ... Read More

Maximum Value of an Integer for Factorial Calculation in C++

Ayush Gupta
Updated on 09-Oct-2020 09:47:58

258 Views

In this problem, we need to create a program to find Maximum value of an integer for which factorial can be calculated on a machine in C++.Factorial of a number is a huge value, as it is the product of all values preceding it. And C++ can handle large values only upto a certain value by using its inbuilt function. We need to find this restriction.Solution ApproachWe will simply use the property of data types which is when the numbers exceed the maximum value a negative number is returned.We will use long long int which is the largest basic data ... Read More

Maximum Weight Difference in C++

Ayush Gupta
Updated on 09-Oct-2020 09:46:05

285 Views

In this problem, we are given an array arr[] and a number M. Our task is to create a program to calculate the Maximum Weight Difference in C++.Problem StatementWe will find M elements from the array such that the absolute difference between the sum and the sum of rest elements is maximum.Let’s take an example to understand the problem, Input: arr[] = {3, 1, 6, 9, 4} M = 3Ouput:15ExplanationWe will consider 4, 6, 9. The sum is 19. The absolute difference with the sum of rest numbers is|19 - 4| = 15Solution ApproachThe solution to the problem is based ... Read More

Queries for Characters in a Repeated String in C++

Ayush Gupta
Updated on 09-Oct-2020 09:43:56

217 Views

In this problem, we are given a string str and Q queries consisting of two values a and b. Our task is to create a program to solve Queries for characters in a repeated string in C++.Problem DescriptionTo solve each query, we need to check whether the characters at index a and b are the same and return the value accordingly.Let’s take an example to understand the problem, Input: str = “tutorialspoint”Q = 2Query = {{0, 2}, {4, 7}}Output:RepeatedNot RepeatedExplanationFor Query 1, the character at index 0 is t, and the character at index 2 is t. Both are the ... Read More

Maximum Value with Choice of Dividing or Considering in C++ Program

Ayush Gupta
Updated on 09-Oct-2020 09:34:55

250 Views

In this problem, we are given a number N. Our task is to create a program to find to Maximum value with the choice of either dividing or considering as it is in C++.Problem DescriptionTo find the maximum, we can consider any two values, either by take the value as it is or we could get the maximum value by dividing.The value could be extracted as F(N/2) + F(N/3) + F(N/4) + F(N/5).Let’s take an example to understand the problem, Input:N = 8Output:9ExplanationF(8) =F(8/2) + F(8/3) + F(8/4) + F(8/5) = F(4) + F(2) + F(2) + F(1) = 4 ... Read More

Maximums from Array When the Maximum Decrements After Every Access in C++

Ayush Gupta
Updated on 09-Oct-2020 09:33:33

119 Views

In this problem, we are given an array arr[]and an integer M. Our task is to create a program to find Maximums from array when the maximum decrements after every access in C++.Problem DescriptionTo find the maximum, we will find the maximum element from the array and after every retrieval and decrease it by -1, M times.Let’s take an example to understand the problem, Input: arr[] = {3, 6, 8, 9} M = 2Ouput:17Explanation1st iteration, maximum = 9, sum = 9, updated arr = {3, 6, 8, 8}2nd iteration, maximum = 8, sum = 9+8 = 17, updated arr = ... Read More

Advertisements