0% found this document useful (0 votes)
34 views11 pages

Byte Battle 2.0 Solutions

Bad

Uploaded by

koushal2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views11 pages

Byte Battle 2.0 Solutions

Bad

Uploaded by

koushal2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Date – 12th Sept, 2024

Byte Battle 2.0


1. Lonely Numbers
You are given size of an integer array as n and n following elements. A number x is lonely when it
appears only once, and no adjacent numbers (i.e. x + 1 and x - 1) appear in the array.

Return all lonely numbers seperated by space along with sorted in ascending order.

Input Format

• int n (size of the array)

• input n number of elements of array

Constraints

1 <= n <= 100000

Output Format

Integers with a space between them

Sample Input 0

10 6 5 8

Sample Output 0

8 10

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Scanner;

public class Solution {

public List<Integer> findLonely(int[] nums) {

Arrays.sort(nums);
Date – 12th Sept, 2024

int n = nums.length;

List<Integer> lonelyNumbers = new ArrayList<>();

for (int i = 0; i < n; i++) {

// Check if the number is unique and has no adjacent numbers

boolean isLonely = true;

if (i > 0 && (nums[i] == nums[i - 1] || nums[i] == nums[i - 1] + 1)) {

isLonely = false;

if (i < n - 1 && (nums[i] == nums[i + 1] || nums[i] == nums[i + 1] - 1)) {

isLonely = false;

if (isLonely) {

lonelyNumbers.add(nums[i]);

return lonelyNumbers;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Read the size of the array

int n = scanner.nextInt();
Date – 12th Sept, 2024

// Read the array elements

int[] nums = new int[n];

for (int i = 0; i < n; i++) {

nums[i] = scanner.nextInt();

// Create an instance of Solution and find lonely numbers

Solution solution = new Solution();

List<Integer> result = solution.findLonely(nums);

// Print the result

for (int num : result) {

System.out.print(num + " ");

scanner.close();

}
Date – 12th Sept, 2024

2. Ash and Binary Game


Ash friend KT Pandey has challenged him over cupcake to beat him in a game.

Initially they have a binary string s consisting of only characters 0 and 1.

Ash and KT Pandey makes alternating moves. Ash makes first move, KT Pandey makes the second
move, Ash makes the third move and so on...

During each move, the current player must choose two different adjacent characters of string s and
delete them. For example, if s=1011001 then the following moves are possible:

choose s1 and s2: 1011001→11001;

choose s2 and s3: 1011001→11001;

choose s4 and s5: 1011001→10101;

choose s6 and s7: 1011001→10110;

If a player can't make any move, they lose. Both players play optimally. You have to determine if Ash
can win.

Input Format

First line consist of integer t - number of testcases

Only line of each test case consist of a string s(consists of 0s and 1s)

Constraints

1<=t<=1000

1<=|s|<=100

Here |s| is length of the given string.

Output Format

For each test case print answer in the single line.

If Ash can win print YES. Otherwise print NO.

Beaware of the Case.

Sample Input 0
Date – 12th Sept, 2024

3
01

1111

0011

Sample Output 0

YES

NO

NO

Explanation 0

In the first test case after Ash move string s become empty and KT Pandey can not make any move.

In the second test case Ash can not make any move initially.

In the third test case after Ash move string s turn into 01 . Then, after KT Pandey move string s
become empty and Ash can not make any move.

import java.util.Scanner;

public class BinaryStringCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Read the number of test cases

int t = scanner.nextInt();

scanner.nextLine(); // Consume the newline character after the integer input

while (t-- > 0) {

// Read the binary string


Date – 12th Sept, 2024

String s = scanner.nextLine();

// Count occurrences of '0' and '1'

int count0 = 0;

int count1 = 0;

for (char c : s.toCharArray()) {

if (c == '0') {

count0++;

} else if (c == '1') {

count1++;

// Determine the result based on the counts

if (Math.min(count0, count1) % 2 == 1) {

System.out.println("YES");

} else {

System.out.println("NO");

scanner.close();

}
Date – 12th Sept, 2024

3. Raj's Destination Dilemma


Raj is planning his day and has a list of destinations to choose from. Each destination has a certain
excitement level. Raj wants to visit two destinations such that the sum of their excitement levels
matches a target excitement level he has in mind. He needs to determine which two destinations to
visit to achieve this target excitement.

Given a list of integers where each integer represents the excitement level of a destination, and an
integer representing the target excitement level Raj wants to achieve, your task is to return the
indices of the two destinations such that their combined excitement level equals the target.

Note: Each input will have exactly one solution, and Raj will not visit the same destination twice. The
output indices can be in any order.

Input Format

• List Item An integer n representing the number of destinations.

• List Item An array nums of n integers where nums[i] represents the excitement level of the i-
th destination.

• List Item An integer target representing the total excitement level Raj wants to achieve by
visiting exactly two destinations.

Constraints

Exactly one solution exists, and indices are distinct.

Output Format

• List Item Return an array containing the two indices of the destinations Raj should visit.

• List Item If no such pair exists, return [-1, -1].

Sample Input 0

1967

Sample Output 0

0,3

Sample Input 1
Date – 12th Sept, 2024

3
8 96 15

Sample Output 1

-1,-1

public class Club {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int n = in.nextInt();

int[] nums = new int[n];

for (int i = 0; i < n; i++) {

nums[i] = in.nextInt();

int target = in.nextInt();

int[] result = twoSum(nums, target);

System.out.println(result[0] + "," + result[1]);

public static int[] twoSum(int[] nums, int target) {

int[] arr = new int[2];

for (int i = 0; i < nums.length; i++) {

for (int j = i + 1; j < nums.length; j++) {

if (nums[i] + nums[j] == target) {


Date – 12th Sept, 2024

arr[0] = i;

arr[1] = j;

return arr; // Return immediately after finding the pair

return new int[]{-1, -1};

4. Power Digits
Preeti is playing a game with her brother Kabir, where she wins if her number's digits are so well-
behaved that when each is raised to its position power, they add up perfectly to the original number.
If not, her brother wins and gets to brag about his superior math skills!

Input Format

A positive integer n.

Constraints

1 ≤ n ≤ 10^6

Output Format

Print "Preeti" if Preeti wins. Otherwise, "Kabir".

Sample Input 0

135

Sample Output 0

Preeti

Sample Input 1
Date – 12th Sept, 2024

175

Sample Output 1

Preeti

import java.util.Scanner;

public class PowerDigits {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input the number

int num = scanner.nextInt();

int sum = 0;

int rem;

int n = num;

int len = 0;

// Calculate the length of the number

while (n != 0) {

len++;

n = n / 10;

// Restore the original number

n = num;

// Calculate the sum of digits powered with their respective positions


Date – 12th Sept, 2024

while (num > 0) {

rem = num % 10;

sum += Math.pow(rem, len);

num = num / 10;

len--;

// Check whether the sum is equal to the original number

if (sum == n) {

System.out.println("Preeti");

} else {

System.out.println("Kabir");

scanner.close();

You might also like