From e9322d8e19ec66c192f56313c37cf0e0d3358272 Mon Sep 17 00:00:00 2001 From: Yash Gutgutia <46281509+ygutgutia@users.noreply.github.com> Date: Thu, 29 Jul 2021 14:58:39 +0530 Subject: [PATCH] Update Noble Integer.cpp --- Arrays/Noble Integer.cpp | 41 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Arrays/Noble Integer.cpp b/Arrays/Noble Integer.cpp index 7edb6c7..d82a625 100644 --- a/Arrays/Noble Integer.cpp +++ b/Arrays/Noble Integer.cpp @@ -1,24 +1,25 @@ +// O(n) Solution without sorting using frequency table + int Solution::solve(vector &A) { - int n = A.size(), ans = -1, i = 0; - - if(!n){ - return ans; + int n = A.size(); + vector freq(n, 0); + int small = 0; // stores how many elements are smaller than the current element + + for(int i=0; i=n) + continue; // Only numbers between 0 to n-1 can be a solution + freq[A[i]]++; // Count frequency of all elements in 0 to n-1 } - - sort(A.begin(), A.end()); - - while(i < n){ - while(i+1 < n && A[i] == A[i+1]){ - i++; - } - - if(n - (i+1) == A[i]){ - ans = 1; - break; - } - - i++; + + for(int i=0; i