In Java, write a method named as getUncommonDigit with two dimensional array. (n
ID: 3884304 • Letter: I
Question
In Java, write a method named as getUncommonDigit with two dimensional array. (no library and no global data are allowed)
I have written a code below but I got stuck on it. It is okay if help me with entirely new code!
public static int[] getUncommonDigit(int[] ary) {
int[] ucdAry = null;
int ucdCount = 0;
//int[][] dcInfoAry = null;
int[][] dcInfoAry = new int[ary.length][10];
int temp;
for (int i = 0; i < ary.length; i++) {
temp = (ary[i] < 0) ? -ary[i] : ary[i];
do {
dcInfoAry[i][temp % 10] = 1;
temp /=10;
} while (temp != 0);
}
//running through a loop to add all array element values
//for each digit (i.e., index).
//if the individual sum is the same as 1, the digit is
//uncommon, otherwise, it is NOT!
//Extracting the digits [i.e., the indices after
//confirming its uncommon behavier.
return ucdAry;
}
Explanation / Answer
public class SubarraySum {
int subArraySum(int arr[], int n, int sum)
{
int curr_sum = arr[0], start = 0, i;
// Pick a starting point
for (i = 1; i <= n; i++)
{
// If curr_sum exceeds the sum, then remove the starting elements
while (curr_sum > sum && start < i-1)
{
curr_sum = curr_sum - arr[start];
start++;
}
// If curr_sum becomes equal to sum, then return true
if (curr_sum == sum)
{
int p = i-1;
System.out.println("Sum found between indexes " + start
+ " and " + p);
return 1;
}
// Add this element to curr_sum
if (i < n)
curr_sum = curr_sum + arr[i];
}
System.out.println("No subarray found");
return 0;
}
public static void main(String[] args)
{
SubarraySum arraysum = new SubarraySum();
int arr[] = {15, 0, -1, 1, 1, 5, 0, 23};
int n = arr.length;
int sum = 1;
arraysum.subArraySum(arr, n, sum);
}
}
output - Sum found between indexes 1 and 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.