Looping through two arrays to check if equal - java

Trying to check if two arrays are equal, meaning same length and same elements in positions.
I've tried Arrays.equals(1,2) but it's still coming out as false, while it needs to be coming out as true.
I've tried to create a nested for loop to check each index but I am still getting false.
My code:
public boolean equals(double[] s) //for loop checking each element
{
if (s==null)
{
return false;
}
for (int i=0;i<data.length;i++)
{
for(int j=0;j<s.length;j++)
{
if (data[i]!=s[j])
{
return false;
}
}
}
return true;
}

Don't reinvent the wheel!
public boolean equals(double[] s) {
return Arrays.equals(s, data);
}
Arrays.equals() compares array lengths and each element.

if you want to see if they have the same elements but you don't care if they have the same order, sort them first.
Arrays.sort(data);
Arrays.sort(s);
return Arrays.equals(data,s);

You don't need a nested loop to check the elements. In fact, your code is wrong in a sense that it's checking all the elements from one array to another.
You might want to
// Check to make sure arrays have same length
if (data.length != s.length)
return false;
for (int i=0;i<data.length;i++)
{
if (data[i]!=s[i])
{
return false;
}
}
return true;

You can use as below :
if(arr1.length!=arr2.length)
return false;
for(int index=0;index<arr1.length;index++)
{
if (arr1[index]!=arr2[index])
return false;
}
return true;

Related

How do i refrence my validation method (true or false) when i makeing a conditional statment?

so i have an ArryList called "files"
and ive created a validation method for validating
if its when used you are callling on a valid index refrence:
// validation method of an ArrayList called "files"
public boolean validIndex(int index){
if(index >= 0 && index < files.size()){
return true;
}
else{
return false;
}
}
The insted of just calling on the get method
i want to be able to refrence "validIndex" method when calling an on an item from the arrayList.
// Trying to make this one work:
Public void listFile(int index){
if(validindex(index) = true){
file.get(index);
}
else{
System.out.println("Index: " + index + "is not valid!");
}
}
please help
Here is your code in a more concise way:
// validation method of an ArrayList called "files"
public boolean validIndex(int index){
return (index >= 0) && (index < files.size());
}
because
if (bool) return true;
else return false;
is the same thing as return bool;
For the other function, simply use:
// Trying to make this one work:
public void listFile(int index){ // Public should be public
if(validIndex(index)){ // No need for = true (which should be == true
// even if you use it) and validindex(index) should be
// validIndex(index) because that is the correct name of your function
file.get(index);
}
else {
System.out.println("Index: " + index + "is not valid!");
}
}
You can also look at some boolean logic to get more familiar with if statements and other things.
Hopefully my explanations and code comments were helpful!
The main problem here is that you are using = instead of ==on if(validindex(index) = true).
With = you are setting the value of validIndex(index) to true. And that does nothing.
What you are trying to do is compare, that is done with ==. However, comparing a boolean is pointless since the bool already is true or false. You simply can use if(validindex(index)).
The same can be said about the validIndex function. You have the following structure:
if ($YourLogic){
//logic is true
return true;
}
else{
//logic is false
return false;
}
As you can see, you are returning the same value your logic is. You can simply return it directly like this:
public boolean validIndex(int index){
return index >= 0 && index < files.size();
}

Partition Equal Subset Sum Top Down TLE

I am Solving Partition Equal Subset Sum of leetcode.
Problem States:
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Note:
Each of the array element will not exceed 100.
The array size will not exceed 200.
I Wrote the Code for the following as
class Solution {
public boolean canPartition(int[] nums) {
int sum=0;
for(int i=0;i<nums.length;i++)
{
sum=sum+nums[i];
}
if(sum%2!=0)
{
return false;
}
int target=sum/2;
return helper(nums,target,nums.length);
}
boolean helper(int nums[],int sum,int n)
{
if(sum==0)
{
return true;
}
if(sum<0)
{
return false;
}
if(n==0)
{
return false;
}
return helper(nums,sum-nums[n-1],n-1)||helper(nums,sum,n-1);
}
}
Notice that I did not included the condition
if(sum<nums[n-1])
{
return false;
}
in the base case as i have included
if(sum<0)
{
return false;
}
which is the same thing as it would just add 1 more recursive call and then return false.
This code works for 89 of the test cases but gives TLE error for
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,100]
Now if the modify the same code and include
if(sum<nums[n-1])
{
return false;
}
and remove
if(sum<0)
{
return false;
}
i.e
class Solution {
public boolean canPartition(int[] nums) {
int sum=0;
for(int i=0;i<nums.length;i++)
{
sum=sum+nums[i];
}
if(sum%2!=0)
{
return false;
}
int target=sum/2;
return helper(nums,target,nums.length);
}
boolean helper(int nums[],int sum,int n)
{
if(sum==0)
{
return true;
}
if(n==0)
{
return false;
}
if(nums[n-1]>sum)
{
return false;
}
return helper(nums,sum-nums[n-1],n-1)||helper(nums,sum,n-1);
}
}
The code works fine and passes all the Test Cases.
Since both the code are same how is that one extra recursive call giving me TLE?
Is there is something else ?
The test cases are really weak. First one should get TLE as you did not use dynamic programming but performed a plain backtrack. Second one should get WA. If you used memoization your complexity would be O( totalSum * arraySize) but the complexity of your code is O(2 ^ arraySize).
now observe you have two options in every state. Either you pick the element or not. Even if this condition is true if(nums[n-1]>sum) , the second option is valid - going forward not picking the item. But your second code ignores both the cases for that condition reducing the calls to the point your solution takes less than 1 millisecond which is faster than most correct solution. As there was no counter test cases for the second solution it passed.

How to check that all values are equal in array using recursion?

I am trying to solve this algorithm recursively; I want to check that all values in the array are the same (or equal to each other). If all values are equal, return true, if they are not, return false. My code is not passing any tests.
public boolean allEqual(int[] a, int start, int end){
if (start > end) return false;
if (a.length==0) return false;
if (start==end && a[start] == a[end]) return true;
if (a[start] != a[end]){
return false;
}
return allEqual(a, start++, end);
}
change
return allEqual(a, start++, end);
to
return allEqual(a, start+1, end);
start++ passes the original value of start to the recursive call (that's what post increment operator returns), so your recursion will never end and you are probably getting a StackOverflowError.
It may be the easiest to simply take the first value of the array and go through until a single value is not the same.
public void allEqual(int[] arr) {
if(arr.length==0) return false;
for(int i=0; i<arr.length; i++)
if(arr[0]!=arr[i]) return false;
return true;
}
Edit: Realized this answer is for doing it with recursion and my answer doesn't do that.
You can solve it using the classic divide and conquer method. Divide the array into halves until there are two elements, and then check if those are equal. Then conquer them and compare the values. Something like this:
class Ideone {
static boolean checkEquals(int a[], int start, int length) {
if (length==2)
return a[start]==a[start+1] && a[start]==a[0];
else
return checkEquals(a,start+0,length/2) &&
checkEquals(a,start+length/2,length/2);
}
public static void main (String[] args) {
int a[]={1,1,1,1,1,1,1,1};
System.out.println(checkEquals(a,0,8));
}
}
Executed here.

checking whether two sequences have the same values in the same order (beginning java)

I want to add a method to a class I made that checks whether the two sequences have the same values in the same order.
Here is what I have so far:
public class Sequence {
private int[] values;
public Sequence(int size) { values = new int[size]; }
public void set(int i, int n) { values[i] = n; }
}
public boolean equals (Sequence other)
...??
The first part of the class I think is correct but I'm having a lot of trouble with the method that tests if the values are in the same order. Ideas and feedback would be much appreciated :)
If you want to say wether 2 Sequences are equals, you can override equals method and hashCode to follow contract.
Example using Eclipse tool:
public class Sequence {
private int[] values;
public Sequence(int size) { values = new int[size]; }
public void set(int i, int n) { values[i] = n; }
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(values);
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Sequence other = (Sequence) obj;
if (!Arrays.equals(values, other.values))
return false;
return true;
}
}
Then in a main class you can do the following thing
public static void main(String args[]){
Sequence s = new Sequence(5);
Sequence s2 = new Sequence(5);// new Sequence(4)
s.set(0, 1);
s2.set(0, 1);
System.out.println(s.equals(s2));//will print true
}
You have to take care that if you use my comment code (new Sequence(4)) this will return false and perhaps is not what you want! Then you will have to implement your own equals and not autogenerated by ide.
Arrays have a built in .equals() method: .equals(int[], int[])
Very simple, but hope this helps.
public boolean equals (Sequence other) {
// if sizez are different, automatic false
if (this.getValues().length != other.getValues().length)
return false;
else
int[] array1 = other.getValues();
int[] array2 = this.getValues();
// if any indices are not equal, return false
for (int i = 0; i < other.getValues().length; i++){
if (array1[i] != array2[i])
return false;
}
// it not returned false, return true
return true;
}
First and foremost, you will need to make your .equals() method a member of your Sequence class. Otherwise, you will only have access to one Sequence object.
If you want to check if 2 arrays have the same elements in the same order, all you need to do is compare each element in turn. Is the first element of this one the same as the first element of the other, etc. When you come across a pair of elements that are different, you will be able to return false. Otherwise, you can return true when you have checked each pair of elements.
One issue you may encounter is arrays of different size. Depending on what you're trying to do, you may want to either return false immediately without checking the elements or stop when you reach the end of the shorter array. Based off your question, you probably want the former, but that depends on what problem you are trying to solve.
Your .equals() method will be able to access the values arrays of both this and other even if they aren't public. This is because .equals() as a function of the Sequence class is allowed to access all the members of Sequence, even in Sequence objects other than this.
With this information, you should be able to write your .equals() method.
public boolean equals (Sequence other){
int[] first = this.getValues();
int[] second = other.getValues();
boolean same = true;
if(first.length != second.length){
return false;
}
for(int i = 0; i < first.length; i++){
if(first[i] != second[i]){
return false;
}
}
return same;
}
Note: you will have to make the values array public in the Sequence class or add a getter method to the Sequence class...a getter would be the better option

check the values of the arraylist values in java

I'm trying to write a method that checks weather all the Objects in an ArrayList have the same value. For example, in the following code list1 should return true, and list2 should return false...
list1=[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
list2=[1,3,4,2,4,1,3,4,5,6,2,1,5,2,4,1]
What is the best way to write this method? Is there any quick methods to do this, or do I need to manually loop through the values?
So, you need to check if all the values in a list are the same?
boolean checkList(List<Integer> list) {
if (list.isEmpty())
return false;
int value = list.get(0);
for (int i = 1; i < list.size(); ++i) {
if (list.get(i) != value)
return false;
}
return true;
}
but I'd be careful about null values in the list, too...

Categories

Resources