Skip to content

Instantly share code, notes, and snippets.

View viveksyngh's full-sized avatar

Vivek Kumar Singh viveksyngh

View GitHub Profile
@viveksyngh
viveksyngh / Stack.c
Last active August 29, 2015 14:25
This is an array Implementation of Stack
/* Array Implementation of Stack */
/* Author Vivek Singh */
/* Last Modified on 19/7/2015 */
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 101
int A[MAX_SIZE];
int TOP = -1;
void Push(int item)
@viveksyngh
viveksyngh / 2D-Spiral.py
Created July 23, 2015 05:37
Prints a 2D array in Spiral Order
def spiralOrder(A):
result = []
T = 0
B = len(A) - 1
L = 0
R = len(A[0]) -1
dir = 0
while T <= B and L <= R :
if dir == 0 :
for i in range(L, R+1) :
@viveksyngh
viveksyngh / MaxSubset.py
Created July 24, 2015 21:41
Function will return Non Negative subarray with maximum sum
def maxset(self, A):
maxSum = 0
curretMaxSum = 0
maxStart = 0
maxEnd = 0
currentStart = 0
for i in range(len(A)) :
if A[i] >= 0 :
curretMaxSum += A[i]
if curretMaxSum > maxSum :
@viveksyngh
viveksyngh / pascal'sTriangle.py
Created July 26, 2015 18:51
Printing pascal traingle
def generate(A):
triangle = []
for rownum in range(A) :
nextValue = 1
row = [nextValue]
for i in range(rownum) :
nextValue = nextValue * (rownum - i) * 1 / (i + 1)
row.append(nextValue)
triangle.append(row)
return triangle
@viveksyngh
viveksyngh / maxSumOfSubarray.py
Created July 27, 2015 19:24
Function will return maximum sum of sub array of an array
def maxSubArray(A):
maxSumSoFar = 0
currentMaxSum = 0
for i in range(0, len(A)) :
currentMaxSum += A[i]
if currentMaxSum < 0 :
currentMaxSum = 0
if maxSumSoFar < currentMaxSum :
maxSumSoFar = currentMaxSum
if maxSumSoFar == 0 and max(A) >= 0 :
@viveksyngh
viveksyngh / PLUS1.py
Created July 28, 2015 19:42
Will return List of digits of a non - negative number incremented by 1
def plusOne(A):
"""
:param A: List containing digits of a non - negative number
:return: List containing digits after addition of 1
""""
length = len(A)
number = 0
i = length - 1
while A[i] + 1 > 9 and i >= 0:
@viveksyngh
viveksyngh / NARRAY.py
Created July 28, 2015 21:05
Find the duplicate and missing element in list of integers from 1 to N
def repeatedNumber(A):
"""
:param A: A tupple of integer
:return: Return a list [duplicate element , missing element]
"""
n = len(A)
dup = 0
for i in range(len(A)) :
if A[abs(A[i]) - 1] > 0 :
@viveksyngh
viveksyngh / LargestNum.py
Created July 29, 2015 19:34
Largest number that can be formed using list of non negative integers
__author__ = 'Vivek'
def comp(X, Y) :
"""
:param: X, Y two integers
:return : -1, 0 or 1
"""
XY = str(X) + str(Y)
YX = str(Y) + str(X)
if XY < YX :
return -1
@viveksyngh
viveksyngh / NEXTPERM.py
Created July 29, 2015 21:06
rearranges numbers into the numerically next greater permutation of numbers
__author__ = 'Vivek'
def nextPermutation(A):
"""
:param: List of integer
:return : numerically next greater permutation of integers
"""
i = -1
last = False
for k in range(len(A)-1) :
if A[k] < A[k+1] :
@viveksyngh
viveksyngh / Positive.py
Created July 30, 2015 21:16
Find Smallest positive integers missing in a lsit of integers
__author__ = 'Vivek'
#Returns smallest missing posistive integer in a list of integers
def divideArray(A) :
"""
:param: List of integers
:return: List having only positive integers
"""
j = 0
for i in range(len(A)) :
if A[i] <= 0 :