+ 2
Two kingdoms are at war. Kingdom 1 has N soldiers (numbered as 1 to N) and the war goes on for K days. Each day only one soldi.
Kingdom 1 can select one soldier from soldier number Ni to Nj. Ni and Nj are provided to you for each day. Selection criteria: Each soldier has 2 parameters - A & B. A soldier is/are selected if A is max. If more than one soldier has A max then the soldier with min. B (of the shortlisted soldiers) is/are selected. If more than one soldier is still available, then the soldier with least index (of the shortlisted soldiers) is selected. Print the soldier number selected for each day of the war.
6 Antworten
+ 1
Hey Can anyone explain the question. Its bit of confusing?
0
Input:
Line 1 contains number of soldiers of Kingdom1 => N
Line 2 contains N space-separated values of A
Line 3 contains N space-separated values of B
Line 4 contains number of days fight goes on => K
Next K lines contain space separated values of Ni and Nj
Output:
K lines contain soldier number selected for each day of the war.
Sample Input:
10
2 5 3 7 9 2 9 8 7 15
5 2 1 8 3 1 2 9 0 5
3
1 5
3 8
4 10
0
where is the prog....
0
import sys
def chkIfDigit(arg):# Validation
if not (arg.isdigit()):
print(arg,'Please enter only positive number')
sys.exit()
return arg
N = int(chkIfDigit(input('N : ')))# No of Soldiers
A = input('A : ').strip().split(' ')
A = list(map(lambda x: chkIfDigit(x), A))
B = input('B : ').strip().split(' ')
B = list(map(lambda x: chkIfDigit(x), B))
if (len(A) != len(B)) or len(B) != N:
print('Length of A,B and N sholud be same')
sys.exit()
K = int(chkIfDigit(input('K : ')))# No of Days
if K == '0':
print('K cannot be 0')
sys.exit()
for i in range(K):# Main Logic
ni,nj=int(chkIfDigit(input('ni : '))),int(chkIfDigit(input('nj : ')))
if ni == '0':
print('ni cannot be 0')
sys.exit()
if(ni>nj):
print('ni cannot be greater then nj')
sys.exit()
tempListA = A[ni:nj]
tempListB = B[ni:nj]
minB = []
maxA = max(tempListA)
for i in range(len(tempListA)):
if tempListA[i] == maxA:
minB.append(tempListB[i])
- 1
:(
- 1
function war() {
var params = arguments;
var arrSpace = [];
for(var i = 4; i < params.length; i++) {
arrSpace.push(params[i]);
}
var obj = {
a: 0,
b: 0,
index: 0
};
var valOfA = 0;
arrSpace.forEach(function (space){
for(var i = space[0]-1; i < space[1]; i++) {
if(obj.a === params[1][i]) {
if(obj.b > params[2][i]) {
obj.b = params[2][i];
obj.index = i+1;
}
}
if(valOfA < params[1][i] && params[1][i] > params[2][i]) {
valOfA = params[1][i];
obj.a = params[1][i];
obj.b = params[2][i];
obj.index = i + 1;
}
}
console.log(obj.index);
});
}
var numOfSol = 10;
var arrA = [2, 5, 3, 7, 9, 2, 9, 8, 7, 15];
var arrB = [5, 2, 1, 8, 3, 1, 2, 9, 0, 5];
var days = 3;
war(numOfSol, arrA, arrB, days, [1,5], [3, 8], [4, 10]);