The program is terminated and on the output screen, 0 is being displayed.
What is wrong?
A xylophone is a musical instrument that consists of a sequence of wooden bars. In order to make a sound, you have to strike one of the bars by a mallet.
For the purpose of this problem we will assume that our xylophone has exactly 50 bars. The bars are numbered from 1 to 50 in the order from the left to the right.
There are 7 notes: A, B, C, D, E, F, G. Each bar of our xylophone plays one of these notes. The notes repeat periodically. When you play the bars from the left to the right, you will hear the following sequence of notes: A, B, C, D, E, F, G, A, B, C, D, E, F, G, A, B, C, ...
In other words: bar 1 produces the note A, bar 2 plays B, bar 3 plays C, ..., bar 7 plays G, and then the sequence of notes starts repeating: bar 8 plays the note A again, bar 9 is B, and so on.
You have hit a sequence of bars. You are given their numbers in the int[] keys. Return the number of distinct notes you've hit.
I am assuming A, B, C, D, E, F, G as 0, 1, 2, 3, 4, 5, 6 respectively and I am counting if a new number between 0 to 6 is coming after modulus by 7 and I incremented the count if it's coming and adding it into the new array so that next time when it will come the count will not get incremented.
import java.util.*;
class Xylophone
{
public static void main(String[] args)
{
int[] rr={1,8,3};
int c = CountKeys(rr);
System.out.println(c);
}
public static int CountKeys(int[] keys)
{
int i,j,modu;// loop vaiables and a variable for modulus
boolean flag=true;
int[] newa = new int[1];// newarray to check if the notes already occurred
int lenOfNewa;
int count=0;// to count
int[] newrr;
modu = keys[0]%7;//taking the mod of 1st element and puttin it in new array to show that it already occurred
newa[0]=modu;
for(i=1;i<keys.length;i++)// as long as elements are there which has been hit
{
modu = keys[i]%7;// take the mod
lenOfNewa = newa.length;//keeping track of length of new array
j=0;
while(j<newa.length)
{
if(modu==newa[j])// If this modulus is already in array, No need to check further.
{
flag=false;
break;
}
j++;
}
if(flag)//If flag is true then it means modulus not in array...inc the count and add it to the new array for future
{
count++;
newrr = new int[lenOfNewa+1];
for(int k=0;k<newa.length;k++)
{
newrr[k] = newa[k];
}
newa=newrr;
newa[lenOfNewa]= modu;
}
}
return count;
}