Click here to Skip to main content
13,632,925 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
You are given two arrays A and B each containing N numbers. 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. Formally, if you chose ith element from A whose value is x and jth element from B whose value is y, you need to minimize the value of (x+y) such that i is not equal to j.
Your objective is to find this minimum value.


Input:
The first line of input contains an integer denoting the test cases,t. The first line of each test case contains an integer N denoting the size of two arrays. Then each of the next two lines contains N space separated integers denoting values of the array A[] and B[] respectively.


Output:
Print the minimum sum which can be obtained under the conditions mentioned in the problem statement.If not possible print "-1" without quotes.


Constraints:
1<=T<=30
1<= N <=100000
1< =Array elements < =100000

Example:
Input:
1
5
5 4 3 2 1
1 2 3 4 5
Output:
2

Explanation:
Minimum sum will be obtained by choosing number at the last index of first array i.e. 5th element of the first array(1) and first index of the second array ie first element of second array (1).

Sum=1+1=2 as their indexes are different but sum is minimum.

What I have tried:

#include
int main()
{
int t,i,j,k,min=0;
//# of test cases
scanf("%d",&t);

struct testcase
  {
  	int sizeOfArray;
  	int a[10];
  	int b[10];
  	int ans;
  	
  };
  
 struct testcase tc[t];            //array of structures, size t 
  
  for(i=0;i<t;i++)
  {
  	scanf("%d",&tc[i].sizeOfArray);  //entering size of a and b
  	
  	 for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of a
  	 {
  	 	
  	 	scanf("%d",&(tc[i].a[j]));
	 }
  	
  	for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of b
  	 {
  	 	
  	 	scanf("%d",&tc[i].b[j]);
	 }  		
  }
  

int no=0;
for(k=0;k<t;k++)
{
	
    min=	tc[k].a[0]+tc[k].b[1];
	for(i=0;i<tc[k].sizeOfArray;i++)
	{
	 
	
		for(j=0;(j<tc[k].sizeOfArray);j++)
		 { 
		 if((tc[k].a[i]+tc[k].b[j]<min)&&(j!=i))
		 	{
		 	 
		 		min=tc[k].a[i]+tc[k].b[j];
			}
		 }
	}
	
	tc[k].ans=min;
	printf("%d\n",min);

}


return 0;
	
}



//As this is giving correct results on my system but I am getting runtime error while //submitting this code
Posted 11-Jan-17 8:23am
Updated 12-Jun-17 21:02pm
v2
Comments
jeron1 12-Jan-17 10:18am
   
struct testcase tc[t]; //array of structures, size t

Doesn't look right, an array with a variable number of entries would need to created dynamically using malloc() or the like.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Quote:
//As this is giving correct results on my system but I am getting runtime error while //submitting this code
The problem is probably that your submission is tested with more that 10 values in array A and B.
PS: next time: try to state the error message.
Quote:
No I was testing with less then 10 elements, it was giving me correct results, but when I submit it on geeksforgeeks, it gives me
Yes you, but them!
How many elements did they used ?
  Permalink  
v2
Comments
chunky77 11-Jan-17 14:52pm
   
No I was testing with less then 10 elements, it was giving me correct results, but when I submit it on geeksforgeeks, it gives me this error-
Run Time Error:
Error : dumped core
chunky77 12-Jan-17 5:24am
   
According to question if I use array size=100000 a[100000],b[100000] then also I get same error.
ppolymorphe 12-Jan-17 6:13am
   
Not what your code saying.
CPallini 13-Jun-17 3:39am
   
My 5.
ppolymorphe 13-Jun-17 4:10am
   
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180712.1 | Last Updated 13 Jun 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100