P

@praveen11

every(), some() in js

NodeJS
4 years ago
var a=[1,3,5,7] var b=a.every( function(x){ return x%2==1; } ) console.log(b) var c=a.some( function(x){

map(), filter() in js

NodeJS
4 years ago
var a=[1,2,3,4] console.log(a) var b=a.map( function(x){ return x+1; } ) console.log(b) var c=a.filter(

substrings of a string

C++
4 years ago
#include <iostream> using namespace std; int main() { string s; cin>>s; for(int i=0;i<s.length();i++){ cout<<s.substr(0,i+1)<<"\n"; } return 0; }

mergesort implementation

C++
4 years ago
#include <iostream> using namespace std; void merge(int *a,int low,int mid,int high){ int x=mid-low+1; int y=high-mid; int l[x],r[y]; for(int i=0;i<x;i++){ l[i]=a[low+i]; }

beautiful function hackwithinfy question 2

Python
4 years ago
n=int(input()) res=[] count=0 while(True): if(n in res): break else: res.append(n) n+=1 n=str(n).rstrip('0')

minimum withdrawals hackwithinfy sample question 1

Python
4 years ago
n=int(input()) a=[int(i) for i in input().split()] k=int(input()) if(sum(a)<k): print('-1') else: x,y = 0,n-1 count = 0 while(x <= y): if(a[x]>=a[y] and k>0):

implementation of binary tree (Not BST)

C++
4 years ago
#include <iostream> using namespace std; class binary{ public: int data; binary *left,*right; binary(int value){ data = value;

sort 0 1 2 in a list/array using dutch algorithm(3 pointer algo)

Python
4 years ago
l=[int(i) for i in input().split()] low=mid=0 high=len(l)-1 while(mid<=high): if(l[mid]==0): l[low],l[mid]=l[mid],l[low] mid+=1 low+=1 elif(l[mid]==1): mid+=1

next permutation

Python
4 years ago
n=int(input()) l=[int(i) for i in str(n)] length=len(l) pt=0 for i in range(length-1,0,-1): if(l[i-1]<l[i]): pt=i-1 break if(pt==0): l.sort()

set matrix zeroes

C++
4 years ago
#include <iostream> #include <vector> using namespace std; int main() { vector<vector<int>> v { {0,1,1,0}, {1,1,0,1}, {1,1,1,1} };

different pattern (alpha-diamond)

Python
4 years ago
n=int(input()) r=2*n-1 c=2*r-1 mid=(c+1)//2 x=mid y=mid for i in range(1,r+1): for j in range(1,c+1): if((j>=x and j<=y) and j&1): if(j<=mid):

longest contiguos subsequence or not

Python
4 years ago
a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] c=[int(i) for i in input().split()] if(set(b) != set(c)): print("False") else: temp=[] res=[] max_len=0 for i in a:

remove digit and check the strings

Python
4 years ago
s=input() t=input() count=0 temp='' for i in range(len(s)): if(s[i]>='0' and s[i]<='9'): temp=s[0:i]+s[i+1:] if(temp < t): count+=1 temp=''

string transformation

Python
4 years ago
def tolower(x): return x.lower() def toupper(x): return x.upper() s=input() ans='' ans+=s[0] for i in range(1,len(s)):

merging of two strings

Python
4 years ago
""" merging two strings based on occurence of the chars and then lexcicographically smallest """ s1=input() s2=input() i=j=0 ans='' while(i<len(s1) and j<len(s2)):

number of possible paths from initial pos to final pos

C++
4 years ago
#include <iostream> using namespace std; int total_paths(int m,int n){ if(m==1 || n==1){ return 1; } return total_paths(m-1,n) + total_paths(m,n-1); // + total_paths(m-1,n-1); //row wise column wise diagonal wise }

copy_n c++ tricks

C++
4 years ago
#include <bits/stdc++.h> using namespace std; int main() { int a[5]={1,2,3,5,6}; int b[5]; copy_n(a,5,b); for(int i=0;i<5;i++){ cout<<b[i]<<" "; } return 0;

Is power of two or not using bit manip

C++
4 years ago
#include <iostream> #include<math.h> using namespace std; int main() { int n; cin>>n; if(n && (!(n&(n-1)))) cout<<"Yes"; else cout<<"No";

number of digits of a number

C++
4 years ago
#include <iostream> #include<math.h> using namespace std; int main() { int n=12345; cout<<floor(log10(n))+1; return 0; }

infytq question 2 9th_mrng

Python
4 years ago
import math def isperfect(n): if (math.ceil(math.sqrt(n)) == math.floor(math.sqrt(n))): return True else: return False n=int(input()) l=[]