Question

In: Computer Science

//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know...

//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know how to check for unique elements

Given an array of integers check if it is possible to partition the array into some number of subsequences of length k each, such that:

  • Each element in the array occurs in exactly one subsequence
  • For each subsequence, all numbers are distinct. Elements in the array having the same value must be in different subsequences

If it is possible to partition the array into subsequences while satisfying the above conditions, return “Yes”, else return “No”. A subsequence is formed by removing 0 or more elements from the array without changing the order of the elements that remain. For example, the subsequence of [1,2,3] are [], [1], [2], [3], [1,2], [2,3], [1,3], [1, 2, 3].

Example

k = 2

numbers = [1, 2, 3, 4]

The array can be portioned with elements [1, 2] as the first subsequence, and elements [3, 4] as the next subsequence. Therefore, return “Yes”.

Example 2

k = 3

numbers = [1, 2, 2, 3]

There is no way to partition the array into subsequences such that all subsequences are of length 3 and each element in the array occurs in exactly one subsequence. Therefore return “No”.

Function Description

Complete the function partitionArray in the editor below. The function has to return one string denoting the answer.

partitionArray has the following parameters:

int k: an integer

int numbers[n]: an array of integers

function partitionArray(k, numbers) {

     //Write your code here

}

Solutions

Expert Solution

Working code implemented in JavaScript and appropriate comments provided for better understanding.

Source Code:

// Array Partitioning

// Given Array
// check if possible to partion into subsequences of
// length k each
// each element in array occurs exactly 1 subsequence
// all numbers in subsequence are distinct
// elements in the array having same value must be
// in different subsequences

// if possible to partition array satisfying all conditions above
// return 'yes', else return 'no'

// ex:
// n = 4 numbers in array
// length of each subsequence needs to be
// k = 2
// arr = [1,2,3,4]
// one possible way
// choose first 2 elements of array ==> [1,2] as first subsequence
// next 2 elements of array => [3,4] next subsequence
// so output === 'yes'

// ex2:
// n = 4
// k = 3
// arr = [1,2,2,3]
// no way to partition array where each subsequence is length of 3
// and each element in array occurs only once
// so output === 'no'

function partitionArray(k, numbers) {
console.log('k:', k)
console.log('numbers', numbers)
const arrLen = numbers.length
console.log('arrLen', arrLen)
var arr1 = []
var arr2 = []
// if arr can by evenly divided k times proceed
if (arrLen % k === 0) {
console.log('yes')
return 'Yes'
} else {
// else return 'no'
console.log('no')
return 'No'
}
}

// Input:
// 2 - number of partitions
// 4 - length of array
// 3 - array[0]
// 5 - array[1]
// 3 - array[2]
// 2 - array[3]

// k = 2
// numbers = [ 3, 5, 3, 2 ]
partitionArray(2, [1, 2, 3, 4]) // output 'Yes'
partitionArray(3, [1, 2, 2, 3]) // output 'No'

Sample Output Screenshots:


Related Solutions

I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
I am trying to solve this problem, but I don't know which test to use: For...
I am trying to solve this problem, but I don't know which test to use: For average risk funds only, test the proportion of funds with Sales Charges is not 50% use α = 0.10. Explain your conclusion. Here is the contingency table with the data: Sales charge Yes No total Risk Low 6 8 14 Avg 42 34 76 High 24 23 47 Total 72 65 137 Context of the problem: Mutual funds are the most common way people...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain why? Thank you A linear total cost curve that passes through the origin implies that a.         average cost is constant and marginal cost is variable. b.         average cost is variable and marginal cost is constant.             c.         average and marginal costs are constant and equal.             d.         you need more information to answer question.
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I am trying to get this code to work but I am having difficulties, would like...
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible package edu.hfcc; /* * Create Java application that will create Fruit class and Bread class * * Fruit class will have 3 data fields name and quantity which you can change. * The third data field price should always...
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
Javascript array of objects: I'm trying to get the input into an array of objects, and...
Javascript array of objects: I'm trying to get the input into an array of objects, and then display the information in a table using a for loop, but I don't think my path is right <!DOCTYPE html> <head> <title>Form</title> <meta charset="utf-8" /> <style media="screen"> h1 { text-align: center; } div { background-color: #pink; border: 2px solid green; padding: 15px; margin: 65px; } </style> <script> var list = []; total = 0; text = ""; function Product(item, quantity, costs){ this.item =...
PHP Language I have XAMPP and I don't know how to display my PHP code. For...
PHP Language I have XAMPP and I don't know how to display my PHP code. For instance, when I create a basic HTML webpage I just open the code into a web browser and it shows me a basic web page. When I try to show a PHP calculation it doesn't show. What are the steps in displaying my PHP doc?
I am trying to determine the weight of long-term debt but don't know where to begin....
I am trying to determine the weight of long-term debt but don't know where to begin. I know the answer is 22.58% but I don't know how to get there.   This is the problem: Problem 9-17 WACC Estimation The following table gives the balance sheet for Travellers Inn Inc. (TII), a company that was formed by merging a number of regional motel chains. Travellers Inn: (Millions of Dollars) Cash $10 Accounts payable $10 Accounts receivable 20 Accruals 10 Inventories 20...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT