Created
October 26, 2017 11:17
-
-
Save madhur/953e04ba8d3a98eebe7128465989695f to your computer and use it in GitHub Desktop.
DeltaEncoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
public static void main(String args[] ) throws Exception { | |
Scanner scan = new Scanner(System.in); | |
ArrayList<Integer> numbers = new ArrayList<Integer>(); | |
ArrayList<Integer> output = new ArrayList<Integer>(); | |
while(true) { | |
try { | |
Integer line = scan.nextInt(); | |
//System.out.println(line); | |
numbers.add(line); | |
} | |
catch(Exception e) { | |
break; | |
} | |
} | |
int prevNum=0; | |
int escapeToken = -128; | |
for(int i =0; i< numbers.size(); ++i) { | |
if(i==0) { | |
output.add(numbers.get(i)); | |
prevNum = numbers.get(i); | |
} else { | |
int diff = numbers.get(i) - numbers.get(i-1); | |
if(diff<-127 || diff > 127) { | |
output.add(escapeToken); | |
} | |
// System.out.println("The diff is: " + diff); | |
output.add(diff); | |
} | |
} | |
for(Integer num: output) { | |
System.out.print(num + " "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment