Skip to content

Instantly share code, notes, and snippets.

View samarthsewlani's full-sized avatar

Samarth Sewlani samarthsewlani

View GitHub Profile
@samarthsewlani
samarthsewlani / DiagonalDifference.py
Created July 2, 2024 13:42
Diagonal Difference HackerRank Solution ( Python )
n = int(input())
lst = []
for i in range(n):
lst.append([int(x) for x in input().split()])
a, b = 0, 0
for i in range(n):
a += lst[i][i]
b += lst[i][n-i-1]
print(abs(a-b))
@samarthsewlani
samarthsewlani / DiagonalDifference.java
Created July 2, 2024 13:40
Diagonal Difference HackerRank Solution
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int matrix[][] = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
matrix[i][j] = sc.nextInt();
@samarthsewlani
samarthsewlani / CompareTriplets.py
Created July 2, 2024 13:39
Compare Triplets HackerRank Solution ( Pyton )
n = 3
alice = [int(x) for x in input().split()]
bob = [int(x) for x in input().split()]
aliceScore, bobScore = 0, 0
for i in range(n):
a, b = alice[i], bob[i]
if(a>b):
aliceScore+=1
elif(b>a):
bobScore+=1
@samarthsewlani
samarthsewlani / CompareTriplets.java
Created June 29, 2024 10:19
Compare Triplets HackerRank Solution
// https://www.hackerrank.com/contests/cp-24-25-batch-coding-fundamentals-classwork/challenges/compare-the-triplets/problem
import java.util.*;
public class Solution {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// Step 1: Take inputs
int n = 3;
int alice[] = new int[n], bob[] = new int[n];
for(int i=0;i<n;i++) alice[i] = sc.nextInt();
@samarthsewlani
samarthsewlani / PairsOfSongsDivisibleBy60.java
Created June 6, 2024 09:57
Pairs of Songs With Total Durations Divisible by 60
//https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/
class Solution {
public int numPairsDivisibleBy60(int[] time) {
int n = time.length, count = 0;
for(int i=0;i<n;i++) time[i] %= 60;
Map<Integer, Integer> visited = new HashMap<>();
for(int i=0;i<n;i++){
int value = (60-time[i])%60;
if(visited.containsKey(value)){
count += visited.get(value);
@samarthsewlani
samarthsewlani / LongestPalindromeFromTwoLetterWords.java
Created June 6, 2024 09:56
Longest Palindrome by Concatenating Two Letter Words
//https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/
class Solution {
public int longestPalindrome(String[] words) {
int n = words.length, count = 0;
Map<String, Integer> visited = new HashMap<>();
for(int i=0;i<n;i++){
String word = words[i];
String reverseWord = word.charAt(1) + "" + word.charAt(0) ;
if(visited.containsKey(reverseWord) && visited.get(reverseWord)>0){
count += 4;
@samarthsewlani
samarthsewlani / RemoveAllAdjacentInString2.java
Created June 6, 2024 09:53
Remove All Adjacent In String 2
//https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/description/
class Solution {
public String removeDuplicates(String s, int k) {
Deque<Character> stack = new ArrayDeque<>();
Deque<Integer> nums = new ArrayDeque<>();
stack.push('#'); nums.push(1);
int n = s.length();
for(int i=0;i<n;i++){
if(s.charAt(i) == stack.peek()){
nums.push(nums.peek()+1);
@samarthsewlani
samarthsewlani / TSP.py
Last active April 26, 2024 08:54
Travelling Salesperson using DP in Python
import sys
INFINITY = sys.maxsize
def travelling_salesperson_problem(weight, n, i, unvisited_cities):
if(len(unvisited_cities) == 0):
# If no cities left to traverse return to starting city 1
# Assumption we always start and end tour at city 1
dp[i][tuple()] = weight[i-1][0]
return weight[i-1][0]
@samarthsewlani
samarthsewlani / SumOfSubsets.c
Created April 24, 2024 10:32
Sum Of Subsets in C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX 100
int INFINITY = INT_MAX;
int X[MAX];
int compare(const void* num1, const void* num2) // comparing function
@samarthsewlani
samarthsewlani / BellmanFord.c
Created April 24, 2024 09:16
Bellman Ford in C
#include <stdio.h>
#include <limits.h>
#define MAX 100
int INFINITY = INT_MAX;
struct Edge{
int u, v, weight;
};