In: Computer Science
I have one error and it encounters my split array and it should
output true or false
public class Main
{
private static int[] freq;
static boolean doubleOrNothing(int[] array, int size, int
i)
{
if (size <= 1)
return false;
if (2 * array[i] == array[i+1])
return true;
return doubleOrNothing(array, size - 1, i++);
}
/**
*
* @param word
* @param sep
*
*
* @param count
* @return
*/
public static String wordSeparator(String word, String sep, int
count)
{
if (count <= 0)
return "";
String new_word="";
if (count == 1)
return word;
return word + sep + wordSeparator(word, sep, count - 1);
}
static boolean mcNuggetNumber(int n) //checks for 3 or 7 piece
chicken nugget
{
boolean l = false, r = false;
if (n == 3 || n == 7)
return true;
if (n >= 3)
l = mcNuggetNumber(n - 3);
if (n >= 7)
r = mcNuggetNumber(n - 7);
return l || r;
}
static boolean splitArray(int arr[], int n, int k)
{
// An odd length array cannot be divided into pairs
if (n == 1)
return false;
// Create a frequency array to count occurrences
// of all remainders when divided by k.
// map freq = null;
// Count occurrences of all remainders
for (int i = 0; i < n; i++)
freq[arr[i] % k]++;
// Traverse input array and use freq[] to decide
// if given array can be divided in pairs
for (int i = 0; i < n; i++)
{
// Remainder of current element
int rem = arr[i] % k;
// If remainder with current element divides
// k into two halves.
if (2 * rem == k)
{
// Then there must be even occurrences of
// such remainder
if (freq[rem] % 2 != 0)
return false;
}
// If remainder is 0, then there must be two
// elements with 0 remainder
else if (rem == 0)
{
if (1 & freq[rem])
{
return false;
}
else
{
}
}
// Else number of occurrences of remainder
// must be equal to number of occurrences of
// k - remainder
else if (freq[rem] != freq[k - rem])
return false;
}
return true;
}
public static void main(String[] args) {
int a1[] ={1};
int a2[] = { 1,2 };
int a3[] = { 0,0,1 };
int a4[] = { 2,3,4 };
int a5[] = { 1,3,5,10 };
int a6[] = { 1,3,5,11 };
int a7[] = { 9,8,7,6,5,4,3,2,1 };
int a8[] = { 9,8,7,6,12,4,3,2,1 };
int i1[] = { 7 };
int i2[] = { 3,4 };
int i3[] = { 3,4,5 };
int i4[] = { 9,10,11 };
int o1[] = { 3,4 };
int o2[] = { 2,3,4 };
int o3[] = { 3,4,5,6 };
int o4[] = { 1,2,3,4,5,6 };
int s1[] = { 2, 2 };
int s2[] = { 2, 3 };
int s3[] = { 5, 2, 3 };
int s4[] = { 5, 2, 2 };
int s5[] = { 1, 1, 1, 1, 1, 1 };
int s6[] = { 1, 1, 1, 1, 1 };
int s8[] = { 1 };
int s9[] = { 3, 5 };
int s10[] = { 5, 3, 2 };
int s11[] = { 2, 2, 10, 10, 1, 1 };
int s12[] = { 1, 2, 2, 10, 10, 1, 1 };
int s13[] = { 1, 2, 3, 10, 10, 1, 1 };
System.out.println("Jones Robert ");
System.out.println("Assignment 3");
System.out.println("Recursion.");
System.out.println("All calls must result in true or
false.");
System.out.println();
System.out.println("Double Or Nothing." );
System.out.println("1. " + doubleOrNothing(a1, 1,0) );
System.out.println( "2. " + doubleOrNothing(a2, 2,0) );
System.out.println("3. " + doubleOrNothing(a3, 3,0) );
System.out.println("4. " + doubleOrNothing(a4, 3,0));
System.out.println("5. " + doubleOrNothing(a5, 4,0) );
System.out.println("6. " + doubleOrNothing(a6, 4,0) );
System.out.println("7. " + doubleOrNothing(a7, 9,0) );
System.out.println("8. " + doubleOrNothing(a8, 9,0) );
System.out.println();
System.out.println();
System.out.println("Word Separator.");
System.out.println("1. " + wordSeparator("Y", "X", 4));
System.out.println("2. " + wordSeparator("Y", "X", 2));
System.out.println("3. " + wordSeparator("Y", "X", 1));
System.out.println("4. " + wordSeparator("Y", "X", 0));
System.out.println();
System.out.println("McNugget Numbers" );
System.out.println(" 1. " + mcNuggetNumber(0));
System.out.println(" 2. " + mcNuggetNumber(1) );
System.out.println(" 3. " + mcNuggetNumber(2) );
System.out.println(" 4. " + mcNuggetNumber(3) );
System.out.println(" 5. " + mcNuggetNumber(4) );
System.out.println(" 6. " + mcNuggetNumber(5) );
System.out.println(" 7. " + mcNuggetNumber(6) );
System.out.println(" 8. " + mcNuggetNumber(7) );
System.out.println(" 9. " + mcNuggetNumber(8) );
System.out.println("10. " + mcNuggetNumber(9) );
System.out.println("11. " + mcNuggetNumber(10) );
System.out.println("12. " + mcNuggetNumber(11) );
System.out.println("13. " + mcNuggetNumber(12) );
System.out.println("14. " + mcNuggetNumber(13));
System.out.println("15. " + mcNuggetNumber(14));
System.out.println("16. " + mcNuggetNumber(15));
System.out.println("17. " + mcNuggetNumber(16));
System.out.println();
System.out.println ("Split the Array");
System.out.println ("1. " + splitArray (1));
System.out.println ("2. " + splitArray (2));
System.out.println ("3. " + splitArray (3));
System.out.println ("4. " + splitArray (4));
System.out.println ("5. " + splitArray (5));
System.out.println ("6. " + splitArray (6));
System.out.println ("7. " + splitArray (7));
System.out.println ("8. " + splitArray (8));
System.out.println ("9. " + splitArray (9));
System.out.println ("10. " + splitArray (10));
System.out.println ("11. " + splitArray (11));
System.out.println ("12. " + splitArray (12));
System.out.println ("13. " + splitArray (13));
System.out.println ("14. " + splitArray (14));
System.out.println ("15. " + splitArray (15));
System.out.println ("16. " + splitArray (16));
System.out.println();
}
private static String splitArray(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
}
I have an error
Hi,
Edited your given code for the pairs of array..it has compilation as well as run time error.i edited the code at many places..user hashmap for the frequency but you can chnage it with array as well.
Thanks
import java.util.HashMap;
public class Main
{
private static int[] freq;
static boolean doubleOrNothing(int[] array, int size, int i)
{
if (size <= 1)
return false;
if (2 * array[i] == array[i+1])
return true;
return doubleOrNothing(array, size - 1, i++);
}
/**
*
* @param word
* @param sep
*
*
* @param count
* @return
*/
public static String wordSeparator(String word, String sep, int count)
{
if (count <= 0)
return "";
String new_word="";
if (count == 1)
return word;
return word + sep + wordSeparator(word, sep, count - 1);
}
static boolean mcNuggetNumber(int n) //checks for 3 or 7 piece chicken nugget
{
boolean l = false, r = false;
if (n == 3 || n == 7)
return true;
if (n >= 3)
l = mcNuggetNumber(n - 3);
if (n >= 7)
r = mcNuggetNumber(n - 7);
return l || r;
}
static boolean splitArray(int arr[], int n, int k) // splitArray ({ 2, 2, 10, 10, 1, 1 },n=6,k=2)
{
// An odd length array cannot be divided into pairs
if (n % 2 == 1)
return false;
// Create a frequency array to count occurrences
// of all remainders when divided by k.
// map freq = null;
HashMap<Integer, Integer> hashmap = new HashMap<>();
// Count occurrences of all remainders
for (int i = 0; i < n; i++){
//freq[arr[i] % k]++;
int rem = ((arr[i] % k) + k) % k;
if (!hashmap.containsKey(rem)) {
hashmap.put(rem, 0);
}
hashmap.put(rem, hashmap.get(rem) + 1);
}
// Traverse input array and use freq[] to decide
// if given array can be divided in pairs
for (int i = 0; i < n; i++)
{
// Remainder of current element
//int rem = arr[i] % k;
int rem = ((arr[i] % k) + k) % k;
// If remainder with current element divides
// k into two halves.
if (2 * rem == k)
{
// Then there must be even occurrences of
// such remainder
//if (freq[rem] % 2 != 0)
//return false;
if (hashmap.get(rem) % 2 == 1)
return false;
}
// If remainder is 0, then there must be two
// elements with 0 remainder
else if (rem == 0)
{
/*if (1 & freq[rem])
{
return false;
}
else
{
} */
if (hashmap.get(rem) % 2 == 1)
return false;
}
// Else number of occurrences of remainder
// must be equal to number of occurrences of
// k - remainder
else
if (hashmap.get(k - rem) != hashmap.get(rem))
return false;
}
return true;
}
public static void main(String[] args) {
int a1[] ={1};
int a2[] = { 1,2 };
int a3[] = { 0,0,1 };
int a4[] = { 2,3,4 };
int a5[] = { 1,3,5,10 };
int a6[] = { 1,3,5,11 };
int a7[] = { 9,8,7,6,5,4,3,2,1 };
int a8[] = { 9,8,7,6,12,4,3,2,1 };
int i1[] = { 7 };
int i2[] = { 3,4 };
int i3[] = { 3,4,5 };
int i4[] = { 9,10,11 };
int o1[] = { 3,4 };
int o2[] = { 2,3,4 };
int o3[] = { 3,4,5,6 };
int o4[] = { 1,2,3,4,5,6 };
int s1[] = { 2, 2 };
int s2[] = { 2, 3 };
int s3[] = { 5, 2, 3 };
int s4[] = { 5, 2, 2 };
int s5[] = { 1, 1, 1, 1, 1, 1 };
int s6[] = { 1, 1, 1, 1, 1 };
int s8[] = { 1 };
int s9[] = { 3, 5 };
int s10[] = { 5, 3, 2 };
int s11[] = { 2, 2, 10, 10, 1, 1 };
int s12[] = { 1, 2, 2, 10, 10, 1, 1 };
int s13[] = { 1, 2, 3, 10, 10, 1, 1 };
System.out.println("Jones Robert ");
System.out.println("Assignment 3");
System.out.println("Recursion.");
System.out.println("All calls must result in true or false.");
System.out.println();
System.out.println("Double Or Nothing." );
/*
System.out.println("1. " + doubleOrNothing(a1, 1,0) );
System.out.println( "2. " + doubleOrNothing(a2, 2,0) );
System.out.println("3. " + doubleOrNothing(a3, 3,0) );
System.out.println("4. " + doubleOrNothing(a4, 3,0));
System.out.println("5. " + doubleOrNothing(a5, 4,0) );
System.out.println("6. " + doubleOrNothing(a6, 4,0) );
System.out.println("7. " + doubleOrNothing(a7, 9,0) );
System.out.println("8. " + doubleOrNothing(a8, 9,0) );
System.out.println();
System.out.println();
System.out.println("Word Separator.");
System.out.println("1. " + wordSeparator("Y", "X", 4));
System.out.println("2. " + wordSeparator("Y", "X", 2));
System.out.println("3. " + wordSeparator("Y", "X", 1));
System.out.println("4. " + wordSeparator("Y", "X", 0));
System.out.println();
System.out.println("McNugget Numbers" );
System.out.println(" 1. " + mcNuggetNumber(0));
System.out.println(" 2. " + mcNuggetNumber(1) );
System.out.println(" 3. " + mcNuggetNumber(2) );
System.out.println(" 4. " + mcNuggetNumber(3) );
System.out.println(" 5. " + mcNuggetNumber(4) );
System.out.println(" 6. " + mcNuggetNumber(5) );
System.out.println(" 7. " + mcNuggetNumber(6) );
System.out.println(" 8. " + mcNuggetNumber(7) );
System.out.println(" 9. " + mcNuggetNumber(8) );
System.out.println("10. " + mcNuggetNumber(9) );
System.out.println("11. " + mcNuggetNumber(10) );
System.out.println("12. " + mcNuggetNumber(11) );
System.out.println("13. " + mcNuggetNumber(12) );
System.out.println("14. " + mcNuggetNumber(13));
System.out.println("15. " + mcNuggetNumber(14));
System.out.println("16. " + mcNuggetNumber(15));
System.out.println("17. " + mcNuggetNumber(16));
System.out.println();
System.out.println ("Split the Array");
System.out.println ("1. " + splitArray (1));
System.out.println ("2. " + splitArray (2));
System.out.println ("3. " + splitArray (3));
System.out.println ("4. " + splitArray (4));
System.out.println ("5. " + splitArray (5));
System.out.println ("6. " + splitArray (6));
System.out.println ("7. " + splitArray (7));
System.out.println ("8. " + splitArray (8));
System.out.println ("9. " + splitArray (9));
System.out.println ("10. " + splitArray (10));
System.out.println ("11. " + splitArray (11));
System.out.println ("12. " + splitArray (12));
System.out.println ("13. " + splitArray (13));
System.out.println ("14. " + splitArray (14));
System.out.println ("15. " + splitArray (15)); */
System.out.println ("16. " + splitArray (s11,6,2));
System.out.println();
}
private static String splitArray(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}