Meaning of the formula how to find lost element in array? - java

The task is to find lost element in the array. I understand the logic of the solution but I don't understand how does this formula works?
Here is the solution
int[] array = new int[]{4,1,2,3,5,8,6};
int size = array.length;
int result = (size + 1) * (size + 2)/2;
for (int i : array){
result -= i;
}
But why we add 1 to total size and multiply it to total size + 2 /2 ?? In all resources, people just use that formula but nobody explains how that formula works

The sum of the digits 1 thru n is equal to ((n)(n+1))/2.
e.g. for 1,2,3,4,5 5*6/2 = 15.
But this is just a quick way to add up the numbers from 1 to n. Here is what is really going on.
The series computes the sum of 1 to n assuming they all were present. But by subtracting each number from that sum, the remainder is the missing number.
The formula for an arithmetic series of integers from k to n where adjacent elements differ by 1 is.
S[k,n] = (n-k+1)(n+k)/2
Example: k = 5, n = 10
S[k,n] = 5 6 7 8 9 10
S[k,n] = 10 9 8 7 6 5
S[k,n] = (10-5+1)*(10+5)/2
2S[k,n] = 6 * 15 / 2
S[k,n] = 90 / 2 = 45
For any single number missing from the sequence, by subtracting the others from the sum of 45, the remainder will be the missing number.

Let's say you currently have n elements in your array. You know that one element is missing, which means that the actual size of your array should be n + 1.
Now, you just need to calculate the sum 1 + 2 + ... + n + (n+1).
A handy formula for computing the sum of all integers from 1 up to k is given by k(k+1)/2.
By just replacing k with n+1, you get the formula (n+1)(n+2)/2.

It's simple mathematics.
Sum of first n natural numbers = n*(n+1)/2.
Number of elements in array = size of array.
So, in this case n = size + 1
So, after finding the sum, we are subtracting all the numbers from array individually and we are left with the missing number.

Broken sequence vs full sequence
But why we add 1 to total size and multiply it to total size + 2 /2 ?
The amount of numbers stored in your array is one less than the maximal number, as the sequence is missing one element.
Check your example:
4, 1, 2, 3, 5, 8, 6
The sequence is supposed to go from 1 to 8, but the amount of elements (size) is 7, not 8. Because the 7 is missing from the sequence.
Another example:
1, 2, 3, 5, 6, 7
This sequence is missing the 4. The full sequence would have a length of 7 but the above array would have a length of 6 only, one less.
You have to account for that and counter it.
Sum formula
Knowing that, the sum of all natural numbers from 1 up to n, so 1 + 2 + 3 + ... + n can also be directly computed by
n * (n + 1) / 2
See the very first paragraph in Wikipedia#Summation.
But n is supposed to be 8 (length of the full sequence) in your example, not 7 (broken sequence). So you have to add 1 to all the n in the formula, receiving
(n + 1) * (n + 2) / 2

I guess this would be similar to Missing Number of LeetCode (268):
Java
class Solution {
public static int missingNumber(int[] nums) {
int missing = nums.length;
for (int index = 0; index < nums.length; index++)
missing += index - nums[index];
return missing;
}
}
C++ using Bit Manipulation
class Solution {
public:
int missingNumber(vector<int> &nums) {
int missing = nums.size();
int index = 0;
for (int num : nums) {
missing = missing ^ num ^ index;
index++;
}
return missing;
}
};
Python I
class Solution:
def missingNumber(self, nums):
return (len(nums) * (-~len(nums))) // 2 - sum(nums)
Python II
class Solution:
def missingNumber(self, nums):
return (len(nums) * ((-~len(nums))) >> 1) - sum(nums)
Reference to how it works:
The methods have been explained in the following links:
Missing Number Discussion
Missing Number Solution

Related

How to add 'n' amount of odd integers?

I'm trying to write a program that computes the sum of the first n positive odd integers.
I'm having trouble figuring out how to incorporate n into finding the sum. I already have a do/while loop to ensure I get a positive value when assigning n value. I know that I have to use a for loop but I'm not really sure how I would do that.
Scanner input = new Scanner(System.in); // open input stream
String cleanUpStr; // clean kbd buffer
int n; // number
int sum; // sum of numbers
int cntr; // counter for loop
cleanUpStr = "nothing yet";
n = 0;
sum = 0;
cntr = 0;
//prompt user for the value of n
// use a loop to ensure a positive output
do
{
System.out.println("Enter the value of n");
n = input.nextInt();
cleanUpStr = input.nextLine();
// print error if n is invalid
if (n < 0)
{
System.out.println("Invalid n value of " + n + ", try again.");
} // end if
}while(n < 0);
for(cntr = 0; cntr < n; ++cntr)
{
} // end for
} // end main
For example: if n = 5, then this should compute 1 + 3 + 5 + 7 + 9.
The nice thing about this problem, is that you don't need to write a loop! The sum of the first n positive odd integers is the square of n (written throughout this post as n^2). This is how you express the sum in terms of n. So the following will suffice:
// Calculate the sum of first n positive odd integers by using the n^2 formula.
public static int sumOddIntegers(int n) {
return n*n;
}
If you're set on using a loop, you can do the following by observing that the i-th positive odd integer can be calculated using the formula (2i-1):
// Calculate the sum of first n positive odd integers by adding each number iteratively in a loop.
public static int sumOddIntegers(int n) {
int oddSum = 0;
for (int i = 1; i <= n; i++) {
oddSum += (2*i - 1);
}
return oddSum;
}
To visualize this, consider the following examples:
n = 1 List: {1} S(n) = 1 = 1 = n^2
n = 2 List: {1, 3} S(n) = 1 + 3 = 4 = n^2
n = 3 List: {1, 3, 5} S(n) = 1 + 3 + 5 = 9 = n^2
n = 4 List: {1, 3, 5, 7} S(n) = 1 + 3 + 5 + 7 = 16 = n^2
n = 5 List: {1, 3, 5, 7, 9} S(n) = 1 + 3 + 5 + 7 + 9 = 25 = n^2
And so on...
Here is a proof by induction showing that the sum of the first n positive odd numbers is n^2. I'm new to Stack Overflow, so I hope my formatting is legible. If it can be improved, please feel free to suggest edits :) It seems like Stack Overflow doesn't support LaTeX style exponent and subscript formatting, so I did my best.
Proof
P(n): The sum of the first n positive odd integers is n^2.
Base Case
P(1): n = 1
The n = 1 case is trivial. The list of the first n positive odd integers is simply {1}. Therefore the sum of the first n positive odd integers is 1. Since 1 = n = n^2, the predicate P(1) holds true.
Inductive Hypothesis
Assume that P(k) holds for any arbitrary positive integer k > 0.
Inductive Step
Given P(k), we will prove that P(k+1) also holds true. In words, if the sum of the first k positive odd integers is k^2, then the sum of the first (k+1) positive odd integers is (k+1)^2.
As part of this proof, assume the following lemma.
Lemma 1: The nth positive odd integer can be expressed as 2n-1.
If P(k) holds, then the sum of the first k positive odd integers {a_1, ... a_k} is k^2 where the element a_k is expressed as 2k-1 (by Lemma 1). It follows that adding the (k+1)st positive odd integer, a_(k+1) to the list of the first k positive odd integers will produce a list of the first (k+1) positive odd integers as follows: {a_1, ... a_k, a_(k+1)}. Therefore, the sum of this list of the first (k+1) positive odd integers will be equal to the sum of the list of the first k positive odd integers plus the value of a_(k+1), the (k+1)st positive odd integer. By Lemma 1, the (k+1)st positive odd integer is expressed as 2(k+1)-1 = 2k+1.
Let S(k) = the sum of the first k positive odd integers. Therefore, S(k) = k^2. The above statements imply that
S(k+1) = S(k) + a_(k+1), adding the (k+1)st positive odd integer
S(k+1) = S(k) + (2(k+1)-1), by Lemma 1
S(k+1) = S(k) + (2k+1)
S(k+1) = k^2 + (2k+1), by inductive hypothesis
S(k+1) = k^2 + 2k + 1
S(k+1) = (k+1)^2, by factoring
We have therefore shown that if S(k) = k^2, then S(k+1) = (k+1)^2. This shows that P(k) -> P(k+1).
By induction, we have proved that P(n) holds for any positive integer n > 0.
The sum of the first n positive odd integers is therefore n^2. QED.
Proof of Lemma 1:
Here is a proof of this by induction.
P(n): The nth positive odd integer, a_n, can be expressed as 2n-1.
Base case: P(1): 1 is the first positive odd integer (case n = 1).
1 = 2(1)-1 = 1.
Therefore, a_1 = 1 = 2n-1. Correct.
Inductive Hypothesis: Assume P(k) holds.
Inductive Step: If P(k) holds, then P(k+1) holds.
If P(k) holds, then the kth positive odd integer can be expressed as 2k-1. By addition, the next positive odd integer (the (k+1)st positive odd integer), would be (2k-1) + 2.
= (2k-1) + 2
= 2k-1 + 2
= 2k+2 - 1
= 2(k+1) -1
We have shown that P(k) -> P(k+1). Therefore, by induction, P(n) holds for all integers n > 0. QED.
Good luck! Hope this was helpful :)
Stream is good, but if you're a beginner a plain old for loop is your best friend.
public static int sumForOddNumbers(int total) {
int sum = 0;
for(int i = 0, odd = 1; i < total; i++, odd += 2) {
sum += odd;
}
return sum;
}
Java stream API suggests quite clear solution:
IntStream.iterate(1, i -> i + 2)
.limit(n)
.sum();
More about IntStream by link
While streams would be a great way to go about this if you were concerned with functional programming, just learning Java I would suggest the below.
int oddValue = 1;
int answer = 0;
for(cntr = 0; cntr < n; ++cntr)
{
//adds oddvalue to your answer
answer += oddValue;
//adds two to odd value (next odd)
oddValue+=2;
}
There are numerous approaches to this problem.
The way you're thinking about it, yes, you can use a loop. Generally, you have a loop counter and some maximum.
The first observation to make is that odd numbers are written in the form 2k-1. Such as k=3, 2 * 3 - 1 = 5, i.e., 5 is the 3rd odd number.
Given this, you can write a loop for k, as follows:
for (int k = 1; k <= n; k++) {
int oddNumber = 2 * k - 1; // the kth odd number
}
You can then sum these up.
Another way to do it is the way #Ruslan has shown, which uses a lambda expression to encode a similar idea. It walks over a list of integers, starting at 1 and stepping 2 each time: 1, 3, 5, 7, .... This can be done in a loop as well:
for (int oddNumber = 1; oddNumber <= (2*n - 1); oddNumber += 2) {
// calculate a sum here
}
Notice that this 2*n - 1 expression came up again. We're counting odd numbers until we reach the nth one.
There are also ways to do this without loops, such as realizing the following pattern:
1 = 1
1 + 3 = 4
1 + 3 + 5 = 9
1 + 3 + 5 + 7 = 16
1 + 3 + 5 + 7 + 9 = 25
This means that the sum of the first n odd numbers is just n^2. No loops needed. (Proofs for this are available on your favourite math website.)

