Pairtel company is given the contract to assign IP addresses to the computers of HackerEarth. The printer used to print the set malfunctioned and some of the addresses were wrongly printed.
Task
Check the validity of each address. Return "IPv4" if P is a valid IPv4 address, "IPv6" if IP is a valid IPv6 address or "Neither" if IP is not a correct IP of any type.
Notes
Example
Assumptions
Output
Approach
Function Description
Complete the solve function provided in the editor. This function takes the following parameter and returns the required answer:
Input Format
Output Format
Print T lines where the ith line is the type of the ith IP address
Constraints
1 <= T <= 10^5
1 <= | ip | <= 10^5
================
You are given an array A of length N consisting of distinct elements.
You need to construct a Binary Search Tree(BST) by inserting each element one by one from array A, starting from the first element till last.
Task
Determine the maximum path sum from root node to leaf node of the BST after each new element is inserted.
Notes
Example
Assumptions
Approach
The world is rapidly changing and getting smarter day by day. To go towards the path of sustainable development, you have decided to build a future par where you have parking space for solar-powered electric vehicles that have 0 emissions and save fuel and the environment. In the future park, the solar-powered cars need to be parked in such a way that they can get just enough sunlight in 1 hour to fully charge themselves. The current state of car parking is represented by an array Current wherein the array element -1 represents the car number at that spot. You are also given an array Desired which shows the ideal parking positioning of cars so that they can charge themselves fully in 1 hour. In one move, you are allowed to move a car one place forward or backward to an empty spot.
Task
Determine the minimum number of moves required to achieve the desired state of solar-powered cars. If it is not possible, print -1.
Notes
Example
Assumptions
Approach
So, the answer is 1.
Function Description
Complete the solve function provided in the editor. This function takes the following 3 parameters and returns the minimum moves required to achieve the final state:
Input Format
Output Format
For each test case in a new line print the number of moves required or -1 if not possible.
Q3)
Assuming sum of N over all test cases is at most 10^5
//check whether it is possible or not by checking pillars position
for(int i=0;i < n;i++)
{
//check whether if there is pillar in one of them and in other it is not.
if((desired[i] < 0) and (current[i] > =0))
return -1;
if((desired[i] > =0) and (current[i] < 0))
return -1;
}
n --- > current size of current or desired array.(Remember it had been modified a bit.)
for(int i=0;i < n;i++)
{
if(desired[i]==-1)//This means both has pillar
{
int j=i+1;
list want,given; //want and given are the car arrangements in desired and current respectively.
list want_indices,given_indices;//want_indices and given_indices represent the indices of the cars in desired and current respectively
while((j < n) and (desired[j]!=-1))
{
if(current[j] > 0){
given_indices.push(j);
given.push(current[j]);
}
if(desired[j] > 0){
want_indices.push(j);
want.push(desired[j]);
}
j++;
}
if(want==given)//check whether cars in same arrangement
{
for(int i=0;i < size of(want_indices);i++)
ans+=abs(want_indices[i]-given_indices[i]); //number of moves
}
else
return -1;
}
}
return ans;
Question 1
Intuition/Approach
For IPv4
range [0-255]
and does not contain leading 0s. If not, then update the value of ans to falsetrue
, then the string is a valid IPv4 address, Otherwise, it is not a valid IPv4 address.For IPV6
size of V
is equal to 8 or not. If not equal, update ans to false.range [1, 4]
and it is a valid hexadecimal number. If not, then update the value of ans to false.true
, then the string is a valid IPv6 address. Otherwise, it is not a valid IPv6 address.Find the path in the binary search tree at which sum of the value on nodes is maximum.
> O(nlog(n))
where n is number of nodes in the binary search tree.
Node* insert(Node* root, int key)
{
Node* newnode = newNode(key);
Node* x = root;
Node* y = NULL;
while (x != NULL) {
y = x;
if (key < x->key)
x = x->left;
else
x = x->right;
}
if (y == NULL)
y = newnode;
else if (key < y->key)
y->left = newnode;
else
y->right = newnode;
return y;
}
void Inorder(Node* root,int sum)
{
if (root == NULL){
val = max(val,sum);
return;
}
else {
Inorder(root->left,sum+root->key);
Inorder(root->right,sum+root->key);
}
}
// To make the tree insert nodes for every element of the array one by one