Problem Name: Missing Number in an Array
Day 3: Missing Number in an Array

Problem Name: Missing Number in an Array

This Problem is on GFG Sheet

Problem Link


Problem Statement :

Given an array of size n-1 such that it only contains distinct integers in the range of 1 to n. Return the missing element.

Example 1

Input: n = 5, arr[] = {1,2,3,5}
Output: 4
Explanation : All the numbers from 1 to 5 are present except 4.

Example 2

Input: n = 2, arr[] = {1}
Output: 2
Explanation : All the numbers from 1 to 2 are present except 2.

Approach

The function missingNumber takes an array of integers array and the size of the intended sequence n as input. It returns the missing number in the sequence of consecutive integers from 1 to n.

The code works by first calculating the expected sum of all consecutive integers from 1 to n. This is done using the formula (n * (n + 1)) // 2.

Next, the code iterates through the given array and accumulates the sum of the elements.

Finally, the code subtracts the actual sum from the expected sum to find the missing number. This is because the difference between the expected sum and the actual sum represents the sum of the missing elements in the sequence.


Java Code

class Solution {
    int missingNumber(int array[], int n) {
        // Your Code Here
        int tot=(n*(n+1))/2;
        int sum=0;
        for(int i=0;i<n-1;i++){
            sum+=array[i];
        }
        int res=tot-sum;
        return res;
    }
}        


要查看或添加评论,请登录

Dhruva Bhat S N的更多文章

  • Problem Title : Left Rotate Matrix K times

    Problem Title : Left Rotate Matrix K times

    Problem Statement: You are given an integer k and matrix mat. Rotate the elements of the given matrix to the left k…

  • Problem Title : Summed Matrix

    Problem Title : Summed Matrix

    Problem Statement: A matrix is constructed of size and given an integer ‘’. The value at every cell of the matrix is…

  • Problem Title: K Sized Subarray Maximum

    Problem Title: K Sized Subarray Maximum

    Problem Statement: Given an array arr[] of size N and an integer K. Find the maximum for each and every contiguous…

  • Problem title: Minimum Platforms

    Problem title: Minimum Platforms

    Problem Statement: Given arrival and departure times of all trains that reach a railway station. Find the minimum…

  • Problem Title: Count Inversions

    Problem Title: Count Inversions

    Problem Description Given an array of integers. Find the Inversion Count in the array.

  • Problem Title: Minimize the Heights II

    Problem Title: Minimize the Heights II

    Problem Statement Given an array denoting heights of towers and a positive integer . For each tower, you must perform…

  • Problem Title: Kth Smallest

    Problem Title: Kth Smallest

    Problem Statement Given an array arr[] and an integer k where k is smaller than the size of the array, the task is to…

  • Problem Title : Sort 0s, 1s and 2s

    Problem Title : Sort 0s, 1s and 2s

    Problem Description: Given an array A of size N containing 0s, 1s, and 2s; you need to sort the array in ascending…

  • Problem Title: Minimum number of jumps

    Problem Title: Minimum number of jumps

    Problem Statement : Given an array arr[] of size n of non-negative integers. Each array element represents the maximum…

  • Problem Title : Find all pairs with a given sum

    Problem Title : Find all pairs with a given sum

    Problem Statement Given two unsorted arrays A of size N and B of size M of distinct elements, the task is to find all…

社区洞察

其他会员也浏览了