Nutnicha
Nutnicha

Reputation: 285

How do I add every array in a list with every nth element in the array

I have a function public static void sortedlist(int[] l, int r) that takes an array int[] l and returns a new array where every non-negative element in the list would be added with every element until the rth element.

So here is an example.

Lets say we have l = {1, 2, -3, 4, 5, 4}, and r = 3. In this case, we would:

Replace l[0] with l[0] + l[1] + l[2] + l[3].
Replace l[1] with l[1] + l[2] + l[3] + l[4].
Not do anything to l[2] because it is negative.
Replace l[3] with l[3] + l[4] + l[5]. (We can't go further than the end of the array.)
Replace l[4] with l[4] + l[5].
Not change the value of a[5] because there are no values after l[5]. So the sum is l[5] itself.
Thus, the result after calling `sortedlist` would be {4, 8, -3, 13, 9, 4}.

Here is my code so far:

public class Practice2 {
    public static void sortedlist(int[] l, int r) {
        int[] A;
        int sum = 0;
        for (int i = 0; i < l.length + r; i+=r) {
            sum = sum +=
        }
    }
}

As you can see, I'm not done with the code because I'm stumped on how am I supposed to move forward from this point.

What I'm trying to do is create a new Array A and then add the new values that I've received from sum into Array A.

Any help would be greatly appreciated. Furthermore, if you could explain the logic behind a working code would be extremely beneficial for me as well.

Thank you :)

Upvotes: 0

Views: 436

Answers (2)

iamgirdhar
iamgirdhar

Reputation: 1155

Please find the solution below and i have also provided some explanation regarding the logic behind it.

Note: I have unit tested it for few cases and it seems working fine.

1) r is less than array length 2) r is equals to array length 3) r is greater than array length

public class Test {
  public static void main(String[] args) {
    int[] input = new int[]{1, 2, -3, 4, 5, 4};
    int r = 3;
    sortedlist(input,r);
  }

  public static void sortedlist(int[] l, int r) {
    List<Integer> list = new ArrayList<>();
    int itr = 0;
    for(int i = itr; i < l.length ; i++){//This loop is for iterating over the given array
      int temp = 0;
      int itr2 = Math.min(itr + r, l.length-1);//This function takes the minimum value and it helps when the (itr+r) > l.length, it will consider the (l.length-1)
      if(l[i] > 0){// checking whether the selected value is -ve or not
        for(int j = i; j <= itr2 ; j++){ // This loop is for calculating the addition over the selected range
          temp =  temp + l[j];
        }
      } else {// if it is-ve, assigning the same value to temp
        temp = l[i];
      }
      list.add(temp);// storing the calculated value in a list of integers
      itr++; // incrementing the main loop iterator 
    }
    System.out.println(list);
  }

}

Output:

[4, 8, -3, 13, 9, 4]

Upvotes: 1

user17233545
user17233545

Reputation:

Try this.

public static void sortedlist(int[] l, int r) {
    for (int i = 0, max = l.length; i < max; ++i)
        if (l[i] >= 0)
            for (int j = i + 1; j <= i + r && j < max; ++j)
                l[i] += l[j];
}

and

int[] a = {1, 2, -3, 4, 5, 4};
sortedlist(a, 3);
System.out.println(Arrays.toString(a));

output:

[4, 8, -3, 13, 9, 4]

Upvotes: 1

Related Questions