parikshit223933 / coding-ninjas-competitive-programming Goto Github PK
View Code? Open in Web Editor NEWSolutions to all the questions I solved during the Competitive programming course with Coding Ninjas.
Solutions to all the questions I solved during the Competitive programming course with Coding Ninjas.
warning: control may reach end of non-void function
/* Its Gary's birthday today and he has ordered his favourite square cake consisting of '0's and '1's . But Gary wants the biggest piece of '1's and no '0's . A piece of cake is defined as a part which consist of only '1's, and all '1's share an edge with eachother on the cake. Given the size of cake N and the cake , can you find the size of the biggest piece of '1's for Gary ?
Constraints :
1<=N<=1000
Input Format :
Line 1 : An integer N denoting the size of cake
Next N lines : N characters denoting the cake
Output Format :
Size of the biggest piece of '1's and no '0's
Sample Input :
2
11
01
Sample Output :
3 */
int count_ones(vector<vector> &arr, int n, int i, int j, bool **visited)
{
int count = 1;
if (i > 0 && !visited[i - 1][j] && arr[i - 1][j] == 1) //up
{
visited[i - 1][j] = true;
count += count_ones(arr, n, i - 1, j, visited);
}
if (j > 0 && !visited[i][j - 1] && arr[i][j - 1] == 1) //left
{
visited[i][j - 1] = true;
count += count_ones(arr, n, i, j - 1, visited);
}
if (i < n - 1 && !visited[i + 1][j] && arr[i + 1][j] == 1) //down
{
visited[i + 1][j] = true;
count += count_ones(arr, n, i + 1, j, visited);
}
if (j < n - 1 && !visited[i][j + 1] && arr[i][j + 1] == 1) //right
{
visited[i][j + 1] = true;
count += count_ones(arr, n, i, j + 1, visited);
}
return count;
}
int getBiggestPieceSize(vector<vector> &arr, int n) {
// Write your code here
int maximum=0;
bool **visited = new bool *[n];
for (int i = 0; i < n; i++)
{
visited[i] = new bool[n];
for (int j = 0; j < n; j++)
{
visited[i][j] = false;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (arr[i][j] == 1 & !visited[i][j])
{
visited[i][j] = true;
int current_maximum = count_ones(arr, n, i, j, visited);
if (current_maximum > maximum)
{
maximum = current_maximum;
}
// for (int p = 0; p < n; p++)
// {
// for (int q = 0; q < n; q++)
// {
// visited[p][q] = false;
// }
// }
}
}
}
return maximum;
}
/* Given a random integer array A of size N. Find and print the pair of elements in the array which sum to 0.
Array A can contain duplicate elements.
While printing a pair, print the smaller element first.
That is, if a valid pair is (6, -6) print "-6 6". There is no constraint that out of 5 pairs which have to be printed in 1st line. You can print pairs in any order, just be careful about the order of elements in a pair.
Input format :
Line 1 : Integer N (Array size)
Line 2 : Array elements (separated by space)
Output format :
Line 1 : Pair 1 elements (separated by space)
Line 2 : Pair 2 elements (separated by space)
Line 3 : and so on
Constraints :
0 <= N <= 10^4
Sample Input:
5
2 1 -2 2 3
Sample Output :
-2 2
-2 2 */
#include<bits/stdc++.h>
using namespace std;
int pairSum(int arr, int n) {
int count=0;
unordered_map<int,int> m;
for(int i=0;i<n;i++){
m[arr[i]]+=1;
}
for(int i=0;i<n;i++){
if(arr[i]==0) continue; // we will handle 0 seperately
int pos = m[arr[i]];
int neg = m[-arr[i]];
if(pos >0 && neg >0){
count = count + (posneg);
m[arr[i]]=0;
m[-arr[i]]=0;
}
}
//handle 0 elements
int zeroes = m[0];
count = count + (zeroes*(zeroes-1))/2;
return count;
}
In the question "String Maker", the int main() is not required at last three lines.
we can ignore .exe files by updating the .gitignore file
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.