The reason why your solution cannot be correct and do O(n): ways++ this is a clear indicator. You need to find each subarray. And there can be n * (n + 1) / 2, e.g.: num=[0, 0, 0, 0], k=0 .
Two pointers was my first idea for this task as well. The reason, why i discarded it is that for straightforward application of TP there should be no more than 1 sub-array per beginning or end. It's not the case for this problem.