Finding matching values in an array - java

I have been tasked to assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Its its going to be tested with the following inputs:
matchValue: 2, userValues: {2, 1, 2, 2}
matchValue: 0, userValues: {0, 0, 0, 0}
matchValue: 10, userValues: {20, 50, 70, 100}
import java.util.Scanner;
public class FindMatchValue {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_VALS = 4;
int[] userValues = new int[NUM_VALS];
int i;
int matchValue;
int numMatches = -99; // Assign numMatches with 0 before your for loop
matchValue = scnr.nextInt();
for (i = 0; i < userValues.length; ++i) {
userValues[i] = scnr.nextInt();
}
// Anything above this can't be changed.
numMatches = 0;
if (matchValue == numMatches) {
numMatches = numMatches + 1;
}
// Anything below this can't be changed.
System.out.println("matchValue: " + matchValue + ", numMatches: " + numMatches);
}
}
Your output matchValue: 2, numMatches: 0
Expected output matchValue: 2, numMatches: 3
Your output matchValue: 0, numMatches: 1
Expected output matchValue: 0, numMatches: 4
Your output matchValue: 10, numMatches: 0
Expected output matchValue: 10, numMatches: 0
The only way I can get a different input to work is changing numMatches from 0 to match one of the other values in matchValue but not all 3 at the same time.

You need to visit all elements of the array and increment the value of numMatches for each match.
numMatches = 0;
for (int x = 0; x < userValues.length; x++) {
if (matchValue == userValues[x]) {
numMatches = numMatches + 1;
}
}
Note: You can write numMatches = numMatches + 1 also as numMatches += 1.
A sample run after this change:
2
2 1 2 2
matchValue: 2, numMatches: 3

I think you only want to find number of values in array which are equal to a specified value. You can do it easily using java streams like this:
private static long getNumMatches(final int[] arr, final int matchValue) {
return Arrays.stream(arr)
.filter(i -> i == matchValue)
.count();
}
You can reuse this for as many input cases as you want:
int[] input1 = {2, 1, 2, 2};
int matchValue1 = 2;
int[] input2 = {0, 0, 0, 0};
int matchValue2 = 0;
int[] input3 = {20, 50, 70, 100};
int matchValue3 = 10;
System.out.println("matchValue: " + matchValue1 + ", numMatches: " + getNumMatches(input1, matchValue1));
System.out.println("matchValue: " + matchValue2 + ", numMatches: " + getNumMatches(input2, matchValue2));
System.out.println("matchValue: " + matchValue3 + ", numMatches: " + getNumMatches(input3, matchValue3));

Related

how to find the subarrays of the given array that matches the checkvalue?

find the subarrays that matches the given sum input under conditions
only one loop is allowed
cant repeat the same index for sum
no inbuild methods allowed
my Output:
int arr[] = { 2, 2, 1, 4, 1, 6, 8, 5, 1, 2, 1, 1, 1 };
int checkval = 3;
results:
the subarrays are:
index 0 to 1
index 3
index 3
index 3 to 3
index 8 to 10
index 9 to 11
Subarray with most elements is 3
Kindly help me to avoid duplicate printing of single index values
also find solution to not to use the same index for next sum,
incorrect
index 8 to 10
index 9 to 11
correct
index 8 to 10
**next index should start with 11 not 9**
index 9 to 11
public class Main {
public static void main(String[] args) {
int arr[] = { 2, 2, 1, 4, 1, 6, 8, 5, 1, 2, 1, 1, 1 };
int sum = 0;
int indexval = 0;
int checkval = 4;
int i = 0;
int len[] = new int[10];
int l = 0;
int lm = 0;
System.out.println("the subarrays are: ");
while (indexval <= arr.length) {
int temp=i;
if (i < arr.length)
sum += arr[i];
else
break;
if (sum <= checkval) {
if (sum == checkval) {
System.out.println("index " + indexval + " to " + i);
len[l++] = i - indexval + 1;
lm = (i - indexval + 1) > len[l] ? (i - indexval + 1) : len[l];
i = indexval;
indexval += 1;
sum = 0;
}
}
else if (arr[temp] == checkval) {
System.out.println("index " + i);
i = indexval;
indexval += 1;
sum = 0;
}
else if (sum > checkval) {
i = indexval;
indexval += 1;
sum = 0;
}
i += 1;
}
System.out.println("Subarray with most elements is " + lm);
}
}

