From 601ecae3647523a16505e342d59590cc2c567aeb Mon Sep 17 00:00:00 2001 From: ShivamDubey7 Date: Sat, 17 Oct 2020 15:19:26 +0530 Subject: [PATCH 01/21] Added efficient MergeSort --- Sorting/MergeSort.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Sorting/MergeSort.cpp diff --git a/Sorting/MergeSort.cpp b/Sorting/MergeSort.cpp new file mode 100644 index 00000000..188a36ad --- /dev/null +++ b/Sorting/MergeSort.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +void merge(vector&v, int startIndex, int mid, int endIndex){ + vectortemp; + int i1 = startIndex; + int i2 = mid+1; + while(i1<=mid && i2<=endIndex){ + if(v[i1]<=v[i2]) + temp.push_back(v[i1]),i1++; + else + temp.push_back(v[i2]),i2++; + } + while(i1<=mid)temp.push_back(v[i1]),i1++; + while(i2<=endIndex)temp.push_back(v[i2]),i2++; + for(int i=0;i&v, int startIndex, int endIndex){ + if(startIndextemp = {1,3,2,12,444,5}; + mergeSort(temp,0, temp.size()-1); + for(int v:temp){ + cout< Date: Sat, 17 Oct 2020 16:06:04 +0530 Subject: [PATCH 02/21] Added LinearSort --- Sorting/LinearSort.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Sorting/LinearSort.cpp diff --git a/Sorting/LinearSort.cpp b/Sorting/LinearSort.cpp new file mode 100644 index 00000000..78e61d92 --- /dev/null +++ b/Sorting/LinearSort.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +void linearSort(vector&v){ + for(int i=0;itemp = {1,3,2,12,444,5}; + linearSort(temp); + for(int v:temp){ + cout< Date: Sat, 17 Oct 2020 17:47:48 +0530 Subject: [PATCH 03/21] Added HeapSort --- Sorting/HeapSort.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sorting/HeapSort.cpp diff --git a/Sorting/HeapSort.cpp b/Sorting/HeapSort.cpp new file mode 100644 index 00000000..2d1d46fe --- /dev/null +++ b/Sorting/HeapSort.cpp @@ -0,0 +1,40 @@ +#include +#define sz(v) ((int)v.size()) +using namespace std; +void heapify(vector&v, int SIZE, int ind){ + int left_child = (ind<<1)+1; + int right_child = left_child+1; + int to_swap; + if(left_child v[ind]) + to_swap = left_child; + else + to_swap = ind; + if(right_child v[to_swap]) + to_swap = right_child; + if(to_swap !=ind){ + int temp = v[to_swap]; + v[to_swap] = v[ind]; + v[ind] = temp; + heapify(v,SIZE,to_swap); + } +} +void heapSort(vector&v){ + vectorheap(sz(v)); + for(int i=(sz(v)-2)/2;i>=0;i--){ + heapify(v,sz(v),i); + } + for(int i=sz(v)-1;i>0;i--){ + int temp = v[0]; + v[0] = v[i]; + v[i] = temp; + + heapify(v,i,0); + } +} +int main(){ + vectortemp = {1,3,2,12,444,5}; + heapSort(temp); + for(int v:temp){ + cout< Date: Sat, 17 Oct 2020 18:19:38 +0530 Subject: [PATCH 04/21] Added BinarySeach --- Searching/BinarySearch.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Searching/BinarySearch.cpp diff --git a/Searching/BinarySearch.cpp b/Searching/BinarySearch.cpp new file mode 100644 index 00000000..c0820198 --- /dev/null +++ b/Searching/BinarySearch.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +//Create a predicate to return T/F for an ind +bool predicate(vector&v, int ind, int search_token){ + return v[ind] >= search_token; +} +int binarySeach(vector&v, int search_token){ + int low = 0,high = v.size(),mid; + while(low v = {1,2,4,5,11,20,25}; + cout< Date: Sun, 18 Oct 2020 08:24:30 +0530 Subject: [PATCH 05/21] Create bubblesort_java --- bubblesort_java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 bubblesort_java diff --git a/bubblesort_java b/bubblesort_java new file mode 100644 index 00000000..436662e3 --- /dev/null +++ b/bubblesort_java @@ -0,0 +1,35 @@ +public class BubbleSortExample { + static void bubbleSort(int[] arr) { + int n = arr.length; + int temp = 0; + for(int i=0; i < n; i++){ + for(int j=1; j < (n-i); j++){ + if(arr[j-1] > arr[j]){ + //swap elements + temp = arr[j-1]; + arr[j-1] = arr[j]; + arr[j] = temp; + } + + } + } + + } + public static void main(String[] args) { + int arr[] ={3,60,35,2,45,320,5}; + + System.out.println("Array Before Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + System.out.println(); + + bubbleSort(arr);//sorting array elements using bubble sort + + System.out.println("Array After Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + + } +} From 392deed9259907d30ba463703d2fc03ca21fcd9a Mon Sep 17 00:00:00 2001 From: huntingcodes <73046973+huntingcodes@users.noreply.github.com> Date: Sun, 18 Oct 2020 08:33:42 +0530 Subject: [PATCH 06/21] Create mirror inverse_java --- mirror inverse_java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 mirror inverse_java diff --git a/mirror inverse_java b/mirror inverse_java new file mode 100644 index 00000000..0f7c3b8c --- /dev/null +++ b/mirror inverse_java @@ -0,0 +1,28 @@ +// Java implementation of the approach +public class GFG { + + // Function that returns true if + // the array is mirror-inverse + static boolean isMirrorInverse(int arr[]) + { + for (int i = 0; i < arr.length; i++) { + + // If condition fails for any element + if (arr[arr[i]] != i) + return false; + } + + // Given array is mirror-inverse + return true; + } + + // Driver code + public static void main(String[] args) + { + int arr[] = { 1, 2, 3, 0 }; + if (isMirrorInverse(arr)) + System.out.println("Yes"); + else + System.out.println("No"); + } +} From 990b851b6e2898db749ebe969a3e2ddd8fb7fd81 Mon Sep 17 00:00:00 2001 From: huntingcodes <73046973+huntingcodes@users.noreply.github.com> Date: Sun, 18 Oct 2020 08:40:44 +0530 Subject: [PATCH 07/21] Create ascii-char_cpp --- ascii-char_cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 ascii-char_cpp diff --git a/ascii-char_cpp b/ascii-char_cpp new file mode 100644 index 00000000..697d3356 --- /dev/null +++ b/ascii-char_cpp @@ -0,0 +1,10 @@ +// CPP program to print +// ASCII Value of Character +#include +using namespace std; +int main() +{ + char c = 'A'; + cout << "The ASCII value of " << c << " is " << int(c); + return 0; +} From 1769869dc6eecab374a5d21d405d8d2fd55abaa9 Mon Sep 17 00:00:00 2001 From: ShivamDubey7 Date: Sun, 18 Oct 2020 11:58:40 +0530 Subject: [PATCH 08/21] Added Recursive BinarySeach --- Searching/BinarySearch.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Searching/BinarySearch.cpp b/Searching/BinarySearch.cpp index c0820198..1593eecb 100644 --- a/Searching/BinarySearch.cpp +++ b/Searching/BinarySearch.cpp @@ -17,8 +17,20 @@ int binarySeach(vector&v, int search_token){ if(v[low]==search_token) return low; return -1; } +int recursiveBinarySearch(vector&v, int search_token, int begin, int end){ + if(begin==end){ + if(v[begin]==search_token) + return begin; + return -1; + } + int mid = (begin+end)/2; + if(predicate(v,mid,search_token)){ + return recursiveBinarySearch(v,search_token,begin,mid); + } + return recursiveBinarySearch(v,search_token,mid+1,end); +} int main(){ vector v = {1,2,4,5,11,20,25}; - cout< Date: Sun, 18 Oct 2020 12:11:54 +0530 Subject: [PATCH 09/21] Renamed --- Sorting/BubbleSort.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Sorting/BubbleSort.java diff --git a/Sorting/BubbleSort.java b/Sorting/BubbleSort.java new file mode 100644 index 00000000..6932c335 --- /dev/null +++ b/Sorting/BubbleSort.java @@ -0,0 +1,34 @@ +public class BubbleSortExample { + static void bubbleSort(int[] arr) { + int n = arr.length; + int temp = 0; + for(int i=0; i < n; i++){ + for(int j=1; j < (n-i); j++){ + if(arr[j-1] > arr[j]){ + //swap elements + temp = arr[j-1]; + arr[j-1] = arr[j]; + arr[j] = temp; + } + + } + } + } + public static void main(String[] args) { + int arr[] ={3,60,35,2,45,320,5}; + + System.out.println("Array Before Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + System.out.println(); + + bubbleSort(arr);//sorting array elements using bubble sort + + System.out.println("Array After Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + + } +} From d63881b4c3928ccae4bd40b462c5d81aadbadae4 Mon Sep 17 00:00:00 2001 From: ShivamDubey7 Date: Sun, 18 Oct 2020 12:13:36 +0530 Subject: [PATCH 10/21] Removed --- bubblesort_java | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 bubblesort_java diff --git a/bubblesort_java b/bubblesort_java deleted file mode 100644 index 436662e3..00000000 --- a/bubblesort_java +++ /dev/null @@ -1,35 +0,0 @@ -public class BubbleSortExample { - static void bubbleSort(int[] arr) { - int n = arr.length; - int temp = 0; - for(int i=0; i < n; i++){ - for(int j=1; j < (n-i); j++){ - if(arr[j-1] > arr[j]){ - //swap elements - temp = arr[j-1]; - arr[j-1] = arr[j]; - arr[j] = temp; - } - - } - } - - } - public static void main(String[] args) { - int arr[] ={3,60,35,2,45,320,5}; - - System.out.println("Array Before Bubble Sort"); - for(int i=0; i < arr.length; i++){ - System.out.print(arr[i] + " "); - } - System.out.println(); - - bubbleSort(arr);//sorting array elements using bubble sort - - System.out.println("Array After Bubble Sort"); - for(int i=0; i < arr.length; i++){ - System.out.print(arr[i] + " "); - } - - } -} From f1a9b4f65cee909d8532529e981b1bcc9c17e086 Mon Sep 17 00:00:00 2001 From: ShivamDubey7 Date: Sun, 18 Oct 2020 12:31:03 +0530 Subject: [PATCH 11/21] Added Iterative exponentiation --- Maths/FastExponentiation.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Maths/FastExponentiation.cpp b/Maths/FastExponentiation.cpp index 8bf0b1e3..4041dd00 100644 --- a/Maths/FastExponentiation.cpp +++ b/Maths/FastExponentiation.cpp @@ -10,9 +10,21 @@ ll fastExpo(ll base, ll power){ } return temp*temp; } +ll fastExpoIterative(ll base, ll power){ + ll res = 1; + while(power >0){ + if(power&1){ + res*=base; + } + power/=2; + base*=base; + } + return res; + +} int main(){ ll base = 5; ll power = 3; - cout< Date: Sun, 18 Oct 2020 12:50:45 +0530 Subject: [PATCH 12/21] Added DFS --- Graph/DFS.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Graph/DFS.cpp diff --git a/Graph/DFS.cpp b/Graph/DFS.cpp new file mode 100644 index 00000000..d60ceaaf --- /dev/null +++ b/Graph/DFS.cpp @@ -0,0 +1,27 @@ +#include +#define pb push_back +using namespace std; +const int LIM = 50; +int vis[LIM]; +vector >AdjList(LIM); +void dfs(int u){ + cout<>n>>m; + for(int i=0;i>u>>v; + AdjList[u].pb(v); + AdjList[v].pb(u); + } + dfs(1); + +} \ No newline at end of file From d8bd23d82ad6f276cbae215903a65e7d0bc45dd5 Mon Sep 17 00:00:00 2001 From: email11235868 <93476692+email11235868@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:38:32 +0530 Subject: [PATCH 13/21] Create 1552b.cpp --- codeforces/1552b.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 codeforces/1552b.cpp diff --git a/codeforces/1552b.cpp b/codeforces/1552b.cpp new file mode 100644 index 00000000..d8e404fd --- /dev/null +++ b/codeforces/1552b.cpp @@ -0,0 +1,68 @@ +#include +#define ll long long int +#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++) +#define rep(i,n) for(int (i)=0;(i) ii; +void solve(){ + int n; + cin>>n; + vector>v(n,vector(5,0)); + rep(i,n){ + rep(j,5){ + cin>>v[i][j]; + } + } + vectorw = v[0]; + int ans=0; + bool exists= false; + + for(int i=1;i w[j])c2++; + } + if(c>=3 || c2>=3)exists = true; + if(c>=3){ + w = v[i]; + ans = i; + } + } + if(n > 1 && !exists){ + cout<<-1< w[j])c++; + } + if(c < 3){ + cout<<-1<>t; + while(t--) + solve(); + return 0; +} From 8bbb5556d82293889408afb8f5bc5bb5cb48df11 Mon Sep 17 00:00:00 2001 From: email11235868 <93476692+email11235868@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:44:41 +0530 Subject: [PATCH 14/21] Create 1530d.cpp --- codeforces/1530d.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 codeforces/1530d.cpp diff --git a/codeforces/1530d.cpp b/codeforces/1530d.cpp new file mode 100644 index 00000000..c6c138a0 --- /dev/null +++ b/codeforces/1530d.cpp @@ -0,0 +1,67 @@ +#include +#define ll long long int +#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++) +#define rep(i,n) for(int (i)=0;(i) ii; +void solve(){ + int n; + cin>>n; + mapm; + vectora(n); + vectorans(n); + rep(i,n)cin>>a[i],m[a[i]]++; + vectorv; + int cnt=0; + rep(i,n){ + if(m.find(i+1) == m.end()){ + v.push_back(i+1); + } + } + rep(i,n){ + if(m[a[i]] > 1){ + m[a[i]]--; + ans[i] = v.back(); + v.pop_back(); + } + else{ + ans[i] = a[i]; + cnt++; + } + } + //either there is 1 self mapping or none + rep(i,n){ + if(ans[i] == i+1){ + + rep(j,n){ + if(ans[j] == a[i]){ + ans[j] = i+1; + break; + } + } + ans[i] = a[i]; + break; + } + } + cout<>test; + while(test--){ + solve(); + } + return 0; +} From d1d46b65132e858586d0ffaa200d71c3ca111773 Mon Sep 17 00:00:00 2001 From: email11235868 <93476692+email11235868@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:45:21 +0530 Subject: [PATCH 15/21] Create 1530c.cpp --- codeforces/1530c.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 codeforces/1530c.cpp diff --git a/codeforces/1530c.cpp b/codeforces/1530c.cpp new file mode 100644 index 00000000..ef8187e0 --- /dev/null +++ b/codeforces/1530c.cpp @@ -0,0 +1,74 @@ +#include +#define ll long long int +#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++) +#define rep(i,n) for(int (i)=0;(i) ii; +bool p(int k, vector&v1, vector&v2){ + int x = (k-sz(v1)); + int canTake = k-k/4; + int take = min(canTake, x); + int s1 = take*100 + v1[canTake-take-1]; + int s2 = canTake < sz(v2) ? v2[canTake-1] : v2.back(); + return s1>=s2; +} +void solve(){ + int n; + cin>>n; + vectorv1(n); + rep(i,n){ + cin>>v1[i]; + } + + vectorv2(n); + rep(i,n){ + cin>>v2[i]; + } + sort(all(v1)); + reverse(all(v1)); + sort(all(v2)); + reverse(all(v2)); + for(int i=1;i>t; + while(t--) + solve(); + return 0; +} +// 1 +// 4 +// 20 30 40 50 +// 100 100 100 100 From 58d6ddc4015510f09fe2c8ba5233696c55a2db34 Mon Sep 17 00:00:00 2001 From: email11235868 <93476692+email11235868@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:45:55 +0530 Subject: [PATCH 16/21] Create 1550b.cpp --- codeforces/1550b.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 codeforces/1550b.cpp diff --git a/codeforces/1550b.cpp b/codeforces/1550b.cpp new file mode 100644 index 00000000..50d93a84 --- /dev/null +++ b/codeforces/1550b.cpp @@ -0,0 +1,42 @@ +#include +#define ll long long int +#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++) +#define rep(i,n) for(int (i)=0;(i) ii; +void solve(){ + int n,a,b; + cin>>n>>a>>b; + string s; + cin>>s; + vectorc(2,0); + c[s[0]-'0']++; + for(int i=1;i0) + ans += b*n; + else + ans += b*(min(c[0],c[1])+1); + cout<>t; + while(t--) + solve(); + return 0; +} From 7697bac6b5d69e753f60fef13de921e20d0be8cc Mon Sep 17 00:00:00 2001 From: Shaurya Sharma <79473274+ShaSha-Codes@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:48:29 +0530 Subject: [PATCH 17/21] Added Selection Sort --- Sorting/SelectionSort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Sorting/SelectionSort.py diff --git a/Sorting/SelectionSort.py b/Sorting/SelectionSort.py new file mode 100644 index 00000000..ba1b5d37 --- /dev/null +++ b/Sorting/SelectionSort.py @@ -0,0 +1,27 @@ +# Selection sort takes n-1 passes to complete +# During every iteration index of minimum number/string is found in the innermost loop and then swapped once it comes out of that loop +# No flag like improvised Bubble Sort so even if we put sorted list it is still going to take O(n^2) time complexity +# Worst Time Complexity: O(n^2) +# Average Time Complexity: O(n^2) +# Best Time Complexity: O(n^2) + +print("Number Sequence Before Selection Sort") +seq=[5,2,1,8,3,5,3] +print(seq) + +def selectionSort(nums): + n=len(nums) + for i in range(n-1): + iMin=i + for j in range(i+1,n): + if(nums[iMin]>nums[j]): + iMin=j + temp=nums[i] + nums[i]=nums[iMin] + nums[iMin]=temp + return nums + +print("Number Sequence After Selection Sort") +print(selectionSort(seq)) + + From 9af438a34096b21419c8d5d4aba53e6e12b61a67 Mon Sep 17 00:00:00 2001 From: email11235866 <93476044+email11235866@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:52:20 +0530 Subject: [PATCH 18/21] Create 1550b --- cf/1550b | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 cf/1550b diff --git a/cf/1550b b/cf/1550b new file mode 100644 index 00000000..50d93a84 --- /dev/null +++ b/cf/1550b @@ -0,0 +1,42 @@ +#include +#define ll long long int +#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++) +#define rep(i,n) for(int (i)=0;(i) ii; +void solve(){ + int n,a,b; + cin>>n>>a>>b; + string s; + cin>>s; + vectorc(2,0); + c[s[0]-'0']++; + for(int i=1;i0) + ans += b*n; + else + ans += b*(min(c[0],c[1])+1); + cout<>t; + while(t--) + solve(); + return 0; +} From bc2c61393c7ae3f679972d2b70835c7c66ffaf29 Mon Sep 17 00:00:00 2001 From: email11235866 <93476044+email11235866@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:53:09 +0530 Subject: [PATCH 19/21] Create second.cpp --- cf/second.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 cf/second.cpp diff --git a/cf/second.cpp b/cf/second.cpp new file mode 100644 index 00000000..ef8187e0 --- /dev/null +++ b/cf/second.cpp @@ -0,0 +1,74 @@ +#include +#define ll long long int +#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++) +#define rep(i,n) for(int (i)=0;(i) ii; +bool p(int k, vector&v1, vector&v2){ + int x = (k-sz(v1)); + int canTake = k-k/4; + int take = min(canTake, x); + int s1 = take*100 + v1[canTake-take-1]; + int s2 = canTake < sz(v2) ? v2[canTake-1] : v2.back(); + return s1>=s2; +} +void solve(){ + int n; + cin>>n; + vectorv1(n); + rep(i,n){ + cin>>v1[i]; + } + + vectorv2(n); + rep(i,n){ + cin>>v2[i]; + } + sort(all(v1)); + reverse(all(v1)); + sort(all(v2)); + reverse(all(v2)); + for(int i=1;i>t; + while(t--) + solve(); + return 0; +} +// 1 +// 4 +// 20 30 40 50 +// 100 100 100 100 From 3a3b0c90ba53d2bd0c2efafcb2021959c1be3a63 Mon Sep 17 00:00:00 2001 From: email11235866 <93476044+email11235866@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:53:33 +0530 Subject: [PATCH 20/21] Create third.cpp --- cf/third.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 cf/third.cpp diff --git a/cf/third.cpp b/cf/third.cpp new file mode 100644 index 00000000..c6c138a0 --- /dev/null +++ b/cf/third.cpp @@ -0,0 +1,67 @@ +#include +#define ll long long int +#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++) +#define rep(i,n) for(int (i)=0;(i) ii; +void solve(){ + int n; + cin>>n; + mapm; + vectora(n); + vectorans(n); + rep(i,n)cin>>a[i],m[a[i]]++; + vectorv; + int cnt=0; + rep(i,n){ + if(m.find(i+1) == m.end()){ + v.push_back(i+1); + } + } + rep(i,n){ + if(m[a[i]] > 1){ + m[a[i]]--; + ans[i] = v.back(); + v.pop_back(); + } + else{ + ans[i] = a[i]; + cnt++; + } + } + //either there is 1 self mapping or none + rep(i,n){ + if(ans[i] == i+1){ + + rep(j,n){ + if(ans[j] == a[i]){ + ans[j] = i+1; + break; + } + } + ans[i] = a[i]; + break; + } + } + cout<>test; + while(test--){ + solve(); + } + return 0; +} From 41909273d112bebb5df1e6f9c3d6c5a7d7c472bd Mon Sep 17 00:00:00 2001 From: email11235866 <93476044+email11235866@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:54:01 +0530 Subject: [PATCH 21/21] Create fourth.cpp --- cf/fourth.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cf/fourth.cpp diff --git a/cf/fourth.cpp b/cf/fourth.cpp new file mode 100644 index 00000000..d8e404fd --- /dev/null +++ b/cf/fourth.cpp @@ -0,0 +1,68 @@ +#include +#define ll long long int +#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++) +#define rep(i,n) for(int (i)=0;(i) ii; +void solve(){ + int n; + cin>>n; + vector>v(n,vector(5,0)); + rep(i,n){ + rep(j,5){ + cin>>v[i][j]; + } + } + vectorw = v[0]; + int ans=0; + bool exists= false; + + for(int i=1;i w[j])c2++; + } + if(c>=3 || c2>=3)exists = true; + if(c>=3){ + w = v[i]; + ans = i; + } + } + if(n > 1 && !exists){ + cout<<-1< w[j])c++; + } + if(c < 3){ + cout<<-1<>t; + while(t--) + solve(); + return 0; +}