So let's say I have an array that is {4 -2 1 2 3 -8 6 7 10 12}: I put it through this method to get a pair that adds up to a target number such as 10.
static int[] getCandidatePair(int A[], int target) {
int []pair = {0,0};
int size = A.length;
for (int j = 0; j < size; j++) {
for (int i = 0; i < size; i++){
if (A[i]+ A[j] == target) {
pair[0] = A[i];
pair[1]= A[j];
}//end if
}//end for i
}//end for j
return pair;
}//end get method
How do I make the outputted pair (4,6) instead of the (-2,12). I'm thinking I may have to sort the array in this function too?
static int[] getCandidatePair(int A[], int target) {
int []pair = {0,0};
int size = A.length;
for (int j = 0; j < size; j++) {
for (int i = 0; i < size; i++){
if ((A[i] + A[j]) == target) {
pair[0] = A[i];
pair[1]= A[j];
return pair;
}//end if
}//end for i
}//end for j
return pair;
}//end get method
Based on the information you provided, this might solve your problem. What was happening first was that your method wasn't returning the first pair it matched. This is because there was not a return statement in your if statement block. Hence, it missed the (4,6) pair and returned the last pair numbers which matched according to your input.
Related
I have an array with integers, I just have to count the pairs and return that counted value.
static int StockMerchant(int n , int arr[]){
int[] temp = new int[n];
int count = 0;
for(int i =0 ; i<n ; i++){
for(int j= i+1 ; j<n ; j++){
if(arr[i]==arr[j]){
arr[j] = '0';
arr[i] ='0';
count++;
}
}
}
return count;
}
when I try: 1 1 2 2 2: gives output 2
But when I change the input to 1 2 1 2 2: gives output 3
I want the output to be 2 in the second case also. please help.
*Sorry for my bad paint skills.
Hope I was able to point out your mistake:)
Here is my solution :
Its basically sorting the array first and checking if it matches the value increment i position to j+1 and j as i+1
static int StockMerchant(int n, int arr[]) {
Arrays.sort(arr);
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
i = j + 1;
j = i;
count++;
}
}
}
return count;
}
Here i'm assuming all your test inputs contains positive integers, this logic works for the positive values only.
static int StockMerchant(int n , int arr[]){
int starter = n>0 && arr[0] != 0 ? 0 : -1;
int temp = starter;
int count = 0;
Arrays.sort(arr);
for(int i =0 ; i<n ; i++){
if(temp == arr[i]){
temp = starter;
count++;
}else{
temp = arr[i];
}
}
return count;
}
I am attempting to write a recursive method that COUNTS the number of combinations of k size in an integer array.
I can easily do this for a known value k (e.g. 3) as so:
int[] arr = {1,2,3,4};
int count = 0;
for(int i = 0; i < arr.length; i++) {
for(int j = i+1; j < arr.length; j++) {
for(int k = j+1; k < arr.length; k++) {
count++;
}
}
}
However, I would like to be able to do this without a known k. I have found methods online that print the combinations using recursion, such as this one:
https://www.techiedelight.com/find-distinct-combinations-of-given-length/, but none that count them.
Simple case of modifying the method linked.
public static int recur(int[] A, int i, int k)
{
int count = 0;
if (k == 0) {
count++;
}
for (int j = i; j < A.length; j++) {
count = count + recur(A, j + 1, k - 1);
}
return count;
}
The problem is to count how many times my bubble sort algorithm switches the places of numbers. I tried to use a variable which increments by one each time, but I think there may be an error with scopes or something similar. Instead of returning a number such as 6 (which means numbers were swapped 6 times), it returns 0, which is what I initialized my variable as. How would I get my program to work?
public static int sort(int arr[]) {
int length = arr.length;
int temp;
int count = 0;
for (int i = 0; i < (length); i++) {
for (int j = 1; j < (length); j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
count++;
}
}
}
return count;
}
Looks like your algorithm is not optimized because of your for loop condition and initialization. As a result you are comparing an element with itself which is redundant.
Proper approach should be like below:
int yourCounter = 0;
for (int i = 0; i < length; i++)
for (int j = 1; j < length-i; j++)
if (arr[j - 1] > arr[j]) {
//swap code
//yourCounter++;
}
your counter should give you proper result then.
I would like to have an optimized java program to print the highest digits to the right of an array.
For eg: a[]={3,6,7,2,4,1}
output should be 7,4,1.
I wrote a program like below
class Rightlargest{
public static void main(String args[]){
int a[]={1,3,2,4,5,2};
int c[]=new int[20];
for(int i=0;i<a.length;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[i]<a[j]){
a[i]=a[j];
}
}
c[i]=a[i];
}
for(int i=0;i<c.length;i++)
{
if(c[i]!=c[i+1])
System.out.println(c[i]);
}
}
}
Even though I got the correct output, its throwing array out of bounds exception along with it.
Please advise.
When i equals of a.length-1, j takes value a.length and that is the problem.
The array has length of 5 elements. It means the last element has index of 4
Fix for : ArrayIndexOutOfBoundsException
i < c.length -1
for (int i = 0; i < c.length -1; i++) { // c.length -1
if (c[i] != c[i + 1])
System.out.println(c[i]);
}
This will not exceed the array index out of bounds. Your loop was executing 1 index more than array length.
As array length->20 and array index starts from 0, Your loop was iterating (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) making last index to be checked as 21 which was out of array bounds.
Working code with fix:
public class RightLargest {
public static void main(String args[]) {
int a[] = { 1, 3, 2, 4, 5, 2 };
int c[] = new int[20];
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] < a[j]) {
a[i] = a[j];
}
}
c[i] = a[i];
}
for (int i = 0; i < c.length -1; i++) {
if (c[i] != c[i + 1])
System.out.println(c[i]);
}
}
}
I am trying to create a program that will take an array of integers, say {2,0,32,0,0,8} that can be of any length, and make it so all of the nonzero numbers are to the left at the lower indexes, and all the zeros are moved to the end.
For example, {2,0,32,0,0,8} becomes {2,32,8,0,0,0}.
This array can be of any length and contain any nonnegative integers.
This is what I have so far:
public static int[] moveLeft(final int[] a) {
for (int i = 0; i < a.length; i++) {
if (a[i] != 0) {
for (int j = 0; j < a.length; j++) {
if (a[j] == 0) {
a[j] = a[i];
a[i] = 0;
}
}
}
}
return a;
}
However, when I do this, it doesn't work for the first and second characters. If I have {1,2,0,1} it will return {2,1,1,0} when it should return {1,2,1,0}. Any help?
Your inner loop should stop before index i. Change this
for (int j = 0; j < a.length; j++) {
to
for (int j = 0; j < i; j++) {
And then your code works for me.