Skip to content

Commit 3e130e2

Browse files
Largest Element of array from gfg is added
1 parent 435cf2d commit 3e130e2

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
id: largest-element-in-array
3+
title: Largest Element In Array
4+
sidebar_label: Largest-Element-In-Array
5+
tags:
6+
- Arrays
7+
- Data Structure
8+
description: "This tutorial covers the solution to the Largest Element In Array problem from the GeeksforGeeks website, featuring implementations in C++."
9+
---
10+
## Problem Description
11+
Given an array `arr`, the task is to find the largest element in it.
12+
13+
## Examples
14+
15+
**Example 1:**
16+
17+
```
18+
Input: arr= [1, 8, 7, 56, 90]
19+
Output: 90
20+
Explanation: The largest element of given array is 90.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: arr = [5, 5, 5, 5]
27+
Output: 5
28+
Explanation: The largest element of given array is 5.
29+
```
30+
31+
## Your Task
32+
33+
You don't need to read input anything. Your task is to complete the function `largest()` which takes the array `arr` and an size of array as `n` as input parameters and returns the largest number.
34+
35+
Expected Time Complexity: O(n)
36+
37+
Expected Auxiliary Space: O(1)
38+
39+
## Constraints
40+
41+
* `1 <= arr.size()<= 10^5`
42+
43+
## Problem Explanation
44+
45+
The task is to traverse the whole array and find the largest element of that array.
46+
47+
## Code Implementation
48+
49+
### C++ Solution
50+
51+
52+
```cpp
53+
class Solution
54+
{
55+
public:
56+
int largest(vector<int> &arr, int n)
57+
{
58+
int maxi = INT_MIN;
59+
for(int i=0; i<n; i++){
60+
maxi = max(arr[i], maxi);
61+
}
62+
return maxi;
63+
}
64+
};
65+
```
66+
67+
```java
68+
public class Solution {
69+
public int largest(int[] arr) {
70+
int maxi = Integer.MIN_VALUE;
71+
for (int i = 0; i < arr.length; i++) {
72+
maxi = Math.max(arr[i], maxi);
73+
}
74+
return maxi;
75+
}
76+
}
77+
78+
```
79+
80+
```python
81+
class Solution:
82+
def largest(self, arr):
83+
maxi = float('-inf')
84+
for i in range(len(arr)):
85+
maxi = max(arr[i], maxi)
86+
return maxi
87+
88+
```
89+
90+
```javascript
91+
class Solution {
92+
largest(arr) {
93+
let maxi = -Infinity;
94+
for (let i = 0; i < arr.length; i++) {
95+
maxi = Math.max(arr[i], maxi);
96+
}
97+
return maxi;
98+
}
99+
}
100+
101+
```
102+
103+
```typescript
104+
class Solution {
105+
largest(arr: number[]) {
106+
let maxi = -Infinity;
107+
for (let i = 0; i < arr.length; i++) {
108+
maxi = Math.max(arr[i], maxi);
109+
}
110+
return maxi;
111+
}
112+
}
113+
114+
``
115+
116+
## Time Complexity
117+
118+
* The time complexity is $$O(n)$$ where n is the length of the input array. This is because we are iterating through the array once to find the maximum element.
119+
120+
## Space Complexity
121+
122+
* The auxiliary space complexity is $O(1)$ which means the space required does not change with the size of the input array. This is because we are only using a fixed amount of space to store the maximum element and the index.

0 commit comments

Comments
 (0)