Discover the indices of elements that, when their values are added together, equal x

I have an array with elements structured like this:

var x = 17;
var arr = [{ value:2, quantity: 4 }, { value:8, quantity: 1 }, { value:3, quantity: 3 }];

How can I identify the indexes of elements that would add up to equal the x number? For example, in this case the desired output would be:

[1, 3, 3, 3]

The goal is to have the lowest length possible for the returned array, such as [0, 0, 0, 1, 2] or [0, 0, 0, 0, 2, 2, 2].

Answer №1

While there are more efficient methods available, the following solution is straightforward and clean. The approach taken treats this problem akin to a linear equation where the combo variable holds the coefficients for each value in the arr array:

// define initial x and arr values
var x = 17;
var arr = [{ value:2, quantity: 4 }, { value:8, quantity: 1 }, { value:3, quantity: 3 }];

// maximums[i] represents the maximum amount of arr[i]'s that can be present in any combination
var maximums = arr.map(function(item){ return Math.floor(x / item.value) });

// an array to store the current coefficients being tested. combo[i]
// corresponds to the coefficient for arr[i]
// all coefficients start at 0 and increment by 1 progressively
var combo = arr.map(function(){ return 0 });

// keep track of the current sum obtained with the coefficients
var sum = 0;

// iterate through the coefficients from left to right, stopping
// when a coefficient exceed its corresponding max value
function next() {
  for(var i = 0; i < combo.length; i++) {
    combo[i]++;
    // adjust the sum due to coefficient increase
    sum += arr[i].value;
    if(combo[i] <= maximums[i]) {
      // stop since coefficient is within range
      break;
    }else{
      // reached maximum for one coeff., reset it and update sum 
      if(i == combo.length-1) return false;
      sum -= arr[i].value * combo[i];
      combo[i] = 0;
    }
  }
  return true;
}

// find all combinations until sum matches x
while(next()) {
  if(sum == x) break;
}

// if no valid combination is found, halt execution
if(sum != x) {
  console.log('not possible');

// otherwise print the successful combination
}else{
  for(var i = 0; i < combo.length; i++) {
    if(combo[i] == 0) continue;
    console.log(combo[i] + ' of (' + JSON.stringify(arr[i]) + ')');
  }
}

If aiming for the smallest possible combination always, utilize this alternative function:

function coeffsUsed() {
  var count = 0;
  for(var i = 0; i < combo.length; i++) {
    if(combo[i] > 0) count++;
  }
  return count;
}

// iterate through all combinations to reach the target sum x
var smallestCombo = {};
var smallest = -1;
while(next()) {
  if(sum == x) {
    var count = coeffsUsed();
    if(smallest == -1 || count < smallest) {
      smallest = count;
      smallestCombo = combo.slice(0); // clone the array
    }
  } 
}

// if no optimal combination is found, exit
if(smallest == -1) {
  console.log('not possible');

// otherwise print the most efficient combination
}else{
  for(var i = 0; i < smallestCombo.length; i++) {
    if(smallestCombo[i] == 0) continue;
    console.log(smallestCombo[i] + ' of (' + JSON.stringify(arr[i]) + ')');
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a JavaScript file that will generate a map based on data from an SQL

I am currently working on developing a Leaflet map with numerous markers. To streamline the process of updating the map, I have stored all the markers in a MySQL database. A PHP script is utilized to retrieve the data from the database, format it in a way ...

Should one consider using the Express app.use() method within an asynchronous callback function?

Seeking advice on a recommended best practice. I recently developed an Express server that generates a JWT and sends it to the client whenever they hit an API endpoint. To maintain modularity, I separated the route handler into its own module and exporte ...

Transforming an SQL Query into JSON format using Oracle 11g in Oracle Application Express (APEX)

In my Oracle APEX v4.2 project, I am dealing with a sizable table containing about 40 columns and up to 50 rows. My goal is to use SQL to fetch the data from this table and convert each row into a JSON object. Operating on Oracle 11gR2, I require this JSO ...

What are some creative ways to incorporate a variety of images into my website?

Currently working on a webpage and running into an issue. I have a div called "background" where I am loading several images using the <img>-tag. The problem is that the images are set to float:left;, causing them to wrap onto a new line as they can& ...

How to enhance and expand Material-UI components

I am trying to enhance the Tab component using ES6 class in this way: import React from "react"; import {Tab} from "material-ui"; class CustomTab extends Tab { constructor(props){ super(props); } render(){ return super.rende ...

The back button functionality in Android cannot be altered or overridden using JavaScript

Hey everyone, I'm currently in the process of developing a mobile application using phonegap. I'm facing an issue where I want to disable the functionality of the back button from navigating to the previous page. I simply want it to do nothing or ...

Is it possible to establish multiple connections simultaneously using Stomp?

I have been utilizing the ng2-stomp-service in my Angular application. Is it advisable to set up multiple connections (not just multiple subscriptions)? In the past, I have always seen a single connection being established, with several subscriptions made ...

Comparing functions and function issues when using onClick in JavaScript

I have successfully created an Image Element on my web page dynamically using JavaScript. I am now trying to set an onClick event for the newly created image element. However, I am encountering a problem and I cannot figure out why?, This is the code I ...

What could be causing the Angular form to refresh the entire page?

Two different forms are displayed on the same page: <form ng-submit="actionA()"> <input type="text"> <input type="submit" value="Submit"> </form> <form ng-submit="actionB()"> <input type="text"> <inp ...

Add owl carousel to your npm project in any way you see fit

After struggling for a while, I finally wanted to implement owl-carousel, but couldn't figure out how to connect it using npm and webpack. The official NPM website states: Add jQuery via the "webpack.ProvidePlugin" to your webpack configuration: ...

What are the potential causes of receiving the error message "No Data Received ERR_EMPTY_RESPONSE"?

I often encounter this issue on my website, especially when I have a thread open. It seems to happen whenever I am actively checking for new posts and notifications via Ajax every 10 seconds or so. However, I'm not sure if this continuous reloading is ...

Fixed position scrollable tabs with Material UI

I recently implemented material-ui scrollable tabs in my project, referencing the documentation at https://mui.com/material-ui/react-tabs/#scrollable-tabs. Here is a snippet of the code I used: <Tabs value={value} onChange={handleChange ...

javascript - convert a JSON string into an object without using quotation marks

Consider the following example: var mystring = `{ name: "hello", value: 1234 }` var jsonobj = JSON.parse(mystring) The code above will not output anything because the "name" and "value" keys are missing quotes. How can I parse this strin ...

Retrieving the value of an ASP.NET Literal in JavaScript

Is there a way to retrieve the value of an ASP.NET Literal control in JavaScript? I attempted the code below but it didn't return any values: var companyName = document.getElementById('litCompanyName').textContent; var companyNumber = docum ...

Exploring the capabilities of storing and retrieving nested objects within a React database

I'm having trouble retrieving nested items from my database. This is how my database is structured: GET /dwelling/room/ [ { "room_id": 1, "room_name": "Living Room", "room_data": [ { "id": 1, ...

Why won't the JavaScript work when linking to a carousel slide from another page?

Trying to follow this 6-year-old guide, but my JavaScript isn't triggering when the URL contains #slide_ - have things changed? Linking to a specific Bootstrap carousel slide from another page My code on page 2: <!doctype html> <html> &l ...

Experiencing an unexpected abundance of console logs when implementing React Hooks

I am attempting to retrieve data from an API. The desired result is being obtained, but when I attempt to log it to the console, it logs 4 times. Within my app.js, I am utilizing the fetchData function: import React, {useEffect, useState} from 'rea ...

Is there a specific side effect that warrants creating a new Subscription?

Recently, I had a discussion on Stack Overflow regarding RxJS and the best approach for handling subscriptions in a reactive application. The debate was whether it's better to create a subscription for each specific side effect or minimize subscriptio ...

Developing a Prototype for an Angular Directive

After following instructions from a question on Stack Overflow, I have updated my application configuration with the code snippet below: $provide.decorator('formDirective', function($delegate) { var directive = $delegate[0]; directive.contro ...

Tips on displaying information following the parsing of JSON

Trying to retrieve a list of users using this code snippet <div id="users" data-users='[{"name":"one","userName":"user_one"}, {"name":"two","userName":"user_two&q ...