[Solved] Lewis is working on a game designing project called as Ball Blast Java, C++, Python, JavaScript

Ball Blast: Lewis is working on a game designing project called as Ball Blast and he is facing issue in implementing the logic for the same. There are N number of balls having a number written on each ball. All the balls are arranged in a row and as per the rules of the game, if there are 3 balls in a row having ODD number on them then the first ball of the set will be removed. The same process is to be repeated for all the balls from left to right. At the end of the game, the numbers on the remaining balls is to be printed.

Example:

If there are 7 balls with numbers: 3, 8, 5, 7, 1, 4, 5 Then: 5, 7, 1 are the 3 balls in a row with odd numbers so, 5 should be removed. 3, 8, 7, 1, 4, 5 No other triplet of odd numbers.

But, If there are 7 balls with numbers: 3, 8, 5, 7, 1, 5, 4 Then: 3, 8, 5, 7, 1, 5, 4 ==> 3, 8, 7, 1, 5, 4 ==> 3, 8, 1, 5, 4

Sample Input 1:

5 4 3 1 3 6

Sample Output 1:

4 1 3 6

Sample Input 2:

6 7 4 5 3 1 4

Sample Output 2:

7 4 3 1 4

Sample Input 3:

4 2 5 3 4

Sample Output 3:

2 5 3 4

Input Format

First line will be the number of Balls N.

Second Line will be the numbers written on Balls.

Sample Input 1:

5 4 3 1 3 6 Constraints N > 0 where N is the number of Balls

Output Format

Output should be the numbers on the Remaining Balls after the Blast

Sample Output 1:

4 1 3 6

Sample Input

0 5 4 1 3 7 8

Sample Output

0 4 3 7 8

Solution

Steps:

  1. Find the first odd number
  2. Find the second odd number
  3. Find the third odd number
  4. Repeat until no more triplets of odd numbers are found

https://sauravhathi.github.io/lpu-cse/

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        sc.nextLine();
        String[] str = sc.nextLine().split(" ");

        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(str[i]);
        }

// https://sauravhathi.github.io/lpu-cse/ 
        int j = 0;
        while (j < arr.length - 2) {
            if (arr[j] % 2 == 1 && arr[j + 1] % 2 == 1 && arr[j + 2] % 2 == 1) {
                arr[j] = 0;
                j += 1;
            } else {
                j++;
            }
        }

        for (int k = 0; k < arr.length; k++) {
            if (arr[k] != 0) {
                System.out.print(arr[k] + " ");
            }
        }

    }

}
n = int(input())
arr = list(map(int, input().split()))

j = 0
while j < len(arr) - 2:
    if arr[j] % 2 == 1 and arr[j + 1] % 2 == 1 and arr[j + 2] % 2 == 1:
        arr[j] = 0
        j += 1
    else:
        j += 1

# https://sauravhathi.github.io/lpu-cse/ 

for k in range(len(arr)):
    if arr[k] != 0:
        print(arr[k], end=" ")
var n = parseInt(prompt());
var arr = prompt().split(" ");
var j = 0;
while (j < arr.length - 2) {
if (arr[j] % 2 == 1 && arr[j + 1] % 2 == 1 && arr[j + 2] % 2 == 1) {
    arr[j] = 0;
    j += 1;
} else {
    j++;
}
}

// https://sauravhathi.github.io/lpu-cse/ 

for (var k = 0; k < arr.length; k++) {
if (arr[k] != 0) {
    document.write(arr[k] + " ");
}
}
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    int j = 0;
    while (j < n - 2) {
        if (arr[j] % 2 == 1 && arr[j + 1] % 2 == 1 && arr[j + 2] % 2 == 1) {
            arr[j] = 0;
            j += 1;
        } else {
            j++;
        }
    }

    for (int k = 0; k < n; k++) {
        if (arr[k] != 0) {
            cout << arr[k] << " ";
        }
    }

}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *