Algorithm
problem link- https://www.spoj.com/problems/OFFSIDE/
OFFSIDE - He is offside!
Hemisphere Network is the largest television network in Tumbolia, a small country located east of South America (or south of East America). The most popular sport in Tumbolia, unsurprisingly, is soccer; many games are broadcast every week in Tumbolia.
Hemisphere Network receives many requests to replay dubious plays; usually, these happen when a player is deemed to be offside by the referee. An attacking player is offside if he is nearer to his opponents’ goal line than the second last opponent. A player is not offside if
- he is level with the second last opponent or
- he is level with the last two opponents.
Through the use of computer graphics technology, Hemisphere Network can take an image of the field and determine the distances of the players to the defending team’s goal line, but they still need a program that, given these distances, decides whether a player is offside.
Input
The input file contains several test cases. The first line of each test case contains two integers A and D separated by a single space indicating, respectively, the number of attacking and defending players involved in the play (2 <= A,D <= 11). The next line contains A integers Bi separated by single spaces, indicating the distances of the attacking players to the goal line (1 <= Bi <= 104). The next line contains D integers Cj separated by single spaces, indicating the distances of the defending players to the goal line (1 <= Cj <= 104). The end of input is indicated by A = D = 0.
Output
For each test case in the input print a line containing a single character: “Y” (uppercase) if there is an attacking player offside, and “N” (uppercase) otherwise.
Example
Input: 2 3 500 700 700 500 500 2 2 200 400 200 1000 3 4 530 510 490 480 470 50 310 0 0 Output: N Y N
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
import java.util.*;
import java.lang.*;
class Main
{
public static void mergeArrays(int[] left, int[] right, int[] input){
int i = 0, j = 0, k = 0;
while(i < left.length && j < right.length){
if(left[i]>right[j]){
input[k] = right[j];
j++;
}
else{
input[k] = left[i];
i++;
}
k++;
}
if(i > left.length-1){
while(j<=right.length-1){
input[k]=right[j];
j++;
k++;
}
}
else if(j > right.length-1){
while(i<=left.length-1){
input[k]=left[i];
i++;
k++;
}
}
}
public static void mergeSort(int[] input){
if(input.length<2)
return;
int mid = input.length/2;
int[] left = new int[mid];
int[] right = new int[input.length-mid];
for(int i = 0; i<=mid-1; i++){
left[i] = input[i];
}
for(int i = mid; i<=input.length-1; i++){
right[i-mid] = input[i];
}
mergeSort(left);
mergeSort(right);
mergeArrays(left, right, input);
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int numOfAttackingPlayers = s.nextInt();
int numOfDefendingPlayers = s.nextInt();
while(numOfAttackingPlayers != 0 || numOfDefendingPlayers != 0){
int[] attackingPlayers = new int[numOfAttackingPlayers];
int[] defendingPlayers = new int[numOfDefendingPlayers];
for(int i = 0; i< numOfAttackingPlayers; i++){
attackingPlayers[i] = s.nextInt();
}
for(int i = 0; i< numOfDefendingPlayers; i++){
defendingPlayers[i] = s.nextInt();
}
mergeSort(attackingPlayers);
mergeSort(defendingPlayers);
if(attackingPlayers[0] < defendingPlayers[1]){
System.out.println("Y");
}
else
System.out.println("N");
numOfAttackingPlayers = s.nextInt();
numOfDefendingPlayers = s.nextInt();
}
}
}
Copy The Code &
Try With Live Editor
Input
500 700
700 500 500
2 2
200 400
200 1000
3 4
530 510 490
480 470 50 310
0 0
Output
Y
N
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int a,d;
cin>>a>>d;
while(a!=0 && d!=0){
int t[a],f[d];
int i;
for(i=0;i<a;i++){
cin>>t[i];
}
for(i=0;i<d;i++){
cin>>f[i];
}
sort(t,t+a);
sort(f,f+d);
if(t[0]<f[1]){
cout<<"Y"<<"\n";
}else{
cout<<"N"<<"\n";
}
cin>>a>>d;
}
}
Copy The Code &
Try With Live Editor
Input
500 700
700 500 500
2 2
200 400
200 1000
3 4
530 510 490
480 470 50 310
0 0
Output
Y
N
Demonstration
SPOJ Solution-He is offside!-Solution in C, C++, Java, Python