Knowee
Questions
Features
Study Tools

Write a function:class Solution { public int solution(int[] A); }that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.Given A = [1, 2, 3], the function should return 4.Given A = [−1, −3], the function should return 1.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..100,000];each element of array A is an integer within the range [−1,000,000..1,000,000].

Question

Write a function:class Solution { public int solution(int[] A); }that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.Given A = [1, 2, 3], the function should return 4.Given A = [−1, −3], the function should return 1.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..100,000];each element of array A is an integer within the range [−1,000,000..1,000,000].

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Java solution for the problem:

import java.util.Arrays;

class Solution {
    public int solution(int[] A) {
        Arrays.sort(A);
        int smallestPositiveInt = 1;
        
        for (int i = 0; i < A.length; i++) {
            if (A[i] == smallestPositiveInt) {
                smallestPositiveInt++;
            }
        }
        
        return smallestPositiveInt;
    }
}

This function works by first sorting the array. Then it iterates through the array, and if the current element is equal to the smallest positive integer we're looking for, it increments the smallest positive integer. This way, it will always find the smallest positive integer that does not occur in the array.

This problem has been solved

Similar Questions

You are given a sorted array of integers. Write a program that implements a binary search algorithm to find the element with the minimum difference from the given target.Note: This question was asked in CTS coding test.Input format :The first line input consists of an integer N, representing the number of array elements.The second line consists of N space-separated integers, representing the sorted array elements.The third line consists of an integer representing the target element.Output format :The output prints an integer representing the element with the minimum difference from the given target.

You are given two integers n and x. You have to construct an array of positive integers nums of size n where for every 0 <= i < n - 1, nums[i + 1] is greater than nums[i], and the result of the bitwise AND operation between all elements of nums is x.Return the minimum possible value of nums[n - 1]. Example 1:Input: n = 3, x = 4Output: 6Explanation:nums can be [4,5,6] and its last element is 6.Example 2:Input: n = 2, x = 7Output: 15Explanation:nums can be [7,15] and its last element is 15. Constraints:1 <= n, x <= 108

class Solution { public: int minOperations(vector<int>& nums, int k) { } }; complete the function using folloiwing statement User 100231. Minimum Operations to Exceed Threshold Value I User Accepted:1 User Tried:3 Total Accepted:1 Total Submissions:3 Difficulty:Easy You are given a 0-indexed integer array nums, and an integer k. In one operation, you can remove one occurrence of the smallest element of nums. Return the minimum number of operations needed so that all elements of the array are greater than or equal to k. Example 1: Input: nums = [2,11,10,1,3], k = 10 Output: 3 Explanation: After one operation, nums becomes equal to [2, 11, 10, 3]. After two operations, nums becomes equal to [11, 10, 3]. After three operations, nums becomes equal to [11, 10]. At this stage, all the elements of nums are greater than or equal to 10 so we can stop. It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10. Example 2: Input: nums = [1,1,2,4,9], k = 1 Output: 0 Explanation: All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums. Example 3: Input: nums = [1,1,2,4,9], k = 9 Output: 4 Explanation: only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums. Constraints: 1 <= nums.length <= 50 1 <= nums[i] <= 109 1 <= k <= 109 The input is generated such that there is at least one index i such that nums[i] >= k.

You are given an array a, of n elements. Find the minimum index based distance between two distinct elements of the array, x and y. Return -1, if either x or y does not exist in the array.

You are playing a game with your friend Ajay where Ajay gave you an array 'A' consisting of 'N' integers such that all the elements of the array does not exceed the value 'K'.Ajay has asked you to replace the minimum number of elements (replacement is the following operation: choose some index i from 1 to n and replace a[i] with some integer in range [1;k] ) to satisfy the following conditions:after all replacements, all a[i] are positive integers not greater than k;for all i from 1 to n/2 the following equation is true: a[i]+a[n]−i+1=x, where x should be the same for all n/2 pairs of elements.Note: It is guaranteed that 'N' is even.Input FormatThe first line contains two integers n and k — the length of a and the maximum possible value of some a[i] correspondingly. It is guratanteed that n is even .The second line of the test case contains n integers a1,a2,…,an, where ai is the i-th element of a.Constraints2≤n≤2⋅10^51≤k≤2⋅10^51≤a[i]≤kOutput FormatPrint the integer — the minimum number of elements you have to replace in a to satisfy the conditions from the problem statement.Sample Input 04 41 1 3 1Sample Output 01Sample Input 16 71 1 1 1 2 2Sample Output 11

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.