<?php
class Matrix{
/**
•@param 2D array $matrix
•
•Prints distinct elements common to all rows of the matrix */
public static function getDistinctElementsCommonToAllRows($matrix){
// A hash map to store count of elements
$hashmap = array();
$selectedHash = array();
$rows = count($matrix);
$cols = count($matrix[0]);
for ($i = 0; $i < $rows; $i++) {
// Increment the count of first
// element of the row
if(array_key_exists($matrix[$i][0],$hashmap)){
$hashmap[$matrix[$i][0]] = $i+1;
}
// Starting from the second element
// of the current row
for ($j = 1; $j < $cols; $j++) {
// If current element is different from
// the previous element i.e. it is appearing
// for the first time in the current row
if(array_key_exists($matrix[$i][$j],$hashmap)){
if ($matrix[$i][$j] !== $matrix[$i][$j - 1]){
$hashmap[$matrix[$i][$j]] = $i+1;
}
}else{
if ($matrix[$i][$j] !== $matrix[$i][$j - 1]){
$hashmap[$matrix[$i][$j]] = $i;
}
}
}
}
foreach($hashmap as $key => $val){
if($val == $rows){
$selectedHash[] = $key;
}
}
return $selectedHash;
}
}
$mat1 = [[2, 1, 4, 3],[1, 2, 3, 2],[3, 6, 2, 3],[5, 2, 5, 3]];
$mat2 = [[12, 1, 14, 3, 16],[14, 2, 1, 3, 35],[14, 1, 14, 3, 11],[14, 25, 3, 2, 1],[1, 18, 3, 21, 14]];
print_r (Matrix::getDistinctElementsCommonToAllRows($mat1));
Print_r('<br>');
print_r (Matrix::getDistinctElementsCommonToAllRows($mat2));
Luhn algorithm for IMEI Check public function __checkIMEI($imei){ if(strlen($imei)==15){ $imeia=($imei[1]*2); if(strlen($imeia)==2){$imeia=str_split($imeia,1); $imeia=$imeia[0]+$imeia[1]; } $imeib=($imei[3]*2); if(strlen($imeib)==2){$imeib=str_split($imeib,1); $imeib=$imeib[0]+$imeib[1]; } $imeic=($imei[5]*2); if(strlen($imeic)==2){$imeic=str_split($imeic,1); $imeic=$imeic[0]+$imeic[1]; } $imeid=($imei[7]*2); if(strlen($imeid)==2){$imeid=str_split($imeid,1); $imeid=$imeid[0]+$imeid[1];} $imeie=($imei[9]*2); if(strlen($imeie)==2){$imeie=str_split($imeie,1); $imeie=$imeie[0]+$imeie[1]; } $imeif=($imei[11]*2); if(strlen($imeif)==2){$imeif=str_split($imeif,1); $imeif=$imeif[0]+$imeif[1]; } $imeig=($imei[13]*2); if(strlen($imeig)==2){$imeig=str_split($imeig,1); $imeig=$imeig[0]+$imeig[1]; } $IMEI= ($ime...
Comments
Post a Comment