Pages

Sunday, 30 July 2023

LeetCode - 1 - Two Sum

 class Solution {

    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> hm = new  HashMap<Integer, Integer>();
        for (int i = 0 ; i < nums.length ; i++) {
            hm.put(nums[i], i);
        }
        for (int i = 0 ; i < nums.length ; i++) {
            var diff = target - nums[i];
            if (hm.containsKey(diff)) {
                int index = hm.get(diff);
                if (index != i) {
                    return new int[]{i, index};
                }
            }
        }
        return new int[0];
    }
}

Sunday, 13 December 2015

CodeEval - Black card - Easy

import sys

def black_card(test):
    arr = test.split(" | ")
    n = int(arr[1])
    valz = arr[0].split()
    while len(valz) > 1:
        valz.pop((n%len(valz))-1)
    return valz.pop()

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    print black_card(test)
test_cases.close()