Question Index

Write a program to find the remaining letter after striking out using the last stroke out character's value. Character value is nothing but 1 for A, 2 for B and so on. To start with cancel out the first letter.

As shown below for COREJAVA, first C is cancelled since it is the first letter, then J is cancelled since it is fourth letter (3 (value of C) + 1) from C, followed by R which is 11th letter (10 (value of J) + 1) from J and so on.

Note: Do not count already cancelled characters while moving.

Input (Character array) Output (Character)
['C', 'O', 'R', 'E', 'J', 'A', 'V', 'A'] *OREJAVA // Always Cancel First Character
*ORE*AVA // Move right by Last Cancelled Character Value ie. C or 3
*O*E*AVA // Move right by Last Cancelled Character Value ie. J or 10
*****A** // Repeat until only one character is present
Output = A
['D', 'O', 'T', 'N', 'E', 'T'] E
['H', 'I', 'B', 'E', 'R', 'N', 'A', 'T', 'E'] E
['P', 'E', 'R', 'L'] E
['D', 'A', 'B', 'C'] *ABC // Cancel First Character D
*A*C // Move right by D(or 4) positions
*A** // Move right by B(or 2) positions
Output = A

class FindRemainingLetterAfterStrikingOut2

{    public static void main(String s[])
        char input[] = {'C', 'O', 'R', 'E', 'J', 'A', 'V', 'A'};
        System.out.println("The remaining letter is : " + getRemainingLetter(input));


public static char getRemainingLetter(char[] input) {
//Write code here to get the remaining letter after striking out as explained above.



Topic: Learn Arrays And Loops

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App