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

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