This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> | |
#define MAX 100 | |
int INFINITY = INT_MAX; | |
struct Edge{ | |
int u, v, weight; | |
}; |
NewerOlder