[Solved] Allens Value with Java, CPP, Python

Allens value: You are given an array A of N (>=3) distinct integers. Your friend Allen removes only 2 elements from this array, adds a value (> 0) to all the remaining elements, shuffles the array and leaves the newly formed array G for you.

Your task is to return the minimum possible value that Allen had added to each element of array A to get to array G.

Note: It is guaranteed that at least one such value which will be greater than 0, will always exist.
Consider 1 based indexing.

Input Specification:
input1 : An integer value N, representing the size of array A.
input2 : An integer array A.
input3 : An integer array G created by Allen.

Output Specification:
Return the minimum possible value that Allen had added to each element of array A to get to array G.

Example 1:
input1 : 4
input2 : {4,3,1,2}
input3 : {7,4}

Output : 3

Explanation:
By removing elements 3 and 2, present at second and fourth index respectively, the array will become {4, 1}. Now by only adding 3 (minimum value) to the remaining elements, the newly formed array G is {7, 4}. Since 3 is the minimum possible value required to be added to convert array A to G, therefore 3 is returned as the output.

Example 2:
input1 : 3
input2 : {2,1,3}
input3 : {2}

Output : 1

Explanation:
By removing elements 2 and 3, present at first and third index respectively, the array will become {1}. Now by adding 1 to the remaining elements, the newly formed array G is {2}. Since 1 is the minimum possible value required to be added to convert array A to G, therefore 1 is returned as the output.

Solution

Steps:

Given three inputs: input1, input2, and input3, where input1 represents the size of array A, input2 represents array A, and input3 represents array G.

  1. Sort both input2 and input3 in ascending order.
  2. Calculate the difference between the first element of input3 (smallest element in G) and the first element of input2 (smallest element in A).
  3. Return this difference as the output, which represents the minimum possible value that Allen added to each element of array A to get array G.
public int allensValue(int input1,int[] input2,int[] input3){
    Arrays.sort(input2);
    Arrays.sort(input3);
    // github.com/sauravhathi
    int diff = input3[0] - input2[0];
    return diff;
}
#include<iostream>
#include<algorithm>
using namespace std;

int allensValue(int input1,int input2[],int input3[]){
    sort(input2,input2+input1);
    sort(input3,input3+input1-2);
    // github.com/sauravhathi
    int diff = input3[0] - input2[0];
    return diff;
}
def allensValue(input1,input2,input3):
    input2.sort()
    input3.sort()
    # github.com/sauravhathi
    diff = input3[0] - input2[0]
    return diff

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *