id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

copies


Write a method named copies that accepts an array of integers a as a parameter and returns a new array a2, with each element value n from a replaced by n consecutive copies of the value n at the same relative location in the array. For example, if an array named a stores the following element values: {3, 5, 0, 2, 2, -7, 0, 4} Then the call of int[] a2 = copies(a); should return a new array a2 containing the following elements. The idea is that the value 3 was replaced by three 3s; the 5 was replaced by five 5s; and so on. {3, 3, 3, 5, 5, 5, 5, 5, 2, 2, 2, 2, 4, 4, 4, 4} Any element whose value is 0 or negative should not be kept in the returned array (as with 0 and -7 above). The array you return must have a length that is exactly long enough to fit its elements. For example, the result array a2 above contains 3 + 5 + 2 + 2 + 4 = 16 total elements, so the returned array's length must be exactly 16 in that case.


copies([1, 2, 3, 4]) → [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
copies([-9, 0, 3, 5]) → [3, 3, 3, 5, 5, 5, 5, 5]
copies([2, 1, -7, 6]) → [2, 2, 1, 6, 6, 6, 6, 6, 6]

...Save, Compile, Run (ctrl-enter)

public int[] copies(int [] a){ }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200

Copyright Nick Parlante 2017 - privacy