Skip to content

Commit 64adbee

Browse files
committed
add GFG codes of arrays
1 parent 534fe6a commit 64adbee

11 files changed

+356
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
//code
6+
int t;
7+
cin>>t;
8+
9+
while(t--)
10+
{
11+
int n;
12+
cin>>n;
13+
14+
int arr[n];
15+
for(int i=0; i<n; i++)
16+
{
17+
cin>>arr[i];
18+
}
19+
int x;
20+
cin>>x;
21+
int answer = 0;
22+
for(int i=0; i<n; i++)
23+
{
24+
if(arr[i]<=x)
25+
{
26+
answer++;
27+
}
28+
29+
}
30+
cout<<answer<<endl;
31+
}
32+
return 0;
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
//code
6+
int t;
7+
cin>>t;
8+
9+
while(t--)
10+
{
11+
int n;
12+
cin>>n;
13+
14+
string a[n];
15+
16+
for(int i=0; i<n; i++)
17+
{
18+
19+
cin>>a[i];
20+
21+
22+
23+
}
24+
string max= " ";
25+
int min=0;
26+
for(int i=0; i<n; i++)
27+
{
28+
if(a[i].length()>min)
29+
{
30+
min = a[i].length();
31+
max = a[i];
32+
}
33+
34+
}
35+
cout<<max<<endl;
36+
}
37+
return 0;
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
//code
6+
int t;
7+
cin>>t;
8+
9+
while (t--)
10+
{
11+
int n;
12+
cin>>n;
13+
int arr[n], product=1;
14+
for(int i=0; i<n; i++)
15+
{
16+
cin>>arr[i];
17+
product *= arr[i];
18+
19+
}
20+
cout<<product<<endl;
21+
}
22+
return 0;
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
//code
6+
int t;
7+
cin>>t;
8+
9+
while(t--)
10+
{
11+
int n;
12+
cin>>n;
13+
14+
int arr[n];
15+
for(int i=0; i<n; i++)
16+
{
17+
cin>>arr[i];
18+
}
19+
sort(arr,arr+n);
20+
if(n%2==0){
21+
cout<<arr[(n/2)-1]<<endl;
22+
}
23+
else{
24+
cout<<arr[n/2]<<endl;
25+
}
26+
}
27+
return 0;
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main() {
5+
//code
6+
int t;
7+
cin>>t;
8+
9+
while(t--)
10+
{
11+
int n;
12+
cin>>n;
13+
int arr[n];
14+
15+
for(int i=0; i<n; i++)
16+
{
17+
cin>>arr[i];
18+
19+
}
20+
for(int i=n-1; i>=0; i--)
21+
{
22+
23+
cout<<arr[i]<<" ";
24+
}
25+
cout<<endl;
26+
27+
}
28+
return 0;
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
//code
6+
int t;
7+
cin>>t;
8+
9+
while(t--)
10+
{
11+
int n;
12+
cin>>n;
13+
14+
int arr[n], sum=0;
15+
for(int i=0; i<n; i++)
16+
{
17+
cin>>arr[i];
18+
sum += arr[i];
19+
20+
}
21+
cout<<sum<<endl;
22+
}
23+
return 0;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
int t;
6+
cin>>t;
7+
8+
9+
while(t--)
10+
{
11+
int n;
12+
cin>>n;
13+
int arr[n], sum=0;
14+
for(int i=0; i<n; i++)
15+
{
16+
cin>>arr[i];
17+
sum += arr[i];
18+
}
19+
cout<<sum<<endl;
20+
21+
22+
}
23+
return 0;
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// { Driver Code Starts
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
// } Driver Code Ends
7+
8+
9+
//User function template for C++
10+
class Solution{
11+
public:
12+
// function to return sum of 1, 2, ... n
13+
long long seriesSum(int n) {
14+
// code here
15+
return ((long long)n*(long long)(n+1))/2;
16+
}
17+
};
18+
19+
// { Driver Code Starts.
20+
21+
int main() {
22+
int t;
23+
cin >> t;
24+
while (t--) {
25+
int n;
26+
cin >> n;
27+
Solution ob;
28+
auto ans = ob.seriesSum(n);
29+
cout << ans << "\n";
30+
}
31+
return 0;
32+
} // } Driver Code Ends
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// { Driver Code Starts
2+
//Initial template for C++
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
// } Driver Code Ends
8+
9+
10+
//User function template for C++
11+
12+
class Solution{
13+
public:
14+
bool arraySortedOrNot(int arr[], int n) {
15+
// code here
16+
if (n==0||n==1)
17+
{
18+
return true;
19+
}
20+
for(int i=1; i<n; i++)
21+
{
22+
if(arr[i-1]>arr[i])
23+
{
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
};
30+
31+
// { Driver Code Starts.
32+
33+
int main() {
34+
int t;
35+
cin >> t;
36+
while (t--) {
37+
int n;
38+
cin >> n;
39+
int arr[n];
40+
for (int i = 0; i < n; i++) {
41+
cin >> arr[i];
42+
}
43+
Solution ob;
44+
bool ans = ob.arraySortedOrNot(arr, n);
45+
cout << ans << "\n";
46+
47+
}
48+
return 0;
49+
}
50+
51+
// } Driver Code Ends
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
//code
6+
int t;
7+
cin>>t;
8+
9+
while(t--)
10+
{
11+
int n;
12+
cin>>n;
13+
14+
int arr[n];
15+
for(int i=0; i<n; i++)
16+
{
17+
cin>>arr[i];
18+
19+
}
20+
int count=0;
21+
for(int i=0; i<n; i++)
22+
{
23+
if (count<arr[i])
24+
{
25+
count = arr[i];
26+
27+
}
28+
29+
}
30+
cout<<count<<endl;
31+
32+
}
33+
return 0;
34+
}

0 commit comments

Comments
 (0)