Code:
public class twoSumSolution {
public int findFirstlocation(int[] a, int target) {
for (int i = a.length - 1; i > 0; i--) {
int temp = a[i];
for (int j = 0; j < i; j++) {
if (a[j] + temp == target) {
return j;
}
}
}
return -1;
}
public int findSecondlocation(int[] a, int first, int target) {
int holdtemp = a[first];
for (int i = 0; i < a.length; i++) {
if (holdtemp + a[i] == target) {
return i;
}
}
return -1;
}
}
public class TwoSum {
public static void main(String[] args) {
int[] nums = new int[] {
11,
2,
15,
7
};
int target = 9;
twoSumSolution s = new twoSumSolution();
int firstindex = s.findFirstlocation(nums, target);
int secondindex = s.findSecondlocation(nums, firstindex, target);
System.out.println("[" + firstindex + "," + secondindex + "]");
}
}
Error in LeetCode Compiler:
Line 7: error: cannot find symbol [in __Driver__.java]
int[] ret = new Solution().twoSum(param_1, param_2);
^
symbol: class Solution
location: class __DriverSolution__
Index output works well in any IDE such as Netbeans or Eclipse , but fails in leetCode compiler can someone guide me?Error is at line 7
Leetcode has the following solution signature which you should not change, that means you don't need to define the main function. Each leetcode problem comes with a default solution signature which you need to fill out with your solution, if you change the signature itself it will give you error.
eg. below is the default signature for the two-sum problem for Java. Assemble your code inside this twoSum function to make it work. You can add your functions if you need as shown.
class Solution {
public int[] twoSum(int[] nums, int target) {
// TODO: fill this out.
}
// eg. functions you can put inside the solution class.
public int findFirstlocation(int[] a , int target)
{
for (int i=a.length-1 ; i>0 ; i--)
{
int temp = a[i];
for(int j=0 ; j<i;j++){
if (a[j]+temp ==target){
return j;
}
}
}
return -1;
}
public int findSecondlocation(int[] a , int first ,int target)
{
int holdtemp = a[first];
for(int i=0 ; i<a.length ; i++){
if (holdtemp + a[i]==target){
return i ;
}
}
return -1;
}
}
Related
You are given an array A of integers and an integer k. Implement an algorithm that determines, in linear time, the smallest integer that appears at least k times in A.
I have been struggling with this problem for awhile, coding in Java, I need to use a HashTable to find the smallest integer that appears at least k times, it also must be in linear time.
This is what I attempted but it does not pass any of the tests
private static int problem1(int[] arr, int k)
{
// Implement me!
HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();
int ans = Integer.MAX_VALUE;
for (int i=0; i < arr.length; i++) {
if(table.containsKey(arr[i])) {
table.put(arr[i], table.get(arr[i]) + 1);
if (k <= table.get(arr[i])) {
ans = Math.min(ans, arr[i]);
}
}else{
table.put(arr[i], 1);
}
}
return ans;
}
Here is the empty code with all of the test cases:
import java.io.*;
import java.util.*;
public class Lab5
{
/**
* Problem 1: Find the smallest integer that appears at least k times.
*/
private static int problem1(int[] arr, int k)
{
// Implement me!
return 0;
}
/**
* Problem 2: Find two distinct indices i and j such that A[i] = A[j] and |i - j| <= k.
*/
private static int[] problem2(int[] arr, int k)
{
// Implement me!
int i = -1;
int j = -1;
return new int[] { i, j };
}
// ---------------------------------------------------------------------
// Do not change any of the code below!
private static final int LabNo = 5;
private static final String quarter = "Fall 2020";
private static final Random rng = new Random(123456);
private static boolean testProblem1(int[][] testCase)
{
int[] arr = testCase[0];
int k = testCase[1][0];
int answer = problem1(arr.clone(), k);
Arrays.sort(arr);
for (int i = 0, j = 0; i < arr.length; i = j)
{
for (; j < arr.length && arr[i] == arr[j]; j++) { }
if (j - i >= k)
{
return answer == arr[i];
}
}
return false; // Will never happen.
}
private static boolean testProblem2(int[][] testCase)
{
int[] arr = testCase[0];
int k = testCase[1][0];
int[] answer = problem2(arr.clone(), k);
if (answer == null || answer.length != 2)
{
return false;
}
Arrays.sort(answer);
// Check answer
int i = answer[0];
int j = answer[1];
return i != j
&& j - i <= k
&& i >= 0
&& j < arr.length
&& arr[i] == arr[j];
}
public static void main(String args[])
{
System.out.println("CS 302 -- " + quarter + " -- Lab " + LabNo);
testProblems(1);
testProblems(2);
}
private static void testProblems(int prob)
{
int noOfLines = prob == 1 ? 100000 : 500000;
System.out.println("-- -- -- -- --");
System.out.println(noOfLines + " test cases for problem " + prob + ".");
boolean passedAll = true;
for (int i = 1; i <= noOfLines; i++)
{
int[][] testCase = null;
boolean passed = false;
boolean exce = false;
try
{
switch (prob)
{
case 1:
testCase = createProblem1(i);
passed = testProblem1(testCase);
break;
case 2:
testCase = createProblem2(i);
passed = testProblem2(testCase);
break;
}
}
catch (Exception ex)
{
passed = false;
exce = true;
}
if (!passed)
{
System.out.println("Test " + i + " failed!" + (exce ? " (Exception)" : ""));
passedAll = false;
break;
}
}
if (passedAll)
{
System.out.println("All test passed.");
}
}
private static int[][] createProblem1(int testNo)
{
int size = rng.nextInt(Math.min(1000, testNo)) + 5;
int[] numbers = getRandomNumbers(size, size);
Arrays.sort(numbers);
int maxK = 0;
for (int i = 0, j = 0; i < size; i = j)
{
for (; j < size && numbers[i] == numbers[j]; j++) { }
maxK = Math.max(maxK, j - i);
}
int k = rng.nextInt(maxK) + 1;
shuffle(numbers);
return new int[][] { numbers, new int[] { k } };
}
private static int[][] createProblem2(int testNo)
{
int size = rng.nextInt(Math.min(1000, testNo)) + 5;
int[] numbers = getRandomNumbers(size, size);
int i = rng.nextInt(size);
int j = rng.nextInt(size - 1);
if (i <= j) j++;
numbers[i] = numbers[j];
return new int[][] { numbers, new int[] { Math.abs(i - j) } };
}
private static void shuffle(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
int rndInd = rng.nextInt(arr.length - i) + i;
int tmp = arr[i];
arr[i] = arr[rndInd];
arr[rndInd] = tmp;
}
}
private static int[] getRandomNumbers(int range, int size)
{
int numbers[] = new int[size];
for (int i = 0; i < size; i++)
{
numbers[i] = rng.nextInt(2 * range) - range;
}
return numbers;
}
}
private static int problem1(int[] arr, int k) {
// Implement me!
Map<Integer, Integer> table = new TreeMap<Integer, Integer>();
for (int i = 0; i < arr.length; i++) {
if (table.containsKey(arr[i])) {
table.put(arr[i], table.get(arr[i]) + 1);
} else {
table.put(arr[i], 1);
}
}
for (Map.Entry<Integer,Integer> entry : table.entrySet()) {
//As treemap is sorted, we return the first key with value >=k.
if(entry.getValue()>=k)
return entry.getKey();
}
//Not found
return -1;
}
As others have pointed out, there are a few mistakes. First, the line where you initialize ans,
int ans = 0;
You should initialize ans to Integer.MAX_VALUE so that when you find an integer that appears at least k times for the first time that ans gets set to that integer appropriately. Second, in your for loop, there's no reason to skip the first element while iterating the array so i should be initialized to 0 instead of 1. Also, in that same line, you want to iterate through the entire array, and in your loop's condition right now you have i < k when k is not the length of the array. The length of the array is denoted by arr.length so the condition should instead be i < arr.length. Third, in this line,
if (k < table.get(arr[i])){
where you are trying to check if an integer has occurred at least k times in the array so far while iterating through the array, the < operator should be changed to <= since the keyword here is at least k times, not "more than k times". Fourth, k should never change so you can get rid of this line of code,
k = table.get(arr[i]);
After applying all of those changes, your function should look like this:
private static int problem1(int[] arr, int k)
{
// Implement me!
HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();
int ans = Integer.MAX_VALUE;
for (int i=0; i < arr.length; i++) {
if(table.containsKey(arr[i])) {
table.put(arr[i], table.get(arr[i]) + 1);
if (k <= table.get(arr[i])) {
ans = Math.min(ans, arr[i]);
}
}else{
table.put(arr[i], 1);
}
}
return ans;
}
Pseudo code:
collect frequencies of each number in a Map<Integer, Integer> (number and its count)
set least to a large value
iterate over entries
ignore entry if its value is less than k
if entry key is less than current least, store it as least
return least
One line implementation:
private static int problem1(int[] arr, int k) {
return Arrays.stream(arr).boxed()
.collect(groupingBy(identity(), counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() >= k)
.map(Map.Entry::getKey)
.reduce(MAX_VALUE, Math::min);
}
This was able to pass all the cases! Thank you to everyone who helped!!
private static int problem1(int[] arr, int k)
{
// Implement me!
HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();
int ans = Integer.MAX_VALUE;
for (int i=0; i < arr.length; i++) {
if(table.containsKey(arr[i])) {
table.put(arr[i], table.get(arr[i]) + 1);
}else{
table.put(arr[i], 1);
}
}
Set<Integer> keys = table.keySet();
for(int i : keys){
if(table.get(i) >= k){
ans = Math.min(ans,i);
}
}
if(ans != Integer.MAX_VALUE){
return ans;
}else{
return 0;
}
}
I'm new in Java programming and I'm trying to write a method that orders an array in crescent or decrescent mode.
public static int[] orderArray(int[] v, boolean mode) {
int newArray[] = {};
for (int i=0; i<v.length; i++) if (mode) newArray[i] = getMin(v, i); else newArray[i] = getMax(v, i);
return newArray;
}
So in another class I put:
int[] myArray = {5,3,4,1,6};
//Call the method
int[] newOrdArray = Vettori.orderArray(myArray, true);
System.out.println(Vettori.printArray(newOrdArray, ","));
There are some methods that I wrote before and I'm using to reach my objective.
I mean the methods: printArray, getMin and getMax
public static int getMin(int[] v, int pos) { //getMin
int minimum = 0;
if (v.length>0)
for (int i=pos;i<v.length;i++) {
if(i==pos) minimum=v[i];
else if(v[i]<=minimum) minimum=v[i];
}
return minimum;
}
public static int getMax(int[] v, int pos) { //getMax
int max = 0;
if (v.length>0)
for (int i=pos;i<v.length;i++) {
if(i==pos) max=v[i];
else if(v[i]>=max) max=v[i];
}
return max;
}
public static String printArray(int[] v, String separator) { //printArray
String stampa = "";
if (v.length>0) {
boolean insert_sep = true;
for (int i=0;i<v.length;i++) {
insert_sep = v[i]!=v.length;
if (insert_sep) stampa+=Integer.toString(v[i])+separator;
else stampa+=Integer.toString(v[i]);
}
stampa = stampa.substring(0, stampa.length()-1);
} else stampa = "Invalid array!";
return stampa;
}
When I compile the code and try to ordinate the array, the program throws the java.lang.ArrayIndexOutOfBoundsException exception. I'm trying to find the issue in my code, but I can't...
Here the output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Utili.Vettori.ordinaVettore(Vettori.java:335)
at mainProgram.main(mainProgram.java:24)
Initialize array in orderArray method before adding any elements:
int[] newArray = new int[v.length];
Your problem lies in the orderArray method where you have created an array of size 0 then attempted to access elements of it causing an ArrayIndexOutOfBoundsException.
Change line
int newArray[] = {};
to create a new integer array of the same size as your method parameter v.
int newArray[] = new int[v.length];
Your resulting method should look like this
public static int[] orderArray(int[] v, boolean mode) {
int newArray[] = new int[v.length];
for (int i=0; i<v.length; i++) if (mode) newArray[i] = getMin(v, i); else newArray[i] = getMax(v, i);
return newArray;
}
found your problem
int newArray[] = {};
for (int i=0; i<v.length; i++) if (mode) newArray[i] = getMin(v, i);
else newArray[i] = getMax(v, i);
you are creating empty array, and then try to access fields which are not exist
The problem is simple, on your "orderArray(int[] v, boolean mode)" method, you did not initialize your new array, and thus you get ArrayOutOfBoundException.
Here, you have to make these changes, I've commented out the errorneous line and added a new line for the array initialization;
// int newArray[] = {};
int newArray[] = new int[v.length];
The complete corrected code is as below;
public class TestArrayOrder {
public static void main(String[] args) {
int[] myArray = { 5, 3, 4, 1, 6 };
// Call the method
int[] newOrdArray = orderArray(myArray, true);
System.out.println(printArray(newOrdArray, ","));
}
public static int[] orderArray(int[] v, boolean mode) {
// int newArray[] = {}; //you should initialize the array
int newArray[] = new int[v.length]; // array initialization
for (int i = 0; i < v.length; i++)
if (mode)
newArray[i] = getMin(v, i);
else
newArray[i] = getMax(v, i);
return newArray;
}
public static int getMin(int[] v, int pos) { // getMin
int minimum = 0;
if (v.length > 0)
for (int i = pos; i < v.length; i++) {
if (i == pos)
minimum = v[i];
else if (v[i] <= minimum)
minimum = v[i];
}
return minimum;
}
public static int getMax(int[] v, int pos) { // getMax
int max = 0;
if (v.length > 0)
for (int i = pos; i < v.length; i++) {
if (i == pos)
max = v[i];
else if (v[i] >= max)
max = v[i];
}
return max;
}
public static String printArray(int[] v, String separator) { // printArray
String stampa = "";
if (v.length > 0) {
boolean insert_sep = true;
for (int i = 0; i < v.length; i++) {
insert_sep = v[i] != v.length;
if (insert_sep)
stampa += Integer.toString(v[i]) + separator;
else
stampa += Integer.toString(v[i]);
}
stampa = stampa.substring(0, stampa.length() - 1);
} else
stampa = "Invalid array!";
return stampa;
}
}
Here is the output for the "true" mode call of your orderArray method;
1,1,1,1,6
And the output for "false" mode;
6,6,6,6,6
Hope that it helps.
I have made this program using array concept in java. I am getting Exception as ArrayIndexOutOfBound while trying to generate product.
I made the function generateFNos(int max) to generate factors of the given number. For example a number 6 will have factors 1,2,3,6. Now,i tried to combine the first and the last digit so that the product becomes equal to 6.
I have not used the logic of finding the smallest number in that array right now. I will do it later.
Question is Why i am getting Exception as ArrayIndexOutOfBound? [i couldn't figure out]
Below is my code
public class SmallestNoProduct {
public static void generateFNos(int max) {
int ar[] = new int[max];
int k = 0;
for (int i = 1; i <= max; i++) {
if (max % i == 0) {
ar[k] = i;
k++;
}
}
smallestNoProduct(ar);
}
public static void smallestNoProduct(int x[]) {
int j[] = new int[x.length];
int p = x.length;
for (int d = 0; d < p / 2;) {
String t = x[d++] + "" + x[p--];
int i = Integer.parseInt(t);
j[d] = i;
}
for (int u = 0; u < j.length; u++) {
System.out.println(j[u]);
}
}
public static void main(String s[]) {
generateFNos(6);
}
}
****OutputShown****
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at SmallestNoProduct.smallestNoProduct(SmallestNoProduct.java:36)
at SmallestNoProduct.generateFNos(SmallestNoProduct.java:27)
at SmallestNoProduct.main(SmallestNoProduct.java:52)
#Edit
The improved Code using array only.
public class SmallestNoProduct {
public static void generateFNos(int max) {
int s = 0;
int ar[] = new int[max];
int k = 0;
for (int i = 1; i <= max; i++) {
if (max % i == 0) {
ar[k] = i;
k++;
s++;
}
}
for (int g = 0; g < s; g++) {
System.out.println(ar[g]);
}
smallestNoProduct(ar, s);
}
public static void smallestNoProduct(int x[], int s) {
int j[] = new int[x.length];
int p = s - 1;
for (int d = 0; d < p;) {
String t = x[d++] + "" + x[p--];
System.out.println(t);
int i = Integer.parseInt(t);
j[d] = i;
}
/*for (int u = 0; u < j.length; u++) {
System.out.println(j[u]);
}*/
}
public static void main(String s[]) {
generateFNos(6);
}
}
Maybe it better:
public class SmallestNoProduct {
public static int smallest(int n) {
int small = n*n;
for(int i = 1; i < Math.sqrt(n); i++) {
if(n%i == 0) {
int temp = Integer.parseInt(""+i+""+n/i);
int temp2 = Integer.parseInt(""+n/i+""+i);
temp = temp2 < temp? temp2: temp;
if(temp < small) {
small = temp;
}
}
}
return small;
}
public static void main(String[] args) {
System.out.println(smallest(6)); //6
System.out.println(smallest(10)); //25
System.out.println(smallest(100)); //205
}
}
Problem lies in this line
String t=x[d++]+""+x[p--];
x[p--] will try to fetch 7th position value, as p is length of array x i.e. 6 which results in ArrayIndexOutOfBound exception. Array index starts from 0, so max position is 5 and not 6.
You can refer this question regarding postfix expression.
Note: I haven't checked your logic, this answer is only to point out the cause of exception.
We are unnecessarily using array here...
below method should work....
public int getSmallerMultiplier(int n)
{
if(n >0 && n <10) // if n is 6
return (1*10+n); // it will be always (1*10+6) - we cannot find smallest number than this
else
{
int number =10;
while(true)
{
//loop throuogh the digits of n and check for their multiplication
number++;
}
}
}
int num = n;
for(i=9;i>1;i--)
{
while(n%d==0)
{
n=n/d;
arr[i++] = d;
}
}
if(num<=9)
arr[i++] = 1;
//printing array in reverse order;
for(j=i-1;j>=0;j--)
system.out.println(arr[j]);
I have 2 classes written in java here they are
public class MinHeapify{
public MinHeapify(String a[], int start, int end){
minHeapify(a,start,end);
}
public static void exchange(String a[],int i,int j) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static int parent(int i) {
return (int) Math.floor((i - 1)/2);
}
public static int left(int i) {
return 2*i + 1;
}
public static int right(int i) {
return 2*(i+1);
}
public static void minHeapify(String a[], int start,int end) {
int l = left(start); int r = right(start);
int smallest=0;
if(l >= end){
smallest = (l < start)? l: start;
}
if(r >= end){
smallest = (r < smallest)? r: smallest;
}
if(smallest != start) {
exchange(a,start,smallest);
minHeapify(a,smallest,end);
}
}
}
import java.util.Scanner;
public class ReservationArray{
private String [] array = new String [50];
public ReservationArray(String [] arg){
int next = 0;
int next2 = 0;
while(arg[++next]!= null){
if(next != arg.length){
arg[next] = array[next2];
next = next++;
next2 = next2++;
}
else if(next == array.length && next!= arg.length){
String [] temp = new String[2*array.length+1];
for(int i=0; i<next; i++){
temp[i] = array[i];
array = temp;
temp = null;
}
} else{
if(next < (array.length/2)-1){
String [] temp = new String[1/2*array.length-1];
for(int i=0; i<next; i++){
temp[i] = array[i];
array = temp;
temp = null;
}
}
}
}
minHeapify(array, 0, array.length-1);
}
The error that I get when i try and compile it is " The method minHeapify(java.lang.String[], int, int) is undefined for the type ReservationArray" and I do not understand what that means, am I missing something?
MinHeapify mh = new MinHeapify();
mh.minHeapify(array, 0, array.length-1);
Since your method is static, you should call this way.
MinHeapify.(array, 0, array.length-1);
add the return type to it
public void MinHeapify(String a[], int start, int end)
and
MinHeapify.minHeapify(array, 0, array.length - 1);
The method minHeapify belongs to the MinHeapifyclass rather than ReservationArray, hence the error. As its static, you can use:
MinHeapify.minHeapify(array, 0, array.length - 1);
Alternatively, you could use a static import:
import static com.your.package.MinHeapify.minHeapify;
And use the current unqualified method call notation.
Well minHeapify is a method is the class MinHeapify. You can't access it in the class ReservationArray unless you use the reference to MinHeapify to access it.
The purpose of this program is to find the kth smallest element in an array without sorting the array using a recursive and nonrecursive decrease and conquer type method.
I was hoping someone could look over my code and try to help me with my array out of bounds error(s).
The method that is throwing these errors is the recursive selection the non recursive selection works fine.
My driver is also attached and everything should compile if you want to test my code.
public class KthSmallest
{
private int counter;
private int term;
private int[] A;
int SelectionNonRecursive(int A[], int kthSmallest, int sizeOfA)
{
this.A = A;
if(kthSmallest == 1 || kthSmallest == sizeOfA)
{
return (LinearSearch(kthSmallest, sizeOfA));
}
else
{
for(int i = 0; i<sizeOfA; i++)
{
counter = 0;
for(int j = 0; j<sizeOfA; j++)
{
if(A[i] < A[j])
{
counter++;
}
}
if((sizeOfA - counter) == kthSmallest)
{
return A[i];
}
}
}
return 0;
}
int SelectionRecursive(int A[], int kthSmallest, int sizeOfA)
{
this.A = A;
return Selection_R(0, sizeOfA - 1, kthSmallest);
}
int Selection_R(int l, int r, int kthSmallest)
{
if(l<r)
{
if(kthSmallest == 1 || kthSmallest == A.length)
{
return (LinearSearch(kthSmallest, A.length));
}
else
{
int s = LomutoPartition(l, r);
if(s == kthSmallest - 1)
{
return A[s];
}
else if(s > (A[0] + kthSmallest - 1))
{
Selection_R(l, s-1, kthSmallest);
}
else
{
Selection_R(s+1, r, kthSmallest);
}
}
}
return 0;
}
int LomutoPartition(int l, int r)
{
int pivot = A[l];
int s = l;
for(int i = l+1; i<r; i++)
{
if(A[i] < pivot)
{
s += 1;
swap(A[s], A[i]);
}
}
swap(A[l], A[s]);
return s;
}
public void swap(int i, int j)
{
int holder = A[i];
A[i] = A[j];
A[j] = holder;
}
int LinearSearch(int kthSmallest, int sizeOfA)
{
term = A[0];
for(int i=1; i<sizeOfA; i++)
{
if(kthSmallest == 1)
{
if(term > A[i])
{
term = A[i];
}
}
else
{
if(term < A[i])
{
term = A[i];
}
}
}
return term;
}
}
public class KthDriver
{
public static void main(String[] args)
{
KthSmallest k1 = new KthSmallest();
int[] array = {7,1,5,9,3};
System.out.print(k1.SelectionRecursive(array, 3, array.length));
}
}
Inside your LomutoPartition method, you are passing the array elements in your swap method: -
swap(A[s], A[i]); // Inside for loop
and
swap(A[l], A[s]); // Outside for loop
And your swap method considers them as indices: -
public void swap(int i, int j) <-- // `i` and `j` are elements A[s] and A[i]
{
int holder = A[i]; <-- // You are accessing them as indices(A[i] -> A[A[s]])
A[i] = A[j];
A[j] = holder;
}
That is why you are getting that exception. Because, if any element in array is greater than size, it will blast out.
You should change your invocation to: -
swap(s, i); // Inside for loop
and
swap(l, s); // Outside for loop
respectively. And leave your method as it is.
Note that, you should pass array indices, and not array elements. If you pass array elements, then the swapping in the method will not be reflected in your array. Because, your method will have its own copy of your elements.