Skip to content

Commit

Permalink
Difference in array elements
Browse files Browse the repository at this point in the history
taking borrow from the next element
  • Loading branch information
suhanigupta23 authored Feb 6, 2024
1 parent c02f8c5 commit 5c46871
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions difference_in_array_elements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.*;
public class Program
{
public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);

int n1=scn.nextInt();
int [] a1=new int [n1];
for(int i=0;i<a1.length;i++)
{
a1[i]=scn.nextInt();
}

int n2=scn.nextInt();
int [] a2=new int[n2];
for(int i=0;i<a2.length;i++)
{
a2[i]=scn.nextInt();
}


int[]diff=new int [n2];
int c=0;

int i=a1.length-1;
int j=a2.length-1;
int k=diff.length-1;

while(k>=0)
{
int d=0;
int a1v=i>=0?a1[i]:0;


if(a2[j]+c>=a1v)
{
d=a2[j]+c-a1v;
c=0;
}

else
{
d=a2[j]+c+10-a1v;
c=-1;
}

diff[k]=d;

i--;
j--;
k--;
}
int idx=0;
while(idx<diff.length)
{
if (diff[idx]==0)
{
idx++;
}
else
{
break;
}
}
while(idx<diff.length)
{
System.out.println(diff[idx]);
idx++;
}
}

}

0 comments on commit 5c46871

Please sign in to comment.