I am dealing with the following problem. I am not looking for anyone to provide me a solution i am looking for some guidance to solving this problem. Here is what i have come up with so far.
I have basically tried to first put a ( around values that repeat. However i am getting a out of bounds error. I would really appreciate it if someone can push me towards the right path for coding a small algorithm that would handle this problem.
My code (in progress)
import java.util.Random;
public class Test {
public static void main(String[] args) {
int[] values = { 1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 6,
3, 1 };
boolean inRun = false;
for (int i = 0; i < values.length; i++) {
if (values[i] == values[i + 1] && values[i + 1] < values.length) {
System.out.print("(");
}
System.out.print(values[i]);
}
}
}
You need to iterate to all the array and if it found a pair then you iterate it again in a while loop until it find the non pair.
sample:
int[] values = { 1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 6, 3, 1 };
boolean inRun = false;
for (int i = 0; i < values.length; i++) {
if (i + 1 < values.length && values[i] == values[i + 1] )
{
System.out.print("(");
while (i + 1 < values.length && values[i] == values[i + 1] )
{
System.out.print(values[i++]);
}
System.out.print(values[i++]);
System.out.print(")");
}
System.out.print(values[i]);
}
result:
12(55)31243(2222)36(55)631
Your error is here,
if (values[i] == values[i + 1] && values[i + 1] < values.length) {
Because i + 1 isn't being tested for less then, or in the correct order -
if (i + 1 < values.length && values[i] == values[i + 1]) {
Or you could use,
for (int i = 0; i < values.length - 1; i++) { // the length of values - 1 so we can
// get the next value.
Related
I want to make a java program that asks for a number as input data and generates the following numerical series, the series must show the amount of numbers indicated by the user, but it will only be able to print the numbers from one to ten and then descending.
Input: 7
Output: 1, 2, 3, 4, 5, 6, 7
Input: 12
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9
Input: 22
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2
This is what I currently am working with:
System.out.print("Input number > ");
int number = sc.nextInt();
int j = 0;
boolean ascending = true;
for(int i = 1; i <= n4; i++){
if(ascending){
if(j >= 10){
j--;
ascending = false;
} else {
j++;
}
} else {
if (j <= 1){
j++;
ascending = true;
} else {
j--;
}
}
System.out.print(j + ", ");
}
The tricky bit in your sequence is that the limits (1 and 10) are printed twice. This means that there are not two, but three delta values in the sequence: +1, then 0, then -1. The following logic should be sufficient to produce the sequence:
int numLoops = sc.next();
int delta = +1;
int currentValue = 1;
StringBuilder result = new StringBuilder();
for (int i = 0; i < numLoops; i++) {
result.append(Integer.toString(currentValue) + ", ");
currentValue += delta;
if (currentValue >= 10) delta = (delta == 0) ? -1 : 0;
if (currentValue <= 1 ) delta = (delta == 0) ? +1 : 0;
}
System.out.println(result.toString());
Note that a StringBuilder instance is used to assemble the output rather than a series of calls to System.out. This is more stylish.
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();
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));
}
}
}
}
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);
}
}
Im struggling to get the below problem right for a quite a while sometime.
Given an array of ints, is it possible to choose a group of some of the ints, such that the group sums to the given target, with this additional constraint: if there are numbers in the array that are adjacent and the identical value, they must either all be chosen, or none of them chosen. For example, with the array {1, 2, 2, 2, 5, 2}, either all three 2's in the middle must be chosen or not, all as a group. (one loop can be used to find the extent of the identical values).
groupSumClump(0, {8, 2, 2, 1}, 9) → true
groupSumClump(0, {2, 4, 4, 8}, 14) → false
The code is at http://ideone.com/Udk5q
Failing test case scenarios:
groupSumClump(0, {2, 4, 4, 8}, 14) → false -->NegativeArraySizeException
groupSumClump(0, {8, 2, 2, 1}, 9) → true false --> X
i have really tried my best to make it work but alas its failing always.
Please Need your help SO experts to resolve this problem
I will be highly obliged and thankful to you,if you can spare a few minutes to look into my problem as well and help me in acheieving the desired solution.
Method "summate" logic is realy messed up.
It should look something like function "f" here:
Algorithm to find which numbers from a list of size n sum to another number
Quick and dirty fix for your code:
class Demo {
public static void main(String args[]) {
Demo.groupSumClump(0, new int[] { 2, 4, 4, 8 }, 14);
Demo.groupSumClump(0, new int[] {8, 2, 2, 1}, 9);
}
public static void groupSumClump(int start, int[] nums, int target) {
start = 0;
nums = adjacents(start, nums);
for (int a_number = 0; a_number < nums.length; a_number++) {
System.out.println("Array is " + nums[a_number]);
}
summate(nums, 0, 0, target);
System.out.println(false);
}
public static int[] adjacents(int start, int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
sum += nums[i] + nums[i + 1];
nums[i] = sum;
nums[i + 1] = 0;
}
}
return nums;
}
static void check(int sum, int target) {
if (sum == target) {
System.out.println(true);
System.exit(0);
}
}
static void summate(int[] numbers, int index, int sum, int target) {
check(sum, target);
if (index == numbers.length) {
return;
}
summate(numbers, index + 1, sum + numbers[index], target);
check(sum, target);
summate(numbers, index + 1, sum, target);
check(sum, target);
}
}
public boolean groupSumClump(int start, int[] nums, int target) {
if(start >= nums.length) return target == 0;
if(start < nums.length-1 && nums[start] == nums[start+1]){
int span = 0;
for(int i = start; i < nums.length && nums[i] == nums[start]; i++){
span++;
} if(groupSumClump(start + span, nums, target)) return true;
return groupSumClump(start + span, nums, target - nums[start] * span);
} if(groupSumClump(start + 1, nums, target-nums[start])) return true;
if(groupSumClump(start +1, nums, target)) return true;
return false;
}