Can I assign each element in a list to a separate variable?

I have a list of 16 numbers, and I want them set as variables n1-n16. I'm looking for something similar to How to assign each element of a list to a separate variable?, but for java.
My code is:
public void setVar()
{
//x = 16;
cardNumR = cardNum;
reversed = 0;
while(cardNumR != 0) {
digit = cardNumR % 10;
reversed = reversed * 10 + digit;
cardNumR /= 10;
}
ArrayList<Long> nums = new ArrayList<Long>(x);
for (int x = 16; x > 0; x--)
{
nums.add(reversed % 10);
reversed -= (reversed % 10);
reversed /= 10;
length = nums.size();
if (length == 16)
{
System.out.println(nums);
}
}
}
which gets me a result of:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
I want to take each of these elements and set n1 = 1, n2 = 2, n3 = 3 so on and so forth such that n16 = 6. I want to be able to do this at the end:
(2*n1) + n2 + (2*n3) + n4 + (2*n5) + n6 + (2*n7) + n8 + (2*n9) + n10 + (2*n11) + n12 + (2*n13) + n14 + (2*n15) + n16
Is there a way I can do this with a loop so that I don't have to do it one by one?
You don't need so many variables. Use a loop instead:
int sum = 0;
for(int i = 0; i < nums.size(); i++) {
if(i % 2 == 0) {
// odd index
sum += 2 * nums.get(i);
} else {
// even index
sum += nums.get(i);
}
}
Well I would just use an array
int [] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6};
or a list for that:
List<Integer> nums = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6);
and apply the following formula:
Arrays
int result = 0;
for(int i = 0; i < array.length; i++) {
result += (i % 2 == 0) ? 2 * array[i] : array[i];
}
or if you want to avoid the use of the modulus (%) operation:
int result = 0
for(int i = 0; i < array.length; i+=2)
result += 2 * array[i];
for(int i = 1; i < array.length; i+=2)
result += array[i];
Lists
int result = 0;
for(int i = 0; i < nums.size(); i++) {
result += (i % 2 == 0) ? 2 * nums.get(i) : nums.get(i);
}
without the modulus operation :
int result = 0
for(int i = 0; i < nums.size(); i+=2)
result += 2 * nums.get(i);
for(int i = 1; i < nums.size(); i+=2)
result += nums.get(i);
Java Streams:
int result = IntStream.range(0, nums.size())
.mapToLong(i -> (i % 2 == 0) ? 2 * nums.get(i) : nums.get(i))
.sum();

How to add two int array values together?

