From 162d8d2483a076efe111c7b3bba4eafcd316d95c Mon Sep 17 00:00:00 2001 From: Ishita Mukherjee Date: Sat, 20 Jul 2024 15:51:36 +0530 Subject: [PATCH] Second Largest Element in gfg is added --- .../Easy problems/Second Largest.md | 218 ++++++++++++++++++ .../2057-Smallest-Index-With-Equal-Value.md | 163 ------------- 2 files changed, 218 insertions(+), 163 deletions(-) create mode 100644 dsa-solutions/gfg-solutions/Easy problems/Second Largest.md delete mode 100644 dsa-solutions/lc-solutions/2000-2099/2057-Smallest-Index-With-Equal-Value.md diff --git a/dsa-solutions/gfg-solutions/Easy problems/Second Largest.md b/dsa-solutions/gfg-solutions/Easy problems/Second Largest.md new file mode 100644 index 000000000..8bf6d3877 --- /dev/null +++ b/dsa-solutions/gfg-solutions/Easy problems/Second Largest.md @@ -0,0 +1,218 @@ +--- +id: second-largest-element +title: Second Largest Element +sidebar_label: Second Largest Element +tags: + - Easy + - Array + - DSA +description: "This tutorial covers the solution to the Second Largest Distinct Element problem from the GeeksforGeeks." +--- + +## Problem Description + +Given an array `arr` of size `n`, print the second largest distinct element from the array. If the second largest element doesn't exist, return `-1`. + +## Examples + +**Example 1:** +``` +Input: arr = [10, 5, 10] +Output: 5 +``` + +**Example 2:** +``` +Input: arr = [10, 10, 10] +Output: -1 +``` + +## Your Task + +You don't need to read input or print anything. Your task is to complete the function `print2largest()` which takes the array `arr` and its size `n` as input parameters and returns the second largest distinct element from the array. If no such element exists, return `-1`. + +Expected Time Complexity: $O(N)$, where N is the number of elements in the array. + +Expected Auxiliary Space: $O(1)$ + +## Constraints + +* `1 ≤ n ≤ 10^5` +* `1 ≤ arr[i] ≤ 10^5` + +## Problem Explanation + +To solve this problem, you need to find the second largest distinct element in the array. This can be achieved by keeping track of the largest and the second largest distinct elements while iterating through the array. + +## Code Implementation + + + + + + ```python + class Solution: + def print2largest(self, arr, n): + if n < 2: + return -1 + + first = second = -1 + for num in arr: + if num > first: + second = first + first = num + elif num > second and num != first: + second = num + + return second + + # Example usage + if __name__ == "__main__": + solution = Solution() + arr = [10, 5, 10] + print(solution.print2largest(arr, len(arr))) # Expected output: 5 + ``` + + + + + + ```cpp + //{ Driver Code Starts + #include + using namespace std; + + // } Driver Code Ends + class Solution { + public: + // Function returns the second largest element + int print2largest(int arr[], int n) { + int first = -1, second = -1; + for (int i = 0; i < n; i++) { + if (arr[i] > first) { + second = first; + first = arr[i]; + } else if (arr[i] > second && arr[i] != first) { + second = arr[i]; + } + } + return second; + } + }; + + //{ Driver Code Starts. + + int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + Solution ob; + auto ans = ob.print2largest(arr, n); + cout << ans << "\n"; + } + return 0; + } + + // } Driver Code Ends + ``` + + + + + + + ```javascript + class Solution { + print2largest(arr, n) { + if (n < 2) return -1; + let first = second = -1; + for (let num of arr) { + if (num > first) { + second = first; + first = num; + } else if (num > second && num != first) { + second = num; + } + } + return second; + } +} + + ``` + + + + + + + ```typescript + class Solution { + print2largest(arr: number[], n: number): number { + if (n < 2) return -1; + let first: number = second: number = -1; + for (let num of arr) { + if (num > first) { + second = first; + first = num; + } else if (num > second && num != first) { + second = num; + } + } + return second; + } +} + + ``` + + + + + + + ```java + public class Solution { + public int print2largest(int[] arr, int n) { + if (n < 2) return -1; + int first = Integer.MIN_VALUE; + int second = Integer.MIN_VALUE; + for (int num : arr) { + if (num > first) { + second = first; + first = num; + } else if (num > second && num != first) { + second = num; + } + } + return second; + } +} +public class Main { + public static void main(String[] args) { + Solution solution = new Solution(); + int[] arr = {10, 5, 10}; + System.out.println(solution.print2largest(arr, arr.length)); // Expected output: 5 + } +} + + ``` + + + + + +## Solution Logic: + +1. Iterate through the array and keep track of the largest and the second largest distinct elements. +2. Return the second largest distinct element or `-1` if it doesn't exist. + +## Time Complexity + +* The function visits each element once, so the time complexity is $O(n)$, where n is the length of the array. This is because we are iterating through the array once. + +* The space complexity of the solution is O(1), which means the space required does not change with the size of the input array. This is because we are using a fixed amount of space to store the variables first, second, and num. diff --git a/dsa-solutions/lc-solutions/2000-2099/2057-Smallest-Index-With-Equal-Value.md b/dsa-solutions/lc-solutions/2000-2099/2057-Smallest-Index-With-Equal-Value.md deleted file mode 100644 index c00ad7942..000000000 --- a/dsa-solutions/lc-solutions/2000-2099/2057-Smallest-Index-With-Equal-Value.md +++ /dev/null @@ -1,163 +0,0 @@ ---- -id: smallest-index-with-equal-value -title: Smallest Index With Equal Value -sidebar_label: 2057-Smallest-Index-With-Equal-Value -tags: - - Array - -description: The problem no. is 2057. The Problem is to find Smallest Index With Equal Value. ---- - -## Problem Description -Given a 0-indexed integer array `nums`, return the smallest index `i` of `nums` such that `i mod 10 == nums[i]`, or `-1` if such index does not exist. - -`x mod y` denotes the remainder when `x` is divided by `y`. - - -### Example - -**Example 1:** - - -``` -Input: nums = [0,1,2] -Output: 0 -Explanation: -i=0: 0 mod 10 = 0 == nums[0]. -i=1: 1 mod 10 = 1 == nums[1]. -i=2: 2 mod 10 = 2 == nums[2]. -All indices have i mod 10 == nums[i], so we return the smallest index 0. -``` -**Example 2:** -``` -Input: nums = [4,3,2,1] -Output: 2 -Explanation: -i=0: 0 mod 10 = 0 != nums[0]. -i=1: 1 mod 10 = 1 != nums[1]. -i=2: 2 mod 10 = 2 == nums[2]. -i=3: 3 mod 10 = 3 != nums[3]. -2 is the only index which has i mod 10 == nums[i]. -``` -### Constraints - -- `1 <= nums.length <= 100` - -## Solution Approach - -### Intuition: - -To efficiently determine the Smallest Index With Equal Value -## Solution Implementation - -### Code In Different Languages: - - - - - ```javascript - -class Solution { - smallestEqual(nums) { - const n = nums.length; - let mini = Infinity; - for(let i = 0; i < n; i++){ - if(i % 10 === nums[i]) mini = Math.min(mini, i); - } - if(mini === Infinity) return -1; - return mini; - } -} - - - - ``` - - - - - ```typescript - class Solution { - smallestEqual(nums: number[]): number { - const n = nums.length; - let mini = Infinity; - for(let i = 0; i < n; i++){ - if(i % 10 === nums[i]) mini = Math.min(mini, i); - } - if(mini === Infinity) return -1; - return mini; - } -} - - - - - ``` - - - - - ```python - class Solution: - def smallestEqual(self, nums): - n = len(nums) - mini = float('inf') - for i in range(n): - if i % 10 == nums[i]: - mini = min(mini, i) - if mini == float('inf'): - return -1 - return mini - - - - ``` - - - - - ```java - public class Solution { - public int smallestEqual(int[] nums) { - int n = nums.length; - int mini = Integer.MAX_VALUE; - for(int i = 0; i < n; i++){ - if(i % 10 == nums[i]) mini = Math.min(mini, i); - } - if(mini == Integer.MAX_VALUE) return -1; - return mini; - } -} - - - - - ``` - - - - - ```cpp -class Solution { -public: - int smallestEqual(vector& nums) { - int n = nums.size(); - int mini = INT_MAX; - for(int i=0; i - - -#### Complexity Analysis - -- Time Complexity: $$O(n)$$ -- Space Complexity: $$O(1)$$ -- The time complexity is $$O(n)$$ where n is the length of the input array nums. This is because the algorithm iterates through the array once, performing a constant amount of work for each element. -- The space complexity is $$O(1)$$ because we are not using any extra space. \ No newline at end of file