001package algs14;
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac OneSum.java
005 *  Execution:    java OneSum input.txt
006 *  Dependencies: In.java Stopwatch.java
007 *
008 *  A program with N running time. Read in N integers
009 *  and counts the number that are exactly 0.
010 *
011 *  % java OneSum 2Kints.txt
012 *  0
013 *************************************************************************/
014
015public class XOneSum {
016
017        // print indices i such that a[i] = 0
018        public static void printAll(int[] a) {
019                int N = a.length;
020                for (int i = 0; i < N; i++) {
021                        if (a[i] == 0) {
022                                StdOut.println(a[i]);
023                        }
024                }
025        }
026
027
028        // return number of indices i such that a[i] = 0
029        public static int count(int[] a) {
030                int N = a.length;
031                int cnt = 0;
032                for (int i = 0; i < N; i++) {
033                        if (a[i] == 0) {
034                                cnt++;
035                        }
036                }
037                return cnt;
038        }
039
040        public static void main(String[] args)  {
041                args = new String[] { "data/2Kints.txt" };
042                int[] a = new In(args[0]).readAllInts();
043                Stopwatch timer = new Stopwatch();
044                int cnt = count(a);
045                StdOut.println("elapsed time = " + timer.elapsedTime());
046                StdOut.println(cnt);
047        }
048}