php - How to take an array of size N?
Get the solution ↓↓↓I am working on to improve my coding skill.
So I joined hackerearth to solve coding problems in PHP language because I know PHP.
But on the very first question I got stuck because I don't know how to take input as an array of size N.
The problem on which I am stuck is given below -
Coding challenge -
Monk loves to preform different operations on arrays, and so being the principal of Hackerearth School, he assigned a task to his new student Mishki. Mishki will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array. As she is new to the school, please help her to complete the task.
What I have tried to solve the problem-
As this problem ask for 3 inputs -
First the number of test cases (T).
Second is the size of the array (N) and number of rotation (K).
Third array of size N.
So I have successfully passed the first test case (code is given below)-
fscanf(STDIN, "%s\n", $t);
fscanf(STDIN, "%s %s\n", $n, $k);
//taking 5 numbers seperated by space.
fscanf(STDIN, "%d %d %d %d %d\n", $item1,$item2,$item3,$item4,$item5);
$arr = [$item1,$item2,$item3,$item4,$item5];
for($i = 0; $i<$k; $i++){
array_unshift($arr, array_pop($arr));
}
echo implode(' ', $arr);
Input for above code -
1
5 2
1 2 3 4 5
Output -
4 5 1 2 3
What is the problem in my solution --
If user gives N= 6 (other than 5) then code will not work. Because size of the array is depend on N.
So please help me how to give array of size N ?
Answer
Solution:
As the other answer says,fscanf
will return an array if you only pass the stream and format.
Additionally, because we are going to be working with multiple test cases, we have to wrap this in a for loop around$t
.
Once we do this, we usestr_repeat
to build ourfscanf
string, and assign its results directly to$arr
. After this we can use the rest of your code. One important addition is the added "\n" character at the end.
fscanf(STDIN, "%s\n", $t);
for($x = 0; $x < $t; $x++){
fscanf(STDIN, "%s %s\n", $n, $k);
//taking 5 numbers seperated by space.
$arr = fscanf(STDIN, str_repeat("%d ", $n) . "\n\n");
for($i = 0; $i<$k; $i++){
array_unshift($arr, array_pop($arr));
}
echo implode(' ', $arr);
echo "\n";
}
However, while this will work for the first 3 test cases, it will exceed the time limit on the last 2. So you will need to find a better algorithm for shifting a position. Maybe look at the difference between n & k so you aren't shifting more than needed.
Answer
Solution:
Some thoughts:
First,fscanf
will return an array if you only pass the stream and the format, you don't need to assign the variables and then construct your own array.
Given that this is the case, you should be able to complete the assignment by generating your format string forfscan
by adding elements based on the$n
that was derived from above, so something like this should work:
fscanf(STDIN, "%s\n", $t);
fscanf(STDIN, "%s %s\n", $n, $k);
//taking $n numbers separated by space.
for( $i = 0; $i < $n, $i++ ){
$inputFormat[] = "%d";
}
$format = implode( ' ', $inputFormat ) . "\n";
$arr = fscanf(STDIN, format);
for($i = 0; $i<$k; $i++){
array_unshift($arr, array_pop($arr));
}
echo implode(' ', $arr);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.