Here we will discuss L&T off campus coding question asked in 2022 recruitment cycle. Problem on arrays for off campus recruitments.
L&T off Campus Question
Two players datta and akash are playing a particular game. There are N number of rounds, for each round maximum score is 10. Based on some task both the players will be assigned with scores. The one with greater score will get 1 point for each round. If both players are having same score then point in given to no one. So when all the rounds are completed, you need to tell who win’s or else match draw in case if same points.
Input:
N=4
Datta=[8,9,6,7]
Akash=[7,4,5,9]
Output:
datta wins
Explanation:
Compare the scores of both data array and akash array, (8,7) as 8>7 1 point is given to datta
Similarly for other scores
At the end datta’s score = 3
Akash’s score = 1
So winner is datta.
Algorithm for Solving
- Take in user input that is N (number of rounds).
- Take the scores of player 1
- take the scores of player 2
- Using loop iterate through all the scores of players and assigh points accordingly.
- At the end compare the point’s and declare the winner.
JAVA Code
package arrayproblems; import java.util.Scanner; class problem1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of rounds's N "); int N =sc.nextInt(); int datta[]=new int[N]; int akash[]=new int[N]; for(int i=0;i<N;i++) { datta[i]=sc.nextInt(); } for(int i=0;i<N;i++) { akash[i]=sc.nextInt(); } int dattascore=0; int akashscore=0; for(int i=0;i<datta.length;i++) { if(datta[i]>akash[i]) { dattascore=dattascore+1; } else if(akash[i]>datta[i]) { akashscore=akashscore+1; } } if(dattascore>akashscore) { System.out.println("datta is winner"); } else if(dattascore==akashscore) { System.out.println("match draw"); } else { System.out.println("akash is a winner"); } } }
Also Read
Now Crack Any TCS Coding Round
Pingback: Beginner's friendly top 5 array questions in java - Is It Actually
Pingback: Xoriant off campus coding question and solution - Is It Actually