Google interviews problems solution and Problem
In this post, we will discuss how to handle any coding problem and how to solve problems in just 4 Steps and also how to analyze the problem statements and how to understand the problem and make a solution step by step. If you understand the coding problem, then the problem becomes so easy.
So, our first step to understand the coding problem, and then we will divide the problem into steps, so, this can help us to solve problems in a short period of time.
Problem statements are given below.
Explanation of the problem statements
The problem statements want to explain to us that, one unsorted array was given to us, and the named of the unsorted array is B. And array is non- negative integers. And they also give the size of the array named N. And they also give the one number that's S.
Now our task is to find the sum of the continuous sub-array which is equal to the S number.
Format to take the input
Format of the output
Understand the format of the output
So, we need to print the starting and ending position of the continuous subarray from left to right and if the sum is not equal to the sum S then we need to print -1.
Explanation of the output:
How to solve the problem using my personal method
We, solve the problem in the 4 steps:
- Analyze the problem
- Take the input from the user
- Apply the logic to solve the problem
- Print the output
1. So, we need to analyze or understand the problem statement that we have done already above.
2. Now our second step to take the input from the user and note that only in the input format that was given above.
So, we first we take the input of the number of the test cases and we named the variable "T".
And in the second line, we want to take the list from the user for N and S and then split the list by using the for loop.
And in third, we take the unsorted array and split this array into the elements.
So, our second step is also completed.
3. The third step is to apply logic to get an exact answer.
So, first of all, we apply the while loop to take all the test case input from the users.
Then we will run the for loop to check the subarray is equal to the sum or not.
4. if the subarray is equal to the sum then we find the position of the for loop and print it otherwise we print the -1.
Steps of the program:
- First, we will take the number of test cases.
- Then we will run the while loop according to the test cases.
- Then we will take the length of the list and sums that we want to output.
- Then we will separate the length(size) and Sum.
- Then we will take the list from the user.
- Then we will check the subarray and check is equal to the given sum or not.
- If they equal to the given sums then we will print the location of the subarray.
- And if it is not equal to this or greater than the given sum we will start the sums of the subarray from the 2 elements.
- After when the one test cases will close then we will minus the 1 number from the test cases.