-2

Hey I am trying to study DS&A and I have tried to understand this problem but I don't understand it. I have looked at other forms but most of them are in other languages. Can someone please explain this problem to me in easiest simple form of JAVA. Thank you

Tech Man
  • 1
  • 1
  • 1
    and what does “DS&A” mean? – Holger Sep 28 '22 at 16:59
  • Data Structures and Algorithms – Tech Man Sep 28 '22 at 17:02
  • 1
    Ok, then it’s not explaining what “this problem” is referring to. – Holger Sep 28 '22 at 17:04
  • the problem is "Find the number of 1's digits in a binary representation of a number" and my question is can someone explain it to me in simple easy form of java – Tech Man Sep 28 '22 at 17:06
  • 1
    There's a method for this: https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/Integer.html#bitCount(int). However, I don't think you're allowed to use that. – Rob Spoor Sep 28 '22 at 17:13
  • No, Im trying to understand how to write this from scratch. – Tech Man Sep 28 '22 at 17:16
  • 1
    For example, “iterate from 0 to 31 and test whether the bit is set and increment your count if the bit is set”. If you want more elaborated algorithms, check https://stackoverflow.com/q/109023/2711488 – Holger Sep 28 '22 at 17:27

1 Answers1

0

You can use Sort algorithms and then binary search to count the number of 1.

For ref. https://www.geeksforgeeks.org/count-1s-sorted-binary-array/

For unsorted binary arrays, Given an unsorted binary array, count number of 1's, where allowed only to check if an entire subarray is all zeros

Gaurav
  • 50
  • 8