Problem:
Given n>=0, create an array with the pattern {1, 1, 2, 1, 2, 3, ... 1, 2, 3 .. n} (spaces added to show the grouping). Note that the length of the array will be 1 + 2 + 3 ... + n, which is known to sum to exactly n*(n + 1)/2.
seriesUp(3) → {1, 1, 2, 1, 2, 3}
seriesUp(4) → {1, 1, 2, 1, 2, 3, 1, 2, 3, 4}
seriesUp(2) → {1, 1, 2}
Solution:
public int[] seriesUp(int n) { int[] result = new int[n * (n + 1) / 2]; int i = 0; for (int j = 1; j <= n; ++j) for (int k = 1; k <= j; ++k) result[i++] = k; return result; }
Here's my solution:
ReplyDeletepublic int[] seriesUp(int n) {
int[] arr = new int[n*(n+1)/2];
int m = 2;
for(int i=0, j=1;i=m){
j=1;
m++;
}
}
return arr;
}
I can't get this to compile, Kyle.
DeleteMy solution, with one loop:
ReplyDeletepublic int[] seriesUp(int n) {
int[] result = new int[n*(n+1)/2];
for (int i=0,j=1,k=1; i<n*(n+1)/2&j<=n; k++){
if(i==j*(j+1)/2){k=1;j++;}
result[i++] = k;
}
return result;
}
public int[] seriesUp(int n) {
ReplyDeleteint[] nums = new int[n*(n+1)/2];
int z = 1; //max number in pattern
int i = 0; //starting index
while(i <= nums.length - 1){
for(int j = 1; j <= z; j++){
nums[i] = j;
i++;
}
z++;
}
return nums;
}
public int[] seriesUp(int n) {
ReplyDeleteint len = n*(n+1)/2;
int[] res = new int[len];
int count = 0;
for(int i=0; i<len; i++)
{
for(int b=1; b<=n; b++)
{
if(count==0){
res[i]=b;
i++;
}
if(i==b*(b+1)/2)
count=1;
}
i--;
count=0;
}
return res;
}
public int[] seriesUp(int n) {
ReplyDeleteint[] res=new int[n*(n+1)/2];
for (int i=0; i<n; i++)
{
for (int j=0; j<=i; j++)
{
res[i*(i+1)/2+j]=j+1;
}
}
return res;
}
public int[] seriesUp(int n) {
ReplyDeleteint size = n * (n + 1) / 2;
int[] array = new int[size];
int counter = 0;
int limit = 1;
for (int i = 0; i < size; i++) {
array[i] = ++counter;
if (counter == limit) {
counter = 0;
limit++;
}
}
return array;
}
public int[] seriesUp(int n) {
ReplyDeleteint[] result = new int[n*(n+1)/2];
int limit = 1;
for(int i=0, j=1;ilimit){
j=1;
limit++;
}
}
return result;
}
public int[] seriesUp(int n) {
ReplyDeleteint arr[] = new int[n*(n+1)/2];
for(int i = 0,j = 0;j<n;j++,i+=j)
{
for(int it = i,c = 1;c<=j+1;it++,c++)arr[it]+=c;
}
return arr;
}
public int[] seriesUp(int n) {
ReplyDeleteint[] arr = new int[n*(n+1)/2];
int k=0;
for(int i=0; i<n*(n+1)/2; i+=k) {
for(int j=0; j<n; j++) {
arr[i+j] = j+1;
}
k++;
}
return arr;
}
public int[] seriesUp(int n) {
ReplyDeleteint[] arr = new int[n*(n+1)/2];
int size = n*(n+1)/2;
for(int i=size-1, j=n, sub=1; i>=0; i--,j--){
if(j==0) {
j=n-sub;
sub++;
}
arr[i] = j;
}
return arr;
}
public int[] seriesUp(int n) {
ReplyDeleteint[] arr = new int[n*(n+1)/2];
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
arr[i*(i+1)/2 + j] = j + 1;
return arr;
}
public int[] seriesUp(int n) {
ReplyDelete//Recursive Solution Below!//
if (n == 0)
{
return new int[0];
}
int counter = 1;
int [] sq = seriesUp(n - 1);
int [] sq2 = new int [sq.length + n];
for (int i = 0; i < sq2.length; i++)
{
if (i < sq.length)
{
sq2[i] = sq[i];
}
else
{
sq2[i] = counter;
counter++;
}
}
return sq2;
}
public int[] seriesUp(int n) {
ReplyDeleteint[] arr = new int[n*(n+1)/2];
for(int i = 1; i<=n; i++) {
int j = 1;
for(int p = i*(i-1)/2; p<i*(i+1)/2; p++) {
arr[p] = j;
j++;
}
}
return arr;
}
My idea is to divide the array into n group with increasing length (1,2,3,4,....n). In each group, I start with 1 at p = i*(i-1)/2 index and increment the value of subsequent elements all the way through p<i*(i+1)/2
public int[] seriesUp(int n) {
ReplyDeleteint size=n*(n+1)/2;
int k=0;
int[]arr=new int[size];
for(int i=1;i<=n;i++)
{
int count=1;
for(int j=1;j<=i;j++)
{
arr[k++]=count;
count+=1;
}
}
return arr;
With Java Stream
ReplyDeletepublic int[] seriesUp(int n) {
return (java.util.stream.IntStream.rangeClosed(1, n)
.flatMap(i -> java.util.stream.IntStream.rangeClosed(1, i)))
.toArray();
}
int[] arr = new int[n*(n+1)/2];
ReplyDeletefor(int i = 0, k = 0; i0; j--) {
arr[arr.length-1-k] = j;
k++;
}
}
return arr;
public Integer[] seriesUp(int n) {
ReplyDeleteStack arr=new Stack();
while(n!=0){
for(int i=n;i>0;i--){
arr.push(i);
}
n--;
}
Integer[] newarr = new Integer[arr.size()];
//stack to array conversion
for (int i = 0; i < newarr.length; i++)
newarr[i] = arr.pop();
return newarr;
}
public static int[] seriesUp(int n) {
ReplyDeleteint arr[]=new int[n*(n+1)/2];
int count=0;
for(int i=0; i<n;i++) {
for(int j=0; j<=i; j++) {
arr[count]=j+1;
count++;
}
}
return arr;
}
//Solution with 1 for loop
ReplyDeletepublic int[] seriesUp(int n)
{
int[] result = new int[n*(n+1)/2];
int pos = 0;
for (int i = 0, j = 1, k = 1; i < result.length; i++)
{
if (i == pos) {
j++;
pos = ((j*j)-j)/2;
k = 1;
}
result[i] = k++;
}
return result;
}