I am trying to add two int arrays together to get a sum. The first array contains 0000000000000000123456789 and the second array contains 0001111111111111111111111. The sum should be 1111111111111234567900. Also I'm trying to do this without BigInteger or BigDecimal.
for(int i=0; i<number1.length;i++){
add= number1[i]+number2[i];
if(plus>=10){
sum[i-1]+=add/10;
sum[i] = add%10;
}else{
sum[i]=add;
}
}
The output that is produced at the moment is 00011111111111112345678100. How can I fix my code so that the 8 becomes a 9?
This kinda works. I can think of a couple of cases where something like this would break, like if the arrays were like {9,9,9} and {9,9,9}, result would be {9,9,8} instead of {1,9,9,8}. It's a minor fix that is being left as an activity to the reader.
public static void main(String []args){
int[] number1 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9};
int[] number2 = {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int carry = 0, sum = 0;
int[] result = new int[number1.length];
for (int i = number1.length - 1; i >= 0 ; i--) {
sum = number1[i] + number2[i] + carry;
result[i] = sum%10;
carry = sum/10;
}
// [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 9, 0, 0]
System.out.println(Arrays.toString(result));
}
Try this
int[] number = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9};
int[] number2 = {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int[] result = new int[number.length+1];
int carry = 0;
for (int i = number.length - 1; i >= 0; i--) {
result[i+1] = number[i] + number2[i] + carry;
if (result[i+1] > 9) {
carry = result[i+1] - 9;
result[i+1] -= 10;
}
else {
carry = 0;
}
}
System.out.println(Arrays.toString(result));
This code should work for properly carrying numbers greater than 10 to the next up and keep the lower numbers!
int[] result = new int[number1.length+1];
for (int i=number1.length-1; i>=0;i--){
result[i+1] = number1[i] + number[i] + result[i+1];
if(result[i+1] >= 10){
result[i+1] -= 10; // it could never be more than 20 so this is ok and if it is 10 than it will still carry over to the next
result[i] = 1; // the ten carried to the next one
}
}
Similar to Seek Addo's answer, but avoids the errors found in his code that prevent the application from properly carrying the numbers.
This is the most compact way I could think to do it, and should yield the proper answers of
[1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,9,0,0] or 1111111111111234567900
The accepted answer is not good. This is my version of it (support for different array sizes):
public static int[] sumIntArrays(int[] n1, int[] n2) {
int n1Size = n1.length - 1, n2Size = n2.length - 1, indexSum = n1Size > n2Size ? n1Size + 2 : n2Size + 2, slack = 0, s;
int[] sum = new int[indexSum];
while (true) {
if (n1Size < 0 && n2Size < 0)
break;
s = (n1Size < 0 ? 0 : n1[n1Size--]) + (n2Size < 0 ? 0 : n2[n2Size--]) + slack;
if (s > 9) {
sum[--indexSum] = s % 10;
slack = s / 10;
} else {
sum[--indexSum] = s;
slack = 0;
}
}
if (slack != 0)
sum[0] = slack;
return sum;
}

Find elements in array whose sum is equal to 10 - Java

I want to find all the pairs of numbers from an array whose sum is equal to 10, and am trying to improve upon this bit of code here:
for (int j = 0; j < arrayOfIntegers.length - 1; j++)
{
for (int k = j + 1; k < arrayOfIntegers.length; k++)
{
int sum = arrayOfIntegers[j] + arrayOfIntegers[k];
if (sum == 10)
return j + "," + k;
}
}
However, I'm having trouble moving through the array. Here's what I have so far:
int[] arrayOfIntegers = {0, 5, 4, 6, 3, 7, 2, 10};
Arrays.sort(arrayOfIntegers);
System.out.println(Arrays.toString(arrayOfIntegers));
int left = arrayOfIntegers[0];
int right = (arrayOfIntegers[arrayOfIntegers.length - 1]);
while (left < right)
{
int sum = left + right;
if (sum == 10) //check to see if equal to 10
{
System.out.println(left + "," + right);
}
if (sum > 10) // if sum is more than 10, move to lesser number
{
right --;
}
if (sum < 10) // if sum is less than 10, move to greater number
{
left++;
}
} // end of while
Try this code by passing the value of the sum and array in which you want to find the pair of elements equals to a given sum using one for loop
private void pairofArrayElementsEqualstoGivenSum(int sum,Integer[] arr){
List numList = Arrays.asList(arr);
for (int i = 0; i < arr.length; i++) {
int num = sum - arr[i];
if (numList.contains(num)) {
System.out.println("" + arr[i] + " " + num + " = "+sum);
}
}
}
You need to capture the values as well as the indexes:
int[] arrayOfIntegers = {0, 5, 4, 6, 3, 7, 2, 10};
Arrays.sort(arrayOfIntegers);
System.out.println(Arrays.toString(arrayOfIntegers));
int left = 0;
int right = arrayOfIntegers.length - 1;
while (left < right)
{
int leftVal = arrayOfIntegers[left];
int rightVal = (arrayOfIntegers[right]);
int sum = leftVal + rightVal;
if (sum == 10) //check to see if equal to 10
{
System.out.println(arrayOfIntegers[left] + "," + arrayOfIntegers[right]);
right --;
left++;
}
if (sum > 10) // if sum is more than 10, move to lesser number
{
right --;
}
if (sum < 10) // if sum is less than 10, move to greater number
{
left++;
}
} // end of while
output:
[0, 2, 3, 4, 5, 6, 7, 10]
0,10
3,7
4,6
This is sample code with javascrypt. Someone can use it
var arr = [0, 5, 4, 6, 3, 7, 2, 10]
var arr1 = arr;
for(var a=0; a<arr.length;a++){
for(var b=0; b<arr.length; b++){
if(arr[a]+arr[b]===10 && a!==b){
console.log(arr[a]+" + "+arr[b])
arr.splice(a,1);
}
}
}
Java - Using single loop
public static void findElements() {
List<Integer> list = List.of(0, 5, 4, 6, 3, 7, 2, 10);
for (int i = 0; i < list.size(); i++) {
int sum = 0;
if (i < list.size() - 1) {
sum = list.get(i) + list.get(i + 1);
if (sum == 10) {
System.out.println("Element: " + list.get(i) + "," + list.get(i + 1));
}
} else {
if (list.get(i) == 10) {
System.out.println("Element: " + list.get(i));
}
}
}
}

Arrays Group Counter

Given a provided array, determine how many groups of a specified size exist.
For the array
[1,1,1,2,2,2,3,3,3,4,5,6,7]
there are 7 groups with at least one, 3 groups with at least 2, and 3 groups with at least 3. A group is a series of same values. 1 1 1 is a group of 3, but it also is a group of 1 and 2. To count as a group, all values must be the same. 1 1 1 is a group of 3 because there are 3 1s in a row.
I'm just curious, how would this be done?
Using java 8
long result = Stream.of(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7)
.collect(Collectors.groupingBy(i -> i))
.entrySet().stream()
.filter(entry -> entry.getValue().size() >= 1) // specify the size
.count();
System.out.println(result);
i used a set array where i looked at all the groups and did a loop to see each new number and if it was the same as the previous number. Which i have a group counter that is passed into a new array. Then the results are printed out.
public class Homework {
public static void main(String[] args) {
int[] array_list = {3, 3, 3, 3, 3, 9, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8};
int[] found_groups = new int[7];
int group_size = 1;
int num_changed = 0;
for (int i = 1; i < array_list.length; i++) {
if (array_list[i] == array_list[i-1]) {
group_size++;
if (group_size == 5) {
found_groups[0] = group_size;
}
if (group_size == 3) {
found_groups[2] = group_size;
found_groups[5] = group_size;
}
if (group_size == 4) {
found_groups[3] = group_size;
}
if (group_size == 2) {
found_groups[4] = group_size;
}
if (group_size ==8) {
found_groups[6] = group_size;
}
}
else if (array_list[i] != array_list[i-1]) {
num_changed++;
group_size = 1;
if (group_size == 1) {
found_groups[1] = group_size;
}
}
}
int group_one = 0;
int group_two = 0;
int group_three = 0;
int group_four = 0;
int group_five = 0;
int group_six = 0;
int group_one_final = 0;
for (int i = 0; i < found_groups.length; i++) {
if (found_groups[i] == 5) {
group_five++;
group_two++;
group_three++;
group_four++;
}
if (found_groups[i] == 1) {
group_one++;
group_one_final = group_one + num_changed;
}
if (found_groups[i] == 3) {
group_three++;
group_two++;
}
if (found_groups[i] == 4) {
group_four++;
group_two++;
group_three++;
}
if (found_groups[i] == 8) {
group_six++;
group_two++;
group_three++;
group_four++;
group_five++;
}
if (found_groups[i] == 2) {
group_two++;
}
}
System.out.println("number of ones groups: " + group_one_final);
System.out.println("number of two groups: " + group_two);
System.out.println("number of three groups: " + group_three );
System.out.println("number of fours groups: " + group_four);
System.out.println("number of fives groups: " + group_five);
System.out.println("number of sixes groups: " + group_six);
}
}

Categories