Number of Steps

Problem

You are given two arrays a1,a2...an  and b1,b2...bn . In each step, you can set  if ai=ai-bi if ai>=bi . Determine the minimum number of steps that are required to make all  a's equal.


C  Solution:


  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n,k,steps=0;
  5. scanf("%d",&n);
  6. int i,a[n],b[n];
  7. for(i=0;i<n;i++)
  8. scanf("%d",&a[i]);
  9. for(i=0;i<n;i++)
  10. scanf("%d",&b[i]);
  11. for(i=0;i<n-1;i++)
  12. {
  13. if(a[i]<a[i+1])
  14. {
  15. k=a[i];
  16. a[i]=a[i+1];
  17. a[i+1]=k;
  18. k=b[i];
  19. b[i]=b[i+1];
  20. b[i+1]=k;
  21. }
  22. for(i=0;i<n-1;i++)
  23. {
  24. while(a[n-1]!=a[i]) {
  25. if(a[i]<=0)
  26. {
  27. printf("-1");
  28. exit(0);
  29. }
  30. if(a[n-1]<a[i]) {
  31. a[i]=a[i]-b[i];
  32. steps++; }
  33. if(a[n-1]>a[i]) 
  34. a[n-1]=a[n-1]-b[n-1];
  35. steps++; }
  36. }
  37. }
  38. printf("%d",steps);
  39. return 0;
  40. }
Note:
       We Are Providing Every Content in this Blog Inspired From Many People and Provide it Open To All People Who Are Willing Gain Some Stuff in Computer Science.

Comment Below If You Have Doubts& queries