-4

please help make php function that check whether a given array of integer consists all sequence number from 1 to N. Each number can only appear once in array. Output true/false.

VRB
  • 186
  • 14
sam achmad
  • 11
  • 1
  • 1
  • https://stackoverflow.com/questions/3559542/more-concise-way-to-check-to-see-if-an-array-contains-only-numbers-integers then check if array has negative ints and check unique values https://stackoverflow.com/questions/3145607/php-check-if-an-array-has-duplicates – mcek Dec 10 '18 at 06:45
  • 1
    You need to add a sample array so that we know what you mean. And include what you have tried. – Andreas Dec 10 '18 at 06:47
  • @sam can you use: `array_key_exists`, `max`, `count`? – dWinder Dec 10 '18 at 11:37
  • @DavidWinder i'm beginner , so max i do , count i do , but array i'm trying – sam achmad Dec 11 '18 at 07:38
  • input : array(2,3,1,4); output : true input : array(2,5,3,4); output : false (because not start from 1) input : array(1,3,4,2,5,6,3) output : false (because 3 appears twice) input : array(1,2,3,6,5,4,7); output : true input : array(1,2,3,5,4,7,8); output : false (because missing number 6) – sam achmad Dec 11 '18 at 07:42
  • Create a php function that check whether a given array of integer consists all sequence number from 1 to N. Each number can only appear once in array. Position of number can be random. Output true/false. (You are not allowed to use php built in array sort function) actually this its my completed question , thank you Mister , and when i have test in yogyakarta test i don't know and i'm start to need help and learning how can be profesional programe thank Mr.David – sam achmad Dec 11 '18 at 07:53

2 Answers2

1

Since you say integer consists all sequence number from 1 to N. Each number can only appear once in array
That means if we compare the array with a range() then the output is true/false.
You can also include a sort in case the arrays are not sorted.

$arr1 = [2,1,3,4,5,6,6,7];
$arr2 = [1,2,3,4,5,6,7];
$arr3 = [1,3,4,5,6,7];
$arr4 = [1,5,6,7,4,3,2];

// sort arrays
sort($arr1);
sort($arr2);
sort($arr3);
sort($arr4);

$n = 7;
$range = range(1,$n);
// or $range(min($array),$n); or $range(min($array),max($array)); depedning on how you want it set up


var_dump($arr1 == $range); //false
var_dump($arr2 == $range); //true
var_dump($arr3 == $range); //false
var_dump($arr4 == $range); //true

https://3v4l.org/DMe46


With the added information, we can use array_intersect and count.
Array intersect returns the matching items in the array.
If we then count them we see if they match.

$arr1 = array(2,3,1,4);
$arr2 = array(2,5,3,4);
$arr3 = array(1,3,4,2,5,6,3);


$range1 = range(1,max($arr1));
$range2 = range(1,max($arr2));
$range3 = range(1,max($arr3));


var_dump(count($range1) == count(array_intersect($arr1, $range1)));
// true

var_dump(count($range2) == count(array_intersect($arr2, $range2)));
// false

var_dump(count($range3) == count(array_intersect($arr3, $range3)));
// false

https://3v4l.org/7Hdll

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Notice OP request **1** to N – dWinder Dec 10 '18 at 07:04
  • 1
    I think the `min()`, `max()` solution covers any combination of numbers – Nigel Ren Dec 10 '18 at 07:04
  • Yes?? You want me to capitalize my `$n`? @DavidWinder – Andreas Dec 10 '18 at 07:04
  • 1
    No, start from 1 and not 0 – dWinder Dec 10 '18 at 07:05
  • Ok. Didn't notice that. Thanks. – Andreas Dec 10 '18 at 07:05
  • ok thank you very much friends, I appreciate my clearer question like this You are not allowed to use php built in array sort function Example input : array(2,3,1,4); output : true input : array(2,5,3,4); output : false (because not start from 1) input : array(1,3,4,2,5,6,3) output : false (because 3 appears twice) input : array(1,2,3,5,4,7,8); output : false (because missing number 6) – sam achmad Dec 10 '18 at 08:08
  • I'm allowed to use any functions I want! If you want help on this page make sure to go to the help section before asking a question again – Andreas Dec 10 '18 at 08:16
0
  • Each number has to be an integer
  • Each number has to be bigger than the previous number
  • Negative number are allowed.
function testInt($my_array)
{
  $prevval = null;
  foreach ($my_array as $val) {
      if ((filter_var($val, FILTER_VALIDATE_INT)) === false) {
          return false;
      }
      if ($prevval !== null) {
          if ($val <= $prevval) {
              return false;
          }
      }
      $prevval = $val;
  }
  return true;
}
$test1 = [-1, "4", 5, 7];
$test2 = [0, 1, 2, 100];
$test3 = [1, 4, 4, 100];
$test4 = [1, 5, 4, 1000];
$test5 = [5, 6, 7, "50"];

var_dump(testInt($test1));
bool(true)
var_dump(testInt($test2));
bool(true)
var_dump(testInt($test3));
bool(false)
var_dump(testInt($test4));
bool(false)
var_dump(testInt($test5));
bool(true)
EvE
  • 734
  • 3
  • 13
  • ok thank you very much friends, I appreciate my clearer question like this You are not allowed to use php built in array sort function Example input : array(2,3,1,4); output : true input : array(2,5,3,4); output : false (because not start from 1) input : array(1,3,4,2,5,6,3) output : false (because 3 appears twice) input : array(1,2,3,5,4,7,8); output : false (because missing number 6) – sam achmad Dec 10 '18 at 08:12