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);
}
}
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();
This is a program to simply return the min value and the number of time it occurs in the array. If the array is filled with {13, 28, 5, 11} it returns the min as 5 and the count as 1. However, When the array is filled with the given numbers it returns the count as 2, when it should be 1. How would I fix this? Thankyou
public class Test4{
public static void main(String[] args){
int[] array = {4, 20, 30, 4, 25, 25, 6, 2, 29, 27, 1, 29, 11, 6, 10, 17, 8};
findMin(array);
}
public static void findMin(int[] array) {
if (array == null || array.length < 1)
return;
int count = 0;
int min = array[0];
for (int i = 1; i <= array.length - 1; i++) {
if (min > array[i]) {
min = array[i];
if(min == array[i]){
count++;
}
}
}
System.out.println("The minimum is: " + min +"\n" + "The count is: " + count);
}
}
You should initialize the count to 1 and reset is to 1 whenever the current min value is changed:
int count = 1; // initial count should be 1
int min = array[0];
for (int i = 1; i <= array.length - 1; i++) {
if (min > array[i]) {
// new minimum - reset count to 1
min = array[i];
count = 1;
} else if (min == array[i]) {
// same minimum - increment count
count++;
}
}
Im trying to solve a google foobar challenge but I am stuck on how to change this to use recursion. any pointers would be helpful
public static int[] answer(int[] l, int t) {
// convert the array into a list
List<Integer> list = new ArrayList<>();
for (int i : l) {
list.add(i);
}
for (int i = 0; i < list.size(); i++) {
Integer n = list.get(i);
if (i >= 1) {
Integer nMinus1 = list.get(i - 1);
Integer nMinus2;
Integer nMinus3;
Integer nMinus4;
Integer nMinus5;
Integer nMinus6;
if (n + nMinus1 == t) {
// done
return new int[]{i - 1, i};
} else if (i >= 2) {
nMinus2 = list.get(i - 2);
if (n + nMinus1 + nMinus2 == t) {
// done
return new int[]{i - 2, i};
} else if (i >= 3) {
nMinus3 = list.get(i - 3);
if (n + nMinus1 + nMinus2 + nMinus3 == t) {
// done
return new int[]{i - 3, i};
} else if (i >= 4) {
nMinus4 = list.get(i - 4);
if (n + nMinus1 + nMinus2 + nMinus3 + nMinus4 == t) {
// done
return new int[]{i - 4, i};
} else if (i >= 5) {
nMinus5 = list.get(i - 5);
if (n + nMinus1 + nMinus2 + nMinus3 + nMinus4 + nMinus5 == t) {
// done
return new int[]{i - 5, i};
} else if (i >= 6) {
nMinus6 = list.get(i - 6);
if (n + nMinus1 + nMinus2 + nMinus3 + nMinus4 + nMinus5 + nMinus6 == t) {
// done
return new int[]{i - 6, i};
}
}
}
}
}
}
}
}
return new int[]{-1, -1};
}
Here is the question:
Given the list l as [4, 3, 5, 7, 8] and the key t as 12, the function answer(l, t) would return the list [0, 2] because the list l contains the sub-list [4, 3, 5] starting at index 0 and ending at index 2, for which 4 + 3 + 5 = 12, even though there is a shorter sequence that happens later in the list (5 + 7). On the other hand, given the list l as [1, 2, 3, 4] and the key t as 15, the function answer(l, t) would return [-1, -1] because there is no sub-list of list l that can be summed up to the given target value t = 15.
You probably don't need an arraylist. You could perform a double loop on the array l. Why do you want recursion?
You could do something like:
public static int[] answer(int[] l, int t) {
int[] rets = {-1, -1};
int sum=0;
for (int i=0; i<l.length; i++) {
sum=0;
for (int j=i; j<l.length; j++) {
sum+=l[j];
if (sum > t) break;
if (sum == t) {
rets[0] = i;
rets[1] = j;
return rets;
}
}
}
return rets;
}
Just submitted my solution.. it got accepted, so I know it was accepted by Google:
(I believe this will be slightly faster than above solution since it will break the loops and return if the target is greater than sum of all numbers in the array.)
public static int[] answer(int[] l, int t){
int sum =0;
List<Integer> indexes = new ArrayList<>();
for(int j=0;j<l.length;j++){
sum = 0;
indexes.clear();
for(int i=j;i<l.length;i++){
sum += l[i];
indexes.add(i);
if(sum >= t){
break;
}
}
if(sum == t){
break;
}
else if(sum > t){
continue;
}
else{
return new int[] {-1,-1};
}
}
int[] returnArray = new int[2];
if(indexes.size()>0){
returnArray[0] = indexes.get(0);
returnArray[1] = indexes.get(indexes.size()-1);
}
return returnArray;
}
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;
}