SUM OF INTERVALS IN A SORTED ARRAY

 Consider a non-empty integer array inarr containing at least two integers. Identify and print outarr array, based on the below logic:

  • For each of the numbers in inarr, move all the negative numbers to the beginning of the array and positive numbers to the end of the array while maintaining their input order.
  • Starting from the first number in the rearranged array, for each pair of adjacent numbers, num1 and num2:

    • find the difference (num2-num1)
    • if the difference is greater than 1, compute the sum of missing numbers between num1 and num2 and store the sum in outarr
    • If the difference is not greater than 1 add 0 to outarr

Note: Consider 0 to be a positive number

Input Format

First line contains inarr with the elements separated by ','(comma) Read the input from the standard input stream.

Constraints

First line contains inarr with the elements separated by ','(comma) Read the input from the standard input stream.

Output Format

Print outarr with the elements separated by ‘,’ (comma) to the standard output stream.

Sample Input 0

-5,5,1,-2,7

Sample Output 0

-7,9,0,20

Explanation 0

For the given inarr, move the negative elements to the beginning and positive elements to the end while maintaining the input order results in -5,-2,5,1,7 Starting from the first number in the rearranged array, the pair of adjacent numbers, num1 and num2, their difference and the corresponding value to be added in outarr are:

  1. num1= -5, num2= -2 difference = -2- (-5) =3; The difference is greater than 1. Hence, missing numbers are -4 and -3, their sum is -7 and outarr becomes {-7}
  2. num1=-2, num2 = 5: difference = 5 -(-2)=7; The difference is greater than 1. Hence, missing numbers are -1,0,1,2,3,4, their sum is 9 and outarr becomes {-7,9}
  3. num1= 5, num2 = 1: difference = 1-5= -4; Since the difference is negative add 0 to outarr. Hence outarr becomes {-7,9,0}
  4. num1= 1, num2= 7: difference = 7-1 = 6; The difference is greater than 1. Hence, missing numbers are 2,3,4,5,6, their sum is 20 and outarr becomes {-7,9,0,20}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class SumOfIntevals {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Getting input and converting it to integer
String[] inarrStr = sc.nextLine().split(",");
Integer[] inarr = new Integer[inarrStr.length];
for (int i = 0; i < inarrStr.length; i++) {
inarr[i] = Integer.parseInt(inarrStr[i]);
}

// Moving all negative numbers to left side and positive numbers to right side
Integer[] rearrangedArray = new Integer[inarr.length];
int index = 0;
for (int i = 0; i < inarr.length; i++) {
if(inarr[i] < 0){
rearrangedArray[index] = inarr[i];
index++;
}
}

for (int i = 0; i < inarr.length; i++) {
if(inarr[i] >= 0){
rearrangedArray[index] = inarr[i];
index++;
}
}

// System.out.println(Arrays.toString(rearrangedArray));

// Finding the diferrence between adjacent numbers

ArrayList<Integer> outList = new ArrayList<Integer>();

for (int i = 0; i < inarr.length - 1; i++) {

int diff = rearrangedArray[i+1]-rearrangedArray[i];
int sum = 0;



if(diff>1){


int increment = rearrangedArray[i] > rearrangedArray[i+1]? -1 : 1;

for (int j = rearrangedArray[i]+increment; j < rearrangedArray[i+1]; j = j+increment){
sum+= j;
}
}
outList.add(sum);

}

String result = outList.toString();
result = result.substring(1,result.length()-1);
result = result.replaceAll(", ",",");
System.out.println(result);



}
}


Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER