We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
// missing number class MissingMultiple {
class Main{ public static void main(String args[]) {
public static void main(String args[]){ int ar[] = {2, 4, 8, 10}; // Example array with multiples int ar[]={1,2,3,5,6,7}; of 2 from 2 to 10, but 6 is missing int n =ar.length +1; int n = ar.length + 1; // n is the length of the array plus int excepted_sum = n*(n+1)/2; the missing number int actual_sum =0; int expectedSum = 2 * n * (n + 1) / 2; for(int i=0;i<ar.length;i++){ int actualSum = 0; actual_sum+=ar[i]; for (int i = 0; i < ar.length; i++) { } actualSum += ar[i]; System.out.println(excepted_sum-actual_sum); } } System.out.println("Missing number: " + } (expectedSum - actualSum)); } } class FindMissingNumberwith_range { public static void main(String args[]) { int start = 10; int end = 20; int[] numbers = {10, 11, 12, 13, 14, 16, 17, 18, 19, 20}; // Array with 15 missing int expectedSum = sumRange(start, end); int actualSum = 0;
// Method to calculate the sum of numbers from start to
end (inclusive) public static int sumRange(int start, int end) { int n = end - start + 1; return n * (start + end) / 2;// arithemetic series formula } }