Q)...A integer array is given as
input. find the difference between each element.
Return the index of the largest element which has the largest difference gap.
input: {2,3,4,2,3}
logic: 2-3=1,3-4=1,4-2=2,2-3=1
2 is the max diff between 4 and 2,return the index of 4(2)
output:2
Return the index of the largest element which has the largest difference gap.
input: {2,3,4,2,3}
logic: 2-3=1,3-4=1,4-2=2,2-3=1
2 is the max diff between 4 and 2,return the index of 4(2)
output:2
- package Set1;
- public class ClassSet3 {
- public static int getDiffArray(int[] n1){
- int n2,n3=0,n4=0,i;
- for(i=0;i<n1.length-1;i++){
- n2=Math.abs(n1[i]-n1[i+1]);
- if(n2>n3){
- n3=n2;
- n4=i+1; }}
- return n4;
- }
- public static void main(String[] args) {
- int[] n1={2,9,4,7,6};
- System.out.println(getDiffArray(n1));
- }
- }