Posts

Showing posts from August, 2023

Lonely Integer - Given an array of integers, where all elements but one occur twice, find the unique element. Hackerrank Solution

Given an array of integers, where all elements but one occur twice, find the unique element. Example a = [1, 2, 3, 4, 3, 2 ,2] The unique element is 4. Function Description Complete the lonelyinteger function in the editor below. lonelyinteger has the following parameter(s): ·        int a[n]: an array of integers Returns ·        int: the element that occurs only once Input Format The first line contains a single integer, n  , the number of integers in the array. The second line contains n  space-separated integers that describe the values in  a . Constraints ·        1 <= n < 100 ·        It is guaranteed that n is an odd number and that there is one unique element. ·        0 <= a[i] <= 100, where 0 <= i < n. In Java  import java.io.*; import j...

Diagonal Difference - HackerRank Solutions

  Given a square matrix, calculate the absolute difference between the sums of its diagonals. For example, the square matrix  is shown below: 1 2 3 4 5 6 9 8 9   The left-to-right diagonal 1 + 5 + 9 = 15. The right to left diagonal 3 + 5 + 9 = 17. Their absolute difference is 2. Function description Complete the diagonalDifference  function in the editor below. diagonalDifference takes the following parameter: int arr[n][m]: an array of integers Return int: the absolute diagonal difference Input Format The first line contains a single integer, , the number of rows and columns in the square matrix . Each of the next  lines describes a row, , and consists of  space-separated integers . Output Format Return the absolute difference between the sums of the matrix's two diagonals as a single integer. Sample Input 3 11 2 4 4 5 6 10 8 -12 Sample Output 15 Explanati...