fork download
  1. int maximumGap(vector<int>& nums) {
  2. int n=nums.size();
  3. int diff=0,maxm=0;
  4. for(int i=0;i<n-1;++i){
  5. if(nums[i+1]-nums[i]>0){
  6. diff=nums[i+1]-nums[i];
  7. }
  8. maxm=max(maxm,diff);
  9. }
  10. return maxm;
  11. }//Enter you code here.
  12. //Please indent properly.
  13.  
  14. <?php
  15. //program to find the common elements of the two array
  16. //here we have to array A and B from which w have to find the common element
  17. //first we sort then using merge sort and after then for traversing through
  18. //the array in one iteration we can find the comman elements the given array
  19. //this is an inspace algorithm meansno extra space is needed
  20.  
  21. //best case time complexity=O(nlogn)
  22. //O(nlogn)-> for sorting
  23. //O(n)-> for while loop to find comman element
  24.  
  25. //average case time complexity=O(nlogn)
  26. //O(nlogn)-> for sorting
  27. //O(n)-> for while loop to find comman element
  28.  
  29. //worst case time complexity =O(nlogn)
  30. //O(nlogn)-> for sorting
  31. //O(n)-> for while loop to find comman element
  32.  
  33.  
  34.  
  35. $commonArray=array();
  36. $A=array(3,4,5,6,7,8,9,10,36,58,27,48);
  37. $B=array(3,10,4,5,6,8,12,24,37,27,50);
  38. sort($A);
  39. sort($B);
  40. $size1=sizeof($A);
  41. $size2=sizeof($B);
  42. $counter1=0;
  43. $counter2=0;
  44. while(($counter1< $size1) && ($counter2)<($size2))//traversing through the array
  45. {
  46.  
  47. if ($A[$counter1] == $B[$counter2])
  48. {
  49. array_push($commonArray,$A[$counter1]); //to enter comman element in the output array
  50. $counter1=$counter1+1;
  51. $counter2=$counter2+1;
  52. }
  53. else if ($A[$counter1] < $B[$counter2])
  54. {
  55. $counter1=$counter1+1; }
  56.  
  57. else
  58. {
  59. $counter2=$counter2+1;
  60. }
  61. }
  62.  
  63. print_r($commonArray);//to print the output array
  64. ?>
  65.  
  66.  
Success #stdin #stdout 0.03s 26580KB
stdin
Standard input is empty
stdout
int maximumGap(vector<int>& nums) {
        int n=nums.size();
        int diff=0,maxm=0;
        for(int i=0;i<n-1;++i){
            if(nums[i+1]-nums[i]>0){
                diff=nums[i+1]-nums[i];
            }
            maxm=max(maxm,diff);
        }
        return maxm;
    }//Enter you code here.
//Please indent properly.

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 8
    [5] => 10
    [6] => 27
)