How can I print out n numbers that satisfy the condition - java

I have written a code that would return either 1 or 4 as the output, and here it is
public static int findSum(int k) {
int sum = 0;
boolean foundNonZero = false;
for (; k != 0; k /= 10) {
if (foundNonZero) {
sum += (k % 10)*(k % 10);
} else {
foundNonZero = k % 10 != 0;
}
}
return sum;
}
Now, I'm trying to write a function public static long[] firstK(int k) that would return k numbers starting from 0 that would satisfy the condition that findsum = 1.
I'm struggling to understand how to do it, despite reading Java syntax and information. and here is my code so far:
public static long[] firstK(int k) {
while (int x = 0;) {
if (findSum(x) = 1;)
System.out.println(x);
}
I know that int(k) isn't used in this, but I have no idea how to implement it. Any help would be greatly appreciated :)
Thank you,
Dave Shah, MUIC

Maybe something like this:
public static List<Integer> firstK(int k) {
List<Integer> result = new ArrayList<>();
for (int x = 0 ; result.size() < k ; x++)
if (findSum(x) == 1)
result.add(k);
return result;
}
If you really want an array rather than a List, I would still use a List to compute the result. I would then create an array from that list and return that as the function's result to have the function return a int[] or long[]. I can't see why you'd want to return long[] or List<Long> rather than int[] or List<Integer>, I doubt the code involved would ever be given the chance to iterate past the maximum integer value.
UPDATE: Here's a complete example that uses an array throughout:
public class Test {
public static int findSum(int k) {
int sum = 0;
boolean foundNonZero = false;
for (; k != 0; k /= 10) {
if (foundNonZero) {
sum += (k % 10)*(k % 10);
} else {
foundNonZero = k % 10 != 0;
}
}
return sum;
}
public static long[] firstK(int k) {
long[] result = new long[k];
int i = 0;
for (int x = 0; i < k; x++)
if (findSum(x) == 1)
result[i++] = x;
return result;
}
public static void main(String[] args) {
long[] r = firstK(5);
System.out.println(Arrays.toString(r));
}
}
Result:
[11, 12, 13, 14, 15]
The use of an array during the operation is fine because you do know the size of that array up front. You often don't, in which case it is usually cleaner to use a List to collect terms, and then convert to an array at the end. I did it this way initially out of habit. In practice, at least in my case, I pretty much never use arrays due to them being less flexible. They ARE more compact and can perform better, but it is rarely the case that the difference matters.

Related

Leetcode #377 Dynamic Programming Logic Question

I have been struggling to comprehend one of the discussion solutions shown from LC #377. I'm not grasping how result is being incremented. I can see that there is the line:
result += combinations(nums, dp, target - nums[i])
which obviously is incrementing result, but how exactly is result being incremented? All I can see that is result is initialized to 0 then the line is executed if that if condition is satisfied, so wouldn't the line:
dp[target] = result
always be set to 0?. Really struggling comprehending, so would appreciate any help.
Code:
class Solution {
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target + 1];
Arrays.fill(dp, -1);
dp[0] = 1;
return combinations(nums, dp, target);
}
public int combinations(int[] nums, int[] dp, int target) {
if (dp[target] != -1) {
return dp[target];
}
int result = 0;
for (int i = 0; i < nums.length; i++) {
if (target >= nums[i]) {
result += combinations(nums, dp, target - nums[i]);
}
}
dp[target] = result;
return result;
}
}
Focusing reply on your question around result being incremented.
This is the code you mentioned:
int result = 0;
for (int i = 0; i < nums.length; i++) {
if (target >= nums[i]) {
result += combinations(nums, dp, target - nums[i]);
}
}
dp[target] = result;
There is a for loop that increments i from 0 to nums.length-1. For each such i, if this condition is met (target >= nums[i]), then result is updated by each call to combinations(nums, dp, target - nums[i]). You can see from the signature that combinations() returns an int value.
When dp[target] = result is reached, the result has sum of the values returned by each of the calls to combinations().
The following code may help you. It counts the total even numbers between 1 to n. I wrote it to match the flavor of recursion used in your post. Hope it helps.
public class SumDemo {
int checkEven(int n) {
return (n % 2 == 0) ? 1 : 0;
}
int countEven(int n) {
int result = 0;
for (int i = 1; i <=n; i++) {
result += checkEven(i);
}
return result;
}
public static void main(String[] args) {
SumDemo demo = new SumDemo();
System.out.println(demo.countEven(10));
}
}
Result:
5
(Since there are 5 even numbers between 1 to 10 (both inclusive)): 2, 4, 6, 8, 10

I tryed to do stairCase problem with n stairs and 1or2or3 steps at a time But my solution is working fine until 13 after that not, its giving less

public class StairCase {
public static int Solution(int n) {
int sum, count = 0;
//Funtion to calculate stair case and use factorial because a solution can have many ways.
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
sum = i + (j * 2) + (k * 3);
if (sum == n) {
count = count + (fact.fact1(i + j + k) / (fact.fact1(i) * fact.fact1(j) * fact.fact1(k)));
}
}
}
/*logic behind this funtion is that like we have to find no of arrangement abbbccde can we written so we write (8!/(3!*2!))
so after getting like ijk= 1,2,3 I rearranged them in that man ways
(i+j+k)!/i!*j!k! !=factorial/
}
return count;
}
public static void doTestPass() {
boolean result = true;
result = result && (Solution(3) == 4);
result = result && (Solution(4) == 7);
result = result && (Solution(11) == 504);
result = result && (Solution(12) == 927);
result = result && (Solution(13) == 1705);
//working fine till 13 not after that
System.out.println(Solution(14));
System.out.println(Solution(20));
System.out.println(Solution(21));
//14--3127 20--68603 21--94351(orignal ans-- 3136,121415,223317)
if (result) {
System.out.println("All Test Cases Passed");
} else {
System.out.println("Test Case Failed");
}
}
public static void main(String[] Args) {
doTestPass();
}
public class fact
{
static int fact1(int n)
{
//this funtion is to create factorial
int res = 1;
for (int i = 2; i <= n; i++)
{
res = res * i;
}
return res;
}
}
}
The problem has to do with using primitive integers for your factorial method and overflowing the integer limit of 2,147,483,647.
Take a look at trial runs of your factorial code below:
fact1(12) outputs 479001600 which is correct for 12!
fact1(13) outputs 1932053504 which is not 13! which should be 6227020800
This means when you execute fact.fact1(i) in your code, it will be outputting the wrong answer for any value greater than 12. This would require you to rework the data structure you use to hold these big numbers to BigInteger.
The reworked fact method would look like this:
BigInteger fact1(int n) {
// this funtion is to create factorial
BigInteger res = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
res = res.multiply(BigInteger.valueOf(i));
}
return res;
}
With the output of fact1(13) being the correct value of 6227020800 now.
You will need to rework the other aspects of your code now with BigInteger, but you should understand now what your issue was.

Erroneous behaviour of Java 8 Stream.sum()

Today while solving this question on HackerRank I used Array stream .sum() function to sum all the entries and proceeded with my algorithm. But for sum reason I found that my algorithm fails for some cases. I used diff to find out it passes 99% cases and for 1% the output is nearly equal but is less than the original answer. That's why I replaced the stream .sum() with a for loop and unexpectedly it passed all the test cases. I tried but couldn't ascertain this uncertain behaviour.
My implementation using stream.sum() :
public class MandragoraForest {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
for (int i = in.nextInt(); i > 0; i--) {
int number = in.nextInt();
int[] h = new int[number];
for (int j = 0; j < number; j++) h[j] = in.nextInt();
System.out.println(new MandragoraForestSolver().solve(h));
}
}
}
class MandragoraForestSolver {
public long solve(int[] h) {
if (h.length==1) return h[0];
Arrays.parallelSort(h);
long sum = Arrays.stream(h)
.sum();
long ans = -1;
for (long i=0, strength = 2; i<h.length; i++, strength++) {
sum -= h[(int)i];
ans = Math.max(ans, strength * sum);
}
return ans;
}
}
Implementation without Java stream :
public class MandragoraForest {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
for (int i = in.nextInt(); i > 0; i--) {
int number = in.nextInt();
int[] h = new int[number];
long sum = 0;
for (int j = 0; j < number; j++) {
h[j] = in.nextInt();
sum += h[j];
}
System.out.println(new MandragoraForestSolver().solve(h, sum));
}
}
}
class MandragoraForestSolver {
public long solve(int[] h, long sum) {
if (h.length==1) return h[0];
Arrays.parallelSort(h);
long ans = -1;
for (long i=0, strength = 2; i<h.length; i++, strength++) {
sum -= h[(int)i];
ans = Math.max(ans, strength * sum);
}
return ans;
}
}
Is there something that I'am missing out ? What could be the reason for this behaviour?
There is one significant difference between using a stream and a loop - the possibility of arithmetic overflow.
Arrays.stream(int[]) returns an IntStream, whose sum() method returns an int result. If the sum exceeds Integer.MAX_VALUE, a silent integer overflow will occur.
However your loop sums by adding int values to a long total, which would not suffer from arithmetic overflow.
The sum of integers in one of the tests must exceed Integer.MAX_VALUE, testing that a long is used to (correctly) calculate the total.
If you want to use a stream to sum, you need to convert the IntStream to a LongStream, which you can do like this:
long sum = Arrays.stream(big).asLongStream().sum();

How to calculate the alternating sum of two arrays in Java?

I have to write a function "alternatingSum(a)" that takes an array of numbers and returns the alternating sum (where the sign alternates from positive to negative or vice versa).
For example:
int[] a = { 5, 3, 8, 4 };
Assert(alternatingSum(a) == 6); // because 5-3+8-4 == 6
So far I have tried to take the array and check to see if the index is odd (not including i=0) then subtract it from the i+1, and if it is even do the same thing except add the two. Am I on the right track with this?
Here is my code:
class foo {
public static int alternatingSum(int[] a){
int sum=0;
for (int i=0;i<a.length;i++){
if (i==0){
sum+=a[0]-a[1];
}
if (i%2 == 0){
sum+=(a[i]+a[i+1]);
}
if (i%2 == 1){
sum+=(a[i]-a[i+1]);
}
}
return sum;
}
public static void testAlternatingSum(){
System.out.println("testing code");
int[] a = { 5, 3, 8, 4 };
assert (alternatingSum(a) == 6); // because 5-3+8-4 == 6
}
public static void main(String[] args){
testAlternatingSum();
}
}
a for loop
I would just keep a boolean flag for even (and toggle it with every loop iteration). If it's an even number, than we're performing addition. But if it's not an even number (it's odd) then we can perform an unary negative operation. That might look something like,
int sum = 0;
boolean even = true;
for (int i = 0; i < a.length; i++) {
sum += even ? a[i] : -a[i];
even = !even
}
return sum;
for-each loop
and you could also use a for-each loop and write it like
boolean even = true;
int sum = 0;
for (int value : a) {
sum += even ? value : -value;
even = !even;
}
return sum;
Easy to read..
int[] alternatingSums(int[] a) {
int sum1 = 0;
int sum2 = 0;
for(int i =0; i < a.length; i++){
if((i % 2) != 1){
sum1 += a[i];
}else{
sum2 += a[i];
}
}
int[] result = {sum1 , sum2};
return result;
}

Cycle through an int array and the use of modulo within it

I need to implement a method that returns the alternating sum of all elements with odd indexes minus the sum of all elements with even indexes. The total sum returned should be -1. 1 - 4 + 9 - 16 + 9 = -1.
Here is my code:
public class Arrays
{
public static void main(String[] args){
int [] data = {1 ,4, 9, 16, 9};
oddAndEven(data);
}
public static int[] oddAndEven(int[] data){
int sum = 0;
int sumA = 0;
int index = data.length;
for(int i:data){
if(index % sumA == 1){
sum = sum-i;
}
else{
sum = sum+i;
}
}
System.out.println(sum);
return sum;
}
}
Can someone tell me where I am going wrong please?
This is a class session, so forgive my basic code and errors.
This is how I would do it:
public class test {
public static void main(String[] args) {
int [] data = {1 ,4, 9, 16, 9};
oddAndEven(data);
}
public static void oddAndEven(int[] data) {
int total = 0;
for (int i = 0; i < data.length; i++)
{
if (i%2==0)
total = total + data[i];
else
total = total - data[i];
}
System.out.println(total);
}
I've gotten rid of the return in the method and changed it to void (as you are printing out the result within it, so there is no need to return it.
You don't need the two different sum values, or the length of the array stored.
The total value is used and set to 0. The for loop then goes through the length of the array. The %2 divides the number by 2 and determines the remainder. So for the first loop, it will calculate 0/2 and work out the remainder (obviously 0). As it ==0, the first if statement in the for loop is executed (adding the numbers).
The second time through, it calculates 1/2, which is 0 with 1 remaining - so the else statement is executed and so on.
Additionally, note how I've gotten rid of the braces around the if and else statements. As long as these statements are a single line, the braces aren't needed - taking the out tends to make the program easier to read (in my opinion). Obviously, if more than one line were needed under them, the braces need to be readded.
What about this ?
public class ArrayMeNow {
public static void main(String[] args) {
int [] data = {1 ,4, 9, 16, 9};
int result = oddAndEven(data);
System.out.println(result);
}
private static int oddAndEven(int[] data) {
int multiplier = 1;
int result = 0;
for(int v:data){
result += v * multiplier;
multiplier *= -1;
}
return result;
}
}
public static int oddAndEven(int[] data) {
int sum = 0;
for (int i=0;i<data.length;i++) {
if (i % 2 == 1) {
sum = sum - data[i];
} else {
sum = sum + data[i];
}
}
System.out.println(sum);
return sum;
}
for(int i:data) doesn't change the value of index. And sumA is supposed to be 2.
Change your for-loop to something like:
for (int i = 0; i < data.length; i++)
if (i % 2 == 1)
sum -= data[i];
else
sum += data[i];
You have to return sum which is of type int NOT int[]. Here is another way to do it.
public static int doAlternateAddSubOn(int[] array) {
int sum = 0;
for(int i=0; i<array.length; i++) {
// When index 'i' is Even, the position is Odd
sum = (i%2==0) ? sum+array[i] : sum-array[i];
}
return sum;
}

Categories

Resources