< Summary

Information
Class: LeetCode.Algorithms.PrimeSubtractionOperation.PrimeSubtractionOperationIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PrimeSubtractionOperation\PrimeSubtractionOperationIterative.cs
Line coverage
100%
Covered lines: 189
Uncovered lines: 0
Coverable lines: 189
Total lines: 223
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
PrimeSubOperation(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PrimeSubtractionOperation\PrimeSubtractionOperationIterative.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.PrimeSubtractionOperation;
 13
 14/// <inheritdoc />
 15public class PrimeSubtractionOperationIterative : IPrimeSubtractionOperation
 16{
 117    private static readonly List<int> Primes =
 118    [
 119        2,
 120        3,
 121        5,
 122        7,
 123        11,
 124        13,
 125        17,
 126        19,
 127        23,
 128        29,
 129        31,
 130        37,
 131        41,
 132        43,
 133        47,
 134        53,
 135        59,
 136        61,
 137        67,
 138        71,
 139        73,
 140        79,
 141        83,
 142        89,
 143        97,
 144        101,
 145        103,
 146        107,
 147        109,
 148        113,
 149        127,
 150        131,
 151        137,
 152        139,
 153        149,
 154        151,
 155        157,
 156        163,
 157        167,
 158        173,
 159        179,
 160        181,
 161        191,
 162        193,
 163        197,
 164        199,
 165        211,
 166        223,
 167        227,
 168        229,
 169        233,
 170        239,
 171        241,
 172        251,
 173        257,
 174        263,
 175        269,
 176        271,
 177        277,
 178        281,
 179        283,
 180        293,
 181        307,
 182        311,
 183        313,
 184        317,
 185        331,
 186        337,
 187        347,
 188        349,
 189        353,
 190        359,
 191        367,
 192        373,
 193        379,
 194        383,
 195        389,
 196        397,
 197        401,
 198        409,
 199        419,
 1100        421,
 1101        431,
 1102        433,
 1103        439,
 1104        443,
 1105        449,
 1106        457,
 1107        461,
 1108        463,
 1109        467,
 1110        479,
 1111        487,
 1112        491,
 1113        499,
 1114        503,
 1115        509,
 1116        521,
 1117        523,
 1118        541,
 1119        547,
 1120        557,
 1121        563,
 1122        569,
 1123        571,
 1124        577,
 1125        587,
 1126        593,
 1127        599,
 1128        601,
 1129        607,
 1130        613,
 1131        617,
 1132        619,
 1133        631,
 1134        641,
 1135        643,
 1136        647,
 1137        653,
 1138        659,
 1139        661,
 1140        673,
 1141        677,
 1142        683,
 1143        691,
 1144        701,
 1145        709,
 1146        719,
 1147        727,
 1148        733,
 1149        739,
 1150        743,
 1151        751,
 1152        757,
 1153        761,
 1154        769,
 1155        773,
 1156        787,
 1157        797,
 1158        809,
 1159        811,
 1160        821,
 1161        823,
 1162        827,
 1163        829,
 1164        839,
 1165        853,
 1166        857,
 1167        859,
 1168        863,
 1169        877,
 1170        881,
 1171        883,
 1172        887,
 1173        907,
 1174        911,
 1175        919,
 1176        929,
 1177        937,
 1178        941,
 1179        947,
 1180        953,
 1181        967,
 1182        971,
 1183        977,
 1184        983,
 1185        991,
 1186        997
 1187    ];
 188
 189    /// <summary>
 190    ///     Time complexity - O(n)
 191    ///     Space complexity - O(1)
 192    /// </summary>
 193    /// <param name="nums"></param>
 194    /// <returns></returns>
 195    public bool PrimeSubOperation(int[] nums)
 3196    {
 20197        for (var i = nums.Length - 1; i > 0; i--)
 8198        {
 8199            if (nums[i] > nums[i - 1])
 4200            {
 4201                continue;
 202            }
 203
 4204            var found = false;
 205
 191206            foreach (var prime in Primes.Where(p => p < nums[i - 1] && nums[i - 1] - p < nums[i]))
 3207            {
 3208                nums[i - 1] -= prime;
 209
 3210                found = true;
 211
 3212                break;
 213            }
 214
 4215            if (!found)
 1216            {
 1217                return false;
 218            }
 3219        }
 220
 2221        return true;
 3222    }
 223}