Why does my code fail the hidden input test cases?

This is the problem to be solved:
John is assigned a new task today. He is given an array A containing N integers. His task is to update all elements of array to some minimum value x , that is, A[i] = x; 1 <= i <= N; such that sum of this new array is strictly greater than the sum of the initial array.
Note that x should be as minimum as possible such that the sum of the new array is greater than the sum of the initial array.
Input Format:
First line of input consists of an integer N denoting the number of elements in the array A.
Second line consists of N space separated integers denoting the array elements.
Output Format:
The only line of output consists of the value of x.
Sample Input:
5
12345
Sample Output:
4
Explanation:
Initial sum of array= 1 + 2 + 3 + 4 + 5 = 15
When we update all elements to 4, sum of array = 4 + 4 + 4 + 4 + 4 = 20 which is greater than 15.
Note that if we had updated the array elements to 3, sum = 15 which is not greater than 15. So, 4 is the minimum value to which array elements need to be updated.
** ==> Here is my code. How can I improve it? or What is the problem in this code? **
import java.util.Scanner;
public class Test2 {
public static void main(String []args){
Scanner s=new Scanner(System.in);
int check=0, sum=0, biggest=0;
int size=s.nextInt();
if(size>=1 && size<=100000) {
int[] arr=new int[size];
for(int i=0; i<size; i++){
int temp=s.nextInt();
if(temp>=1 && temp<=1000) {
arr[i] = temp;
biggest=biggest > temp ? biggest:temp;
sum=sum+temp;
}
else break;
}
for(int i=1; i<biggest; i++){
check=(size*i)>sum ? i:0;
}
System.out.print(check);
}
else System.err.print("Invalid input size");
}
}
Issue:
for(int i=1; i<biggest; i++){
check=(size*i)>sum ? i:0;
}
There are 2 problems with this, hence it doesn't work. They are as follows-
(size*i)>sum ? i - The problem statement states that it needs minimum possible sum greater than sum of array of elements. Your code blindly assigns i to check without checking the minimality.
check=(size*i)>sum ? i:0 - So, even if you had come across some integer previously, you lost it because you assigned it to 0 if the condition is not satisfied.
I will share my idea of how would I go about this.
Approach 1
Sum all elements like you did.
Now, take average of elements - sum / size of the array. Let's say we store it in a variable average.
Print average + 1 as your answer, as that is the value that could give you minimum possible sum > sum of array itself.
Time Complexity: O(n), where n is size of the array.
Space Complexity: O(1)
Approach 2
Sum all elements like you did.
Calculate min and max for the array and store it in variables, say mini and maxi.
Now, do a binary search between mini and maxi and keep checking the minimum sum > sum criteria.
In this process, you will have variables like low, mid and high.
low = mini,high = maxi
while low <= high:
mid = low + (high - low) / 2
If mid * size <= sum,
low = mid + 1
else
high = mid - 1
Now, print low as your answer.
Let range = maxi - mini.
Time Complexity: O(n) + O(log(range)) = O(n) asymptotically, where n is size of the array.
Space Complexity: O(1)
Not sure if I completely followed what your attempt was, but there should be a pretty straightfoward solution. You know the size of the array and you can easily iterate through the array to get the value of the elements stored in it. All you need to do to find your min x is to take sumOfArray/size of array and then add one to the result to make your result higher.
In your example 15/5=3. 3+1 = 4 so that's your answer. If the numbers summed to 43, 43/5 = 8 r 3, so your answer is 9 (9*5=45). Etc.
When trying some other test cases, then the results are wrong. Try:
Input:
5
1 1 1 1 5
Expected Output: 2 Actual Output: 4
and
Input:
5
5 5 5 5 5
Expected Output: 6 Actual Output: 0

Getting median out of frequency table (counting sort)

I can't understand the logic behind getMedian method. How exactly median is evaluated, what is the connection between count of elements and sum of elements? Appreciate if someone could explain it's logic.
public static void main(String[] args) {
Random r = new Random();
int[] ar = r.ints(0, 100).limit(9).toArray();
int k = ar.length;
int[] count = getCounts(ar);
double median = getMedian(count, k);
System.out.println(median);
}
private static int[] getCounts(int[] ar) {
int[] count = new int[100];
for (int i = 0; i < ar.length; i++) {
count[ar[i]]++;
}
return count;
}
private static double getMedian(int[] count, int d) {
int sum = 0;
for (int i = 0; i < count.length; i++) {
sum += count[i];
if (2 * sum < d)
continue;
else if (2 * sum == d)
return (2 * i + 1) / 2.0;
else
return i * 1.0;
}
return -1.0;
}
There is a relation because it is a frequency table. You are thinking it differently but let me give you an example.
1 1 1 3 3 4 4 4 5 5 5 5 if this is the array then the frequency table would be :-
1 3 4 5
- - - -
3 2 3 4
So this is median.
So now I am adding every element count and asking us the question, where does the median lie? or where is that indexm which if I consider I will cover the middle element?
Now here I am checking if sum > d/2 then it's done. We found the median.else if it is less then I still have to traverse other elements to get to the middle of the array. And if it is sum==d/2 then we have found it but we have to send the correct position. And we simply send the one in the lower middle (happens in case like 1,1,1,1).
Walk through
1 1 1 3 3 4 4 4 5 5 5 5
Now I check if I traverse all set of 1's where I am? I covered 3 elements. But it's not the half of the total numbers(6).
Now add number of 3's. 5. This is also not.
Now I add number of 4's, So 8 elements I covered. So I covered more than half of number of elements. So median lies here.
More detailed explanation:
You are asked to find the median of an array of 10 integers.
[1 2 3 4 5 6 7 8 9]
Then median is in element at position floor(9/2)=4, which is 5 Right?
[1 1 2 2 3 3 4 4 5]
Where is the median element at position floor(9/2)=4, which is 3. Right?
So now think this,
1 2 3 4 5
2 2 2 2 1
Now you will try to find the floor(9/2) th element here starting from beginning. And that's why you need to find the sum of the frequencies and all.
Hope you get it?
Correct algorithm
What you need to do is :-
N = number of elements.
F[] = frequency array
so if N is odd
find the element at floor(N/2)-th place and median is that element.
else
find the element at floor((N-1)/2) and floor(N/2) th position and return their average.
Finding the element is simple:
Find( F[], p) // find the element at position p
{
p=p+1
for i in [0..|F|]
cumulative+=F[i]
if cumulative == p
return this element.
else cumulative >p
return this element
}

Adding Two Elements in an Array (Java)

I need to write a method that adds the two last elements of two arrays together and add the sum on another array. But if the two elements are more than 10, then I would need to carry that number to the next array.
This program should be similar to what an odometer does.
Here is my sample code.
int [] sum(int []number1, int []number2)
{
int [] total;
int carry = 0;
for ( int k = numbers1 - 1; k >= 0; k++)
{
sum = number1[k] + number2[k] + carry;
carry = sum/10;
total[k] = sum
}
return total;
}
An example output would be:
0 1 2 3 4
0 8 9 9 9
4 5 7 0 3
5 4 7 0 2
So the top array is just a visual aid that tells where the position for the number is.
The program is suppose to add the next two arrays together. i.e. 9 + 3 = 12
Since 12 is above 9, it carries over the 10 to the next set of arrays, that is why the third array only has a 2 in its place, and that is why the next array is 9 + 0 = 0; because the 10 was carried over.
I am not sure why my code won't work. I don't get the right numbers. Could anyone give any hints or solution to the problem?
-Thanks
I assume numbers1 is the number of elements in the array.
In this case it should be k-- instead of k++ because you are starting from the last element and moving backword.

Sum of numbers under 10,000 that are multiples of 3, 5 or 7 in Java

I know how to get the program to add up the sums of the multiple for each of 3, 5 and 7, but I'm not sure how I'd get the program to only use each number once. For example, I can get the program to find out all of the numbers and add them up for 3 and then do the same for 5, but then the number 15 would be in the final number twice. I'm not sure exactly how I'd get it to only take the number once. Thanks for any help.
While the generate-and-test approach is simple to understand, it is also not very efficient if you want to run this on larger numbers. Instead, we can use the inclusion-exclusion principle.
The idea is to first sum up too many numbers by looking at the multiples of 3, 5 and 7 separately. Then we subtract the ones we counted twice, i.e. multiples of 3*5, 3*7 and 5*7. But now we subtracted too much and need to add back the multiples of 3*5*7 again.
We start by finding the sum of all integers 1..n which are multiples of k. First, we find out how many there are, m = n / k, rounded down thanks to integer division. Now we just need to sum up the sequence k + 2*k + 3*k + ... + m*k. We factor out the k and get k * (1 + 2 + ... + m).
This is a well-known arithmetic series, which we know sums to k * m * (m + 1)/2 (See triangle number).
private long n = 9999;
private long multiples(long k) {
long m = n / k;
return k * m * (m + 1) / 2:
}
Now we just use inclusion-exclusion to get the final sum:
long sum = multiples(3) + multiples(5) + multiples(7)
- multiples(3*5) - multiples(3*7) - multiples(5*7)
+ multiples(3*5*7);
This will scale much better to larger n than just looping over all the values, but beware of overflow and change to BigIntegers if necessary.
The easiest approach would be to use a for loop thus:
int sum = 0;
for(int i=1; i<10000; i++)
{
if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0)
sum += i;
}
Use a Set to store the unique multiples, and then sum the values of the Set.
I would use a Set. This way you are guaranteed that you won't get any duplicates if they are your main problem.
One simple solution would be to add each number thats a multiple of 3,5, or 7 to an Answer list. And then as you work thru each number, make sure that its not already in the answer list.
(pseudo-code)
List<int> AnswerList;
List<int> MultiplesOfFive;
List<int> MultiplesOfSeven;
List<int> MultiplesOfThree;
for (int i = 0 ; i < 10000; i++)
{
if ( i % 3 == 0 && AnswserList.Contains(i) == false)
{
MultiplesOfThree.Add(i);
AnswerList.Add(i);
}
if ( i % 5 == 0 && AnswserList.Contains(i) == false)
{
MultiplesOfFive.Add(i);
AnswerList.Add(i);
}
if ( i % 7 == 0 && AnswserList.Contains(i) == false)
{
MultiplesOfSeven.Add(i);
AnswerList.Add(i);
}
}
for the solution that loops 1 to 1000 use i<=10000 otherwise it'll skip 10000 itself which is a multiple of 5. Apologies, for some reason i can't post this as a comment

Categories