Mentor Session Vikas Sharma
Original Session
Date: 21 Dec 2024
- Sum of two numbers without using arithmetic operators
- GFG here
int sum(int a , int b) { //code here while (b != 0) { int carry = (a&b)<<1; a = a^b; b = carry; } return a; }
- Search in a Row-Column sorted matrix, solve in O(n)
- GFG here
static boolean matSearch(int mat[][], int x) { // your code here int row = 0, col = mat[0].length-1; while (col > -1 && row < mat.length) { if (x > mat[row][col]) { row ++; } else if (x < mat[row][col]) { col --; } else { return true; } } return false; }
- unsorted array, Find two indexes such that, arr[i] - arr[j] == k, k is target
static void towSumAMinusBEqK(int[] nums, int k) { // i -j = k, k+j = i; // [-1,2,5,8,-3], k=3 Map<Integer, Integer> map = new HashMap(); for (int i=0; i< nums.length; i++) { map.put(nums[i], i); } for (int i=0; i< nums.length; i++) { int sub = k+nums[i]; if (!map.containsKey(sub)) { map.put(nums[i], i); continue; } int j = map.get(sub); System.out.print(" i " + i + " j " + j); break; } }