Sadina Khatun
Sadina Khatun

Reputation: 1166

Twin array Find a Minimum value from each Array index must be different

You are given two arrays A and B each containing n, integers. You need to choose exactly one number from A and exactly one number from B such that the index of the two chosen numbers is not same and the sum of the 2 chosen values is minimum.

Your objective is to find and print this minimum value.

For example in the image shown below is the minimum sum. enter image description here

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int twinArrays(int[] ar1, int[] ar2){
        // Complete this function
        int minAr1 = ar1[0];
        int minAr2;
        int index = 0;
        for(int i =0; i<ar1.length;i++) {
            if(ar1[i]<minAr1) {
                minAr1 = ar1[i];
                index = i;
            }
        }
        if(index == 0) {
            minAr2 = ar2[1];
        }else {
            minAr2 =ar2[0];
        }

        for(int j=0;j<ar2.length;j++) {
            if(j!=index && minAr2>ar2[j]) {
                minAr2 =ar2[j];
            }
        }
        return minAr1+minAr2;
     }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        if(n>=2 && n<=1000000) {
        int[] ar1 = new int[n];
        for(int ar1_i = 0; ar1_i < n; ar1_i++){
            int temp1 = in.nextInt();
            if(temp1>=1&&temp1<=1000000) {
                ar1[ar1_i] = temp1;
            }
        }
        int[] ar2 = new int[n];
        for(int ar2_i = 0; ar2_i < n; ar2_i++){
            int temp2 = in.nextInt();
            if(temp2>=1&&temp2<=1000000) {
                ar2[ar2_i] = temp2;
            }
        }
        int result = twinArrays(ar1, ar2);
        System.out.println(result);
        }

    }
}

i have Implemented that is working fine but still something is missing so some test case failing could anyone give me some clue and hint to optimize my code if you find any defect in this code

Upvotes: 0

Views: 3016

Answers (4)

Sanket Makani
Sanket Makani

Reputation: 2489

One simple observation is that if minimum elements from both arrays are at different position, Sum of both elements would be an answer so after sorting answer in this case would be A[0]+B[0], but if both are at the same position, there are two possibilities for answer after sorting both the arrays.

Suppose we have two arrays A and B which are sorted in increasing order. Then there are two possible answers and the actual answer would be minimum of them.

  1. Take minimum possible number from array A so that number would be A[0] and now find the minimum possible number from array B such that index of that element is not equal to 0 so best possible choice would be B[1]. Therefore answer in this case is A[0]+B[1].
  2. Another possibility is that we consider minimum possible number from array B first and then go to choose a number from array A so here those numbers would be A[1] and B[0] respectively and thus answer in this case is A[1]+B[0].

Now for the final answer, we can take minimum of both these possible answers.

Final_Answer = min(A[0]+B[1],A[1]+B[0]);

If you don't want to sort the Arrays due to tight time constraint. You can just keep track of First and Second minimum element of both arrays and use it in place of A[0],A[1] in above equation respectively.

A sample code using first and second minimum,

static int twinArrays(int[] ar1, int[] ar2){

    int first_minimum_ar1 = Integer.MAX_VALUE;
    int second_minimum_ar1 = Integer.MAX_VALUE;
    int index_ar1=-1;

    int first_minimum_ar2 = Integer.MAX_VALUE;
    int second_minimum_ar2 = Integer.MAX_VALUE;
    int index_ar2=-1;


    for(int i=0;i<ar1.length;i++)
    {
        int element = ar1[i];
        if(first_minimum_ar1>=element)
        {
            second_minimum_ar1=first_minimum_ar1;
            first_minimum_ar1=element;
            index_ar1=i;
        }
        else if(second_minimum_ar1>element)
        {
             second_minimum_ar1=element;
        }
    }

    for(int i=0;i<ar2.length;i++)
    {
        int element = ar2[i];
        if(first_minimum_ar2>=element)
        {
            second_minimum_ar2=first_minimum_ar2;
            first_minimum_ar2=element;
            index_ar2=i;
        }
        else if(second_minimum_ar2>element)
        {
             second_minimum_ar2=element;
        }
    }

    if(index_ar2!=index_ar1)
        return first_minimum_ar1+first_minimum_ar2;


    return Math.min(first_minimum_ar1+second_minimum_ar2,first_minimum_ar2+second_minimum_ar1);

}

Upvotes: 1

Yati Sawhney
Yati Sawhney

Reputation: 1402

This one will probably pass all of your test cases.

Runtime Complexity ~ o(n)

static int twinArrays(int[] ar1, int[] ar2){
        int ar1Minimum = Integer.MAX_VALUE , ar1MinIndex = -1;
        int ar1SecondMin = Integer.MAX_VALUE ;

        //Get the element with minimum value and index
        for(int i = 0 ; i < ar1.length ; ++i){
            if(ar1Minimum > ar1[i]){
                ar1Minimum = ar1[i]; ar1MinIndex = i;
            }       
        }


        //Get the second minimum 
        for(int i = 0 ; i < ar1.length ; ++i){
            if(ar1SecondMin > ar1[i] && i!=ar1MinIndex){ // i != ar1MinIndex = Important to avoid duplicate minimum values [1,1,2,4] min = 1 , secondMin = 2 and not 1
                ar1SecondMin = ar1[i]; 
            }       
        }

        int ar2Minimum = Integer.MAX_VALUE , ar2MinIndex = -1;
        int ar2SecondMin = Integer.MAX_VALUE ;

        for(int i = 0 ; i < ar2.length ; ++i){
            if(ar2Minimum > ar2[i]){
                ar2Minimum = ar2[i]; ar2MinIndex = i;
            }       
        }

        for(int i = 0 ; i < ar2.length ; ++i){
            if(ar2SecondMin > ar2[i] && i != ar2MinIndex){
                ar2SecondMin = ar2[i];
            }       
        }

        if(ar1MinIndex != ar2MinIndex){
            return ar1Minimum + ar2Minimum;
        }

        return Math.max((ar1Minimum + ar2SecondMin), (ar1SecondMin + ar2Minimum));


 }

Upvotes: 0

MuM6oJuM6o
MuM6oJuM6o

Reputation: 107

What I did was, sort one array(ar1) in ascending order(take ar[0]) and the other in descending order(ar2) and then take (ar2[ar2.length-1]).

 class TwinArray{
    static int twinArrays(int[] ar1, int[] ar2){
       Arrays.sort(ar1);
       Integer[] Ar2 = new Integer[ar2.length];
        for (int i=0;i<ar2.length;i++)
            Ar2[i]=ar2[i];
         Arrays.sort(Ar2,Collections.reverseOrder());
    System.out.println(Ar2[Ar2.length-1]);
     return (ar1[0]+Ar2[Ar2.length-1]);
    }
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] ar1 = new int[n];
        for(int ar1_i = 0; ar1_i < n; ar1_i++){
            ar1[ar1_i] = in.nextInt();
        }
        int[] ar2 = new int[n];
        for(int ar2_i = 0; ar2_i < n; ar2_i++){
            ar2[ar2_i] = in.nextInt();
        }
        int result = twinArrays(ar1, ar2);
        System.out.println(result);
    }
}

Upvotes: 0

templatetypedef
templatetypedef

Reputation: 372704

Your code works by finding the minimum value from the first array, then finding the minimum value from the second array that isn't at the same position of the minimum in the first array. The problem with this approach is that it isn't guaranteed to find the overall minimum sum. For example, consider these small arrays:

 [0, 1], [0, 100]

Here, if you take the minimum value of the first array (0) and the minimum value of the second array that isn't at the same position (100), you get the sum 100. However, the correct sum to pick is the smallest value from the second array (0) and the second-smallest value from the first array (1) for a total of 1.

One observation that's useful here is that the minimum-sum pair must be one of the following:

  • The sum of the smallest values from each array.
  • The sum of the smallest value from one array and the second-smallest value from the other.

Your solution is on the right track here, except you don't consider taking the smallest from the second array and the second smallest from the first. Try editing your code to account for that case.

Upvotes: 2

Related Questions