Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • Subarray Sum
  • Duplicted number increase by 1

Was this helpful?

  1. 19. Company Summary

LinkedIn

Subarray Sum

Given a array of integers, find out the sum of all subarrays

Ex:[1,2,3] -> [1], [2], [3], [1,2], [2,3], [1,2,3]

sum = 20

  • Every element arr[i] appears in two types of subsets:

    • In sybarrays beginning with arr[i]. There are

      (n-i) such subsets. For example [2] appears

      in [2] and [2, 3].

    • In (n-i)*i subarrays where this element is not

      first element. For example [2] appears in

      [1, 2] and [1, 2, 3].

Sum = (n-i) + (n-i)*i = (n-i)(i+1)

public static long SubArraySum( int arr[] , int n )
{
    long result = 0;

    // computing sum of subarray using formula
    for (int i=0; i<n; i++)
        result += (arr[i] * (i+1) * (n-i));
    return result ;
}

Duplicted number increase by 1

E.g. [1,2,2,3,3] -> [1,2,3,4,5]

private static List<Integer> f(int[] A){
    List<Integer> list = new ArrayList<>();
    list.add(A[0]);
    int prev = A[0];
    for(int i=1; i<A.length; i++){
        if(A[i]<=prev) list.add(prev+1);
        else list.add(A[i]);
        prev = list.get(list.size()-1);
    }
    return list;
}
 public static void main(String []args){
     int[] A = new int[]{1,2,2,3,3};
    System.out.println(f(A));
 }
PreviousHackerrankNext20. Design

Last updated 5 years ago

Was this helpful?