Skip to content

Commit 0ab2793

Browse files
Merge pull request #591 from geeky-auro/main
Some Important Array Problems Added
2 parents 6d5ba9e + 92cc2e5 commit 0ab2793

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

Arrays/32_Leaders_in_Array.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Given an integer array A of size n.
3+
Find and print all the leaders present in the input array.
4+
An array element A[i] is called Leader, if all the elements following it (i.e. present at its right) are less than or equal to A[i].
5+
Print all the leader elements separated by space and in the same order they are present in the input array.
6+
7+
Input Format :
8+
Line 1 : Integer n, size of array
9+
Line 2 : Array A elements (separated by space)
10+
Output Format : leaders of array (separated by space)
11+
12+
Constraints : 1 <= n <= 10^6
13+
Sample Input 1 : 6 3 12 34 2 0 -1
14+
Sample Output 1 : 34 2 0 -1
15+
16+
Sample Input 2 : 5 13 17 5 4 6
17+
Sample Output 2 : 17 6
18+
*/
19+
#include<iostream>
20+
#include<climits>
21+
using namespace std;
22+
void Leaders(int* arr,int len)
23+
{
24+
for(int i=0;i<len;i++){
25+
bool isbig=true; //Initially assume that present element is greater than next element
26+
for(int j=i+1;j<len;j++){
27+
// check for the next element if it is greater change then change flag "isbig" to false
28+
if(!(arr[i]>=arr[j])){
29+
isbig=false;
30+
break;
31+
}
32+
// if they are arranged as they should be as in the case of leaders then change the flag to true
33+
// Or you can leave the flag unchanged as it is ....!
34+
// In the below codes are added for better understanding and Workflow.
35+
else{
36+
isbig=true;
37+
continue;
38+
}
39+
}
40+
if(isbig){
41+
// Print the element for Leaders..!
42+
cout<<arr[i]<<" ";
43+
}
44+
}
45+
}
46+
int main()
47+
{
48+
int len;
49+
cin>>len;
50+
int *arr = new int[len + 1];
51+
52+
for(int i=0;i<len;i++)
53+
{
54+
cin>>arr[i];
55+
}
56+
Leaders(arr,len);
57+
}

Arrays/33_Sort_0_1_2.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
3+
You are given an integer array/list(ARR) of size N.
4+
It contains only 0s, 1s and 2s.
5+
Write a solution to sort this array/list in a 'single scan'.
6+
'Single Scan' refers to iterating over the array/list just once or to put it in other words, you will be visiting each element in the array/list just once.
7+
Note: You need to change in the given array/list itself.
8+
Hence, no need to return or print anything.
9+
10+
Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run.
11+
Then the test cases follow.
12+
13+
First line of each test case or query contains an integer 'N' representing the size of the array/list.
14+
15+
Second line contains 'N' single space separated integers(all 0s, 1s and 2s) representing the elements in the array/list. Output Format : For each test case, print the sorted array/list elements in a row separated by a single space.
16+
17+
Output for every test case will be printed in a separate line.
18+
Constraints :
19+
1 <= t <= 10^2 0 <= N <= 10^5
20+
Time Limit: 1 sec
21+
22+
Sample Input 1: 1 7 0 1 2 0 2 0 1
23+
Sample Output 1: 0 0 0 1 1 2 2
24+
25+
Sample Input 2: 2 5 2 2 0 1 1 7 0 1 2 0 1 2 0
26+
Sample Output 2: 0 1 1 2 2 0 0 0 1 1 2 2
27+
28+
*/
29+
#include<iostream>
30+
using namespace std;
31+
32+
void simplifiedsort012(int *arr, int n){
33+
34+
int i=0,nz=0,nt=n-1;
35+
while(i<n &&i<=nt){
36+
if(arr[i]==0){
37+
int temp=arr[i];
38+
arr[i]=arr[nz];
39+
arr[nz]=temp;
40+
nz++;
41+
}
42+
else if(arr[i]==2){
43+
int temp=arr[i];
44+
arr[i]=arr[nt];
45+
arr[nt]=temp;
46+
nt--;
47+
continue;
48+
49+
}
50+
51+
i++;
52+
}
53+
}
54+
55+
int main(){
56+
57+
int n;
58+
cout<<"Enter the size of the Array "<<endl;
59+
cin>>n;
60+
int *arr=new int[n];
61+
cout<<"Enter Elements in the Array "<<endl;
62+
for(int i=0;i<n;i++){
63+
cin>>arr[i];
64+
}
65+
simplifiedsort012(arr,n);
66+
return 0;
67+
}
68+
69+
/*
70+
Explanation
71+
👉🏼Take two variables as starting(nz) and ending(nt) depicting position of 0's and 2's to be swapped respectively
72+
73+
👉🏼Initialize nz as Starting Index i.e 0 and increment it after every swap
74+
75+
👉🏼Initialize nt as Ending Index i.e Size-1 and decrement it after every swap
76+
77+
Special Attention should be given to the following points :)
78+
✨Swap 0's and increment nz & i
79+
80+
✨Swap 2's and increment nt but not i (ignore the updation of expression)
81+
*/

0 commit comments

Comments
 (0)