Tanvir
Tanvir

Reputation: 43

Using Java check n value is appears n time and the number is in consecutive location in an given array

Question: Define an array to be packed if all its values are positive, each value n appears n times and all equal values are in consecutive locations. So for example, {2, 2, 3, 3, 3} is packed because 2 appears twice and 3 appears three times. But {2, 3, 2, 3, 3} is not packed because the 2s are not in consecutive locations. And {2, 2, 2, 3, 3, 3} is not packed because 2 appears three times. Write a method named isPacked that returns 1 if its array argument is packed, otherwise it returns 0. You may assume that the array is not null

If you are programming in Java or C#, the function signature is int isPacked(int[ ] a)

    public class isPackd{
  public static void main(String[] args){
    int result=isPacked(new int[ ] {2, 2, 1});
    System.out.println("Return: " + result);
    result=isPacked(new int[ ] {7, 7, 7, 7, 1, 7, 7, 7});
    System.out.println("Return: " + result);
  }//main()


  public static int isPacked(int[ ] a){

    for (int i=0; i<a.length; i++) {
      int chkHowManyTimes=howManyTimes(a, a.length, a[i]);
      if (chkHowManyTimes !=a[i]) {
        return 0;
      } // end of if
    } // end of for

    if (isConsecutive(a)==0) {
      return 0;
    } // end of if

    return 1; 
  }//isPacked()

  public static int howManyTimes(int[] array, int length, int findNumber){
    int count=0;

    for (int i=0; i<length; i++) {
      if (array[i]==findNumber) {
        count +=1;
      } // end of if
    } // end of for
    //System.out.println(count);
    return count;
  }//howMany..()  

  public static int isConsecutive(int[] a){
    //
    //need help here to write code for this section
    //
    return 1;
  }//isCon..()


}//class

I need help to check whether the number is in consecutive location or not.

Upvotes: 1

Views: 800

Answers (2)

asok
asok

Reputation: 151

public class Packed {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] a = { 7, 7, 7, 7, 7,7, 7 };
    int tt = isPacked(a);
    System.out.println(tt);

}

private static int isPacked(int[] a) {

    int n;
    int count = 0;
    int k = 0;
    int initialindex;
    int finalindex = 0;
    for (int i = 0; i < a.length; i++) {
        if (a[i] < 0) {
            return 0;
        }
        n = a[i];
        k = 0;
        initialindex = i;
        for (int j = 0; j < a.length; j++) {
            k++;
            if (n == a[j]) {
                i++;
                finalindex = k;
                count++;
            }
        }

        if (n != count || (finalindex - initialindex) != count) {
            return 0;
        }
        i--;
        count = 0;
    }
    return 1;
 }

}

Upvotes: 0

ShihabSoft
ShihabSoft

Reputation: 845

And then,here is the full code below. So, is there anything just feel free to ask here.

public class Consecutive
{
/***************************************************************************/
Author :- ShihabSoft
/***************************************************************************/
public static void main(String[] someArgs)
{
int[] args=new int[someArgs.length];
    for(int i=0;i<someArgs.length;i++)
        args[i]=Integer.parseInt(someArgs[i]);
        int swit=checkArrConsecutive(args);
        if(swit==0 || swit==1)
        System.out.println("All "+args.length+" values appeared accordingly in the array");

switch(swit)
{
    case 0:
        System.out.println("Array is not well packed.");
    break;
    case 1:
        System.out.println("Array is well packed");
}
}
//As per your needs we need two functions
static int checkArrConsecutive(int[] args)
{
int curr=0,temp=0;
if(args!=null)
{
int numIndex=0;
    for(int i=0;i<args.length;i++)
    {
        curr=args[i];
        temp=0;
        if(checkNoccursNtime(args,curr)==0)
        {
            System.out.println("Sorry the number :- "+curr+" at index '"+i+"' repeats more or less than '"+curr+"' times");
            return 2;
        }

        for(int j=numIndex;j<args.length;j++)
        {
        if(temp==curr)
            break;
        if(args[j]!=curr)
        {
            return 0;
        }
        else
            temp++;

        }
        if(curr!=(args.length!=(i+1) ? args[i+1] : args[i]))
        numIndex=i+1;

    }
return 1;


}
return 0;
}
static int checkNoccursNtime(int[] args,int n)
{
if(args!=null)
{
int curr=0,temp=0;
    temp=0;
    curr=n;
    for(int j=0;j<args.length;j++)
    {
        if(args[j]==curr && temp != curr)
            temp++ ;
        else if(args[j]==curr)
            return 0;
    }
return temp==curr ? 1 : 0;
}
return 0;
}

}

Just compile the above code and run passing values like this.

java Consecutive 5 5 5 5 5 1 2 2 3 3 3

Upvotes: 2

Related